-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.py
153 lines (102 loc) · 3.66 KB
/
ws.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ,--(saket)--(RiPlayServer)--(27/07/16 07:49)--(【ツ】)---
# `--(~/ws.py$)-->
import json
import re
from threading import Lock
import player
__tag__ = 'ws.py'
_subscribers = list()
_subscriber_lock = Lock()
def handler(connection, data):
"""Handle WS requests"""
if data == "status":
ws_status(connection)
elif data == "start":
ws_start(connection)
elif data == "stop":
ws_stop(connection)
elif data == "skip":
ws_skip(connection)
elif data == "collection":
ws_collection(connection)
elif data == "playlist":
ws_playlist(connection)
elif data == "clear":
ws_clear(connection)
elif data == "freq":
ws_freq(connection)
elif re.match(r"play (?:\d+|all|shuffle)", data):
n = data.split()[1]
ws_play(connection, n if n in ["all", "shuffle"] else int(n))
elif re.match(r"queue \d+", data):
n = int(data.split()[1])
ws_queue(connection, n)
elif re.match(r"output (?:radio|audio)", data):
ws_output(connection, data.split()[1])
else:
connection.sendMessage(data)
def ws_start(connection):
"""Handle WS request to Start player"""
# connection.sendMessage(json.dumps(dict(started=True)))
player.start_player()
def ws_stop(connection):
"""Handle WS request to Stop player"""
# connection.sendMessage(json.dumps(dict(stopped=True)))
player.stop_player()
def ws_skip(connection):
"""Handle WS request to Skip song"""
if not player.get_playlist():
connection.sendMessage(json.dumps(dict(skipped=None)))
return
# connection.sendMessage(json.dumps(dict(skipped=player.get_playlist()[0])))
player.skip()
def ws_collection(connection):
"""Handle WS request for Music Collection"""
connection.sendMessage(player.get_collection_json())
def ws_playlist(connection):
"""Handle WS request for current Playlist"""
connection.sendMessage(player.get_playlist_json())
def ws_status(connection):
"""Handle WS request for current status"""
connection.sendMessage(player.get_status_json())
def ws_clear(connection):
"""Handle WS request to clear playlist"""
# connection.sendMessage(json.dumps(dict(cleared=True)))
player.clear()
def ws_freq(connection):
"""Handle WS request for Broadcast Frequency"""
connection.sendMessage(json.dumps(dict(freq=player.get_freq())))
def ws_play(connection, n):
"""Handle WS request to Play Song n-> [int|"all"|"shuffle"]."""
# connection.sendMessage(json.dumps(dict(playing=True)))
player.play(n)
def ws_queue(connection, n):
"""Handle WS request to Add to playlist"""
connection.sendMessage(json.dumps(dict(queued=player.get_collection().get(n))))
player.queue(n)
def ws_subscribe(connection):
if connection not in _subscribers:
_subscriber_lock.acquire()
_subscribers.append(connection)
_subscriber_lock.release()
_inform_subscriber(connection)
def ws_unsubscribe(connection):
if connection in _subscribers:
_subscriber_lock.acquire()
_subscribers.remove(connection)
_subscriber_lock.release()
def ws_output(connection, output):
radio_out = (output == "radio")
if radio_out != player.get_mode():
connection.sendMessage(json.dumps(dict(output=output)))
player.set_mode(radio_out)
def inform_subscribers():
_subscriber_lock.acquire()
for subscriber in _subscribers:
_inform_subscriber(subscriber)
_subscriber_lock.release()
def _inform_subscriber(subscriber):
status = player.get_status_json()
subscriber.sendMessage("status->%s" % status)