diff options
Diffstat (limited to 'app.py')
-rwxr-xr-x | app.py | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -1,6 +1,6 @@ #!/usr/bin/env python2 -import os, mimetypes, json, cgi, recode, time, urllib, events, db, itertools +import os, mimetypes, json, cgi, recode, time, urllib, events, db, itertools, re from config import config from directory import Directory, File @@ -136,6 +136,32 @@ class Application(object): start_response('200 OK', [('Content-Type', 'text/plain')]) return [] + re_search = re.compile(r'(")?((?(1)[^"]|[^ ])+)(?(1)")') + + def json_search(self, environ, start_response, path): + args = cgi.FieldStorage(environ = environ) + query = args.getvalue('query') + r = self.re_search.findall(query) + d = {} + l = [] + for _, v in r: + if ':' in v: + k, v = v.split(':', 1) + d[k] = v + else: + l.append(v) + + results = [] + try: + session = db.Session() + r = db.Track.search(session, *l, **d) + results = [File(x.get_path(), metadata = x.get_metadata()).json() for x in r] + finally: + session.close() + + start_response('200 OK', []) + return json.dumps(results) + handlers = { 'files': files, 'static': static, @@ -143,6 +169,7 @@ class Application(object): 'list': json_list, 'recode': json_recode, 'play': json_play, + 'search': json_search, 'events': events.EventSubscriber, } |