-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwebserver.py
156 lines (136 loc) · 3.69 KB
/
webserver.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import cherrypy
import os
import mqueue
import pkgutil
skewlist = []
queue = mqueue.Queue()
stash = mqueue.Stash()
fetcher = mqueue.Fetcher()
volume = mqueue.Volume()
try:
playlist_max = int(os.getenv("MZ_PLAYLIST_MAX") or "20")
except ValueError:
playlist_max = 20
class Musicazoo:
def skew(self):
return cherrypy.request.headers.get("X-Forwarded-For") in skewlist
@cherrypy.expose
def index(self):
return pkgutil.get_data("mqueue", "index.html")
# @cherrypy.expose
# def dice(self):
# cherrypy.response.headers['Content-Type'] = 'image/svg'
# return dice_svg
@cherrypy.expose
@cherrypy.tools.json_out()
def enqueue(self, youtube_id):
if self.skew():
return {"success": False}
youtube_ids = fetcher.query_search(youtube_id)
if not youtube_ids:
return {"success": False}
for youtube_id in youtube_ids[:playlist_max]:
queue.enqueue_ytid(youtube_id)
return {"success": True}
@cherrypy.expose
@cherrypy.tools.json_out()
def status(self):
ents = queue.read_queue()
playback_status = queue.playback_status()
if self.skew():
playback_status["listing"] = []
playback_status["titles"] = {}
playback_status["loaded"] = {}
playback_status["volume"] = 0
else:
playback_status["listing"] = [ent.kvs for ent in ents]
playback_status["titles"] = {ent.ytid: queue.read_title(ent.ytid) for ent in ents}
playback_status["loaded"] = {ent.ytid: stash.exists(ent.ytid) for ent in ents}
playback_status["volume"] = volume.get_volume()
return playback_status
@cherrypy.expose
def delete(self, uuid):
if self.skew():
return
# TODO: why is this a loop?
while True:
found = queue.read_queue_by_uuid(uuid)
if found is None:
break
queue.remove(found)
@cherrypy.expose
def reorder(self, uuid, dir):
try:
rel = 1 if int(dir) >= 0 else -1
except ValueError:
return "fail"
if not queue.move(uuid, rel):
return "fail"
return "ok"
@cherrypy.expose
@cherrypy.tools.json_out()
def search(self, q):
return fetcher.query_search_multiple(q)
@cherrypy.expose
@cherrypy.tools.json_out()
def getvolume(self):
return volume.get_volume()
@cherrypy.expose
@cherrypy.tools.json_out()
def setvolume(self, vol):
if self.skew():
return "ok"
vol = min(volume.get_volume() + 5, int(vol))
try:
volume.set_volume(vol)
return "ok"
except ValueError:
return "fail"
@cherrypy.expose
@cherrypy.tools.json_out()
def pause(self):
queue.pause()
return "ok"
@cherrypy.expose
@cherrypy.tools.json_out()
def navigate(self, rel):
try:
rel = float(rel)
except ValueError:
return "fail"
queue.navigate(rel)
return "ok"
@cherrypy.expose
@cherrypy.tools.json_out()
def top(self):
frequencies = []
for member, (title, plays) in queue.play_counts():
frequencies.append((member, title, plays))
frequencies.sort(reverse=True, key=lambda x: x[2])
return frequencies
@cherrypy.expose
@cherrypy.tools.json_out()
def random(self):
youtube_id = queue.random_previous_ytid()
if not youtube_id:
return {"success": False}
youtube_ids = fetcher.query_search(youtube_id, search=False)
if youtube_ids is None or len(youtube_ids) != 1:
return {"success": False}
youtube_id = youtube_ids[0]
queue.enqueue_ytid(youtube_id, increment=False)
return {"success": True, "ytid": youtube_id}
cherrypy.config.update({'server.socket_port': 8000})
cherrypy.tree.mount(Musicazoo(), os.getenv("MZ_LOCATION") or "/", config={
"/images": {
"tools.staticdir.on": True,
"tools.staticdir.dir": os.path.abspath(os.path.join(os.path.dirname(__file__), "images")),
},
"/video": {
"tools.staticdir.on": True,
"tools.staticdir.dir": os.path.abspath(stash.dir),
},
})
if __name__ == "__main__":
cherrypy.engine.start()
cherrypy.engine.block()