summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xapp.py16
-rw-r--r--directory.py5
-rw-r--r--events.py12
-rw-r--r--session.py19
-rw-r--r--static/player.js5
5 files changed, 42 insertions, 15 deletions
diff --git a/app.py b/app.py
index 1da175f..9120ea4 100755
--- a/app.py
+++ b/app.py
@@ -4,6 +4,7 @@ import os, mimetypes, json, cgi, recode, time, urllib, events, threading
from config import config
from directory import Directory, File
+from session import Session
class Application(object):
# Application handlers
@@ -58,28 +59,17 @@ class Application(object):
encoder = args.getvalue('encoder') if 'encoder' in args else None
f = File(path)
- t = threading.Thread(target = f.recode, args = (decoder, encoder))
+ t = threading.Thread(target = f.recode, args = (decoder, encoder, environ['sessionid']))
t.start()
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
- def json_is_cached(self, environ, start_response, path):
- args = cgi.FieldStorage(environ = environ)
- path = args.getvalue('path') if 'path' in args else None
-
- cache_file = os.path.join(config.get('cache_dir'), path)
- cache_file = os.path.splitext(cache_file)[0] + '.mp3'
-
- start_response('200 OK', [('Content-Type', 'text/plain')])
- return json.dumps(os.path.exists(cache_file))
-
handlers = {
'files': files,
'static': static,
'list': json_list,
'cache': json_cache,
- 'is_cached': json_is_cached,
'events': events.EventSubscriber,
}
@@ -94,7 +84,7 @@ class Application(object):
path = ['static', 'index.html']
if module in self.handlers:
- return self.handlers[module](self, environ, start_response, path)
+ return Session(self.handlers[module])(self, environ, start_response, path)
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return [str(path)]
diff --git a/directory.py b/directory.py
index a93e00c..33391c6 100644
--- a/directory.py
+++ b/directory.py
@@ -89,7 +89,7 @@ class File(DirectoryEntry):
cache_file = os.path.splitext(cache_file)[0] + '.mp3'
return cache_file
- def recode(self, decoder, encoder):
+ def recode(self, decoder, encoder, sessionid = None):
decoder = recode.decoders[decoder]()
encoder = recode.encoders[encoder]()
recoder = recode.Recoder(decoder, encoder)
@@ -105,6 +105,9 @@ class File(DirectoryEntry):
recoder.recode(self.abs_path, cache_file)
events.event_pub.cached(self.path)
+ if sessionid:
+ events.event_pub.play(sessionid, '/files/{0}'.format(self.path))
+
def json(self):
cache_file = self.get_cache_file()
d = DirectoryEntry.json(self)
diff --git a/events.py b/events.py
index 4759a19..451e6f0 100644
--- a/events.py
+++ b/events.py
@@ -2,11 +2,13 @@ import zmq, json
from config import config
def EventSubscriber(app, environ, start_response, path):
+ session = 'session-' + environ['sessionid']
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(config.get('event_subscriber'))
socket.setsockopt(zmq.SUBSCRIBE, 'cached')
socket.setsockopt(zmq.SUBSCRIBE, 'recoding')
+ socket.setsockopt(zmq.SUBSCRIBE, session)
start_response('200 OK', [('Content-Type', 'text/event-stream')])
yield ': event source stream\n\n'
@@ -18,7 +20,12 @@ def EventSubscriber(app, environ, start_response, path):
break
message = socket.recv()
address, message = message.split(None, 1)
- if address in ('cached', 'recoding'):
+
+ # split session-specific messages
+ if address == session:
+ address, message = message.split(None, 1)
+
+ if address in ('cached', 'recoding', 'play'):
data = json.dumps({'type': address, 'path': message})
yield 'data: {0}\n\n'.format(data)
@@ -36,4 +43,7 @@ class EventPublisher(object):
def cached(self, path):
self.socket.send('cached {0}'.format(path))
+ def play(self, session, path):
+ self.socket.send('session-{0} play {1}'.format(session, path))
+
event_pub = EventPublisher()
diff --git a/session.py b/session.py
new file mode 100644
index 0000000..9928992
--- /dev/null
+++ b/session.py
@@ -0,0 +1,19 @@
+import Cookie, hashlib, time
+
+class Session(object):
+ def __init__(self, func):
+ self.func = func
+
+ def __call__(self, app, environ, start_response, path):
+ cookie = Cookie.SimpleCookie(environ['HTTP_COOKIE'] if 'HTTP_COOKIE' in environ else None)
+ sessionid = cookie.get('sessionid')
+ if not sessionid:
+ sessionid = hashlib.sha1(str(time.time) + environ['REMOTE_ADDR']).hexdigest()
+ cookie['sessionid'] = sessionid
+ start_response('302 Found', [
+ ('Set-Cookie', cookie['sessionid'].OutputString()),
+ ('Location', environ['REQUEST_URI'])])
+ return []
+ environ['sessionid'] = sessionid.value
+
+ return self.func(app, environ, start_response, path)
diff --git a/static/player.js b/static/player.js
index 70b5a6d..ce75249 100644
--- a/static/player.js
+++ b/static/player.js
@@ -153,6 +153,11 @@ function event_handler(event) {
if(a)
a.setAttribute('class', 'file file-' + data['type']);
break;
+ case 'play':
+ log('[play] ' + data['path']);
+ change_url(data['path']);
+ audio.play();
+ break;
default:
log('[event] unknown type: ' + data['type']);
}