summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-08-23 18:15:26 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-08-23 18:15:26 +0200
commit914761368f6a5f19df26019e8290b4e86fb76cfb (patch)
treecf479451cec2e109889f077ffaf7549d019e2b0e /app.py
parentc8754366695abe10364963f1fe0c82dad5987102 (diff)
Implemented simple search.
Diffstat (limited to 'app.py')
-rwxr-xr-xapp.py29
1 files changed, 28 insertions, 1 deletions
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,
}