diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-08-23 18:15:26 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-08-23 18:15:26 +0200 |
commit | 914761368f6a5f19df26019e8290b4e86fb76cfb (patch) | |
tree | cf479451cec2e109889f077ffaf7549d019e2b0e /app.py | |
parent | c8754366695abe10364963f1fe0c82dad5987102 (diff) |
Implemented simple search.
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, } |