forked from plamere/SmarterPlaylists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherrypy_server.py
102 lines (86 loc) · 2.81 KB
/
cherrypy_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys
import cherrypy
from cherrypy import tools
import ConfigParser
import json
import time
import components
import compiler
import pbl
import time
class SmarterPlaylistServer(object):
def __init__(self):
pass
@cherrypy.expose
@tools.json_out()
def inventory(self):
start = time.time()
print 'inventory'
results = {
'status': 'ok',
'inventory': components.exported_inventory,
'types': components.inventory['types']
}
print 'inventory', time.time() - start
return results
@cherrypy.expose
@tools.json_out()
@tools.json_in()
def run(self):
print 'inventory'
start = time.time()
# program = request.json
print cherrypy.request.headers
cl = cherrypy.request.headers['Content-Length']
#rawbody = cherrypy.request.body.read(int(cl))
program = cherrypy.request.json
print 'got program', program
status, obj = compiler.compile(program)
print 'compiled in', time.time() - start, 'secs'
if 'max_tracks' in program:
max_tracks = program['max_tracks']
else:
max_tracks = 40
results = { 'status': status}
if status == 'ok':
tracks = []
tids = pbl.get_tracks(obj, max_tracks)
print
for i, tid in enumerate(tids):
print i, pbl.tlib.get_tn(tid)
tracks.append(pbl.tlib.get_track(tid))
print
results['tracks'] = tracks
results['name'] = obj.name
results['time'] = time.time() - start
print 'compiled and executed in', time.time() - start, 'secs'
if app.trace:
print json.dumps(results, indent=4)
print 'run', time.time() - start
return results
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
cherrypy.response.headers['Content-Type']= 'application/json'
def error_page_404(status, message, traceback, version):
cherrypy.response.headers['Content-Type']= 'application/json'
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
results = { 'status' : 'error', 'reason': message}
return json.dumps(results)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--debug':
DEBUG = True
cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
config = {
'global' : {
'server.socket_host' : '0.0.0.0',
'server.socket_port' : 5000,
'server.thread_pool' : 10,
},
'/' : {
'tools.CORS.on' : True,
'tools.caching.on' : True,
'error_page.404': error_page_404,
}
}
cherrypy.quickstart(SmarterPlaylistServer(), '/sps', config=config)