From 914761368f6a5f19df26019e8290b4e86fb76cfb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 23 Aug 2011 18:15:26 +0200 Subject: Implemented simple search. --- app.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'app.py') diff --git a/app.py b/app.py index f02dcc6..a799c6f 100755 --- a/app.py +++ b/app.py @@ -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, } -- cgit v1.2.3