-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
147 lines (93 loc) · 3.4 KB
/
main.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
from tornado import ioloop, web
import soco
import json
import random
import sys
print("restarted")
class YoHandler(web.RequestHandler):
def addTrack(self, track):
#kwargs = {'album': track.album, 'album_art_uri': track.album_art_uri, 'creator': track.creator,
# 'original_track_number': track.original_track_number}
#content = {'uri': track.uri + "#" + str(random.randint(0,99999)), 'title': track.title, 'parent_id': track.parent_id}
#content.update(kwargs)
#print(track.title)
#ntrack = soco.data_structures.MLTrack(track.uri + "#" + str(random.randint(0,99999)), track.title, track.parent_id, **kwargs)
#ntrack.item_id = track.parent_id + ":" + str(random.randint(0,99999))
try:
player.add_to_queue(track)
except Exception as e:
print("first failed", e)
player.add_uri_to_queue(track.uri)
finally:
pass
def get(self, *args, **kwargs):
global currlist
username = self.request.path[1:] #Where the Yo was sent
yofrom = self.get_argument("username", None, False)
if yofrom == None:
self.write("error: no username")
return
else:
print("yo from " + yofrom)
self.write("hi, yo")
self.finish() #End the HTTP Request, real stuff begins.
playlists = json.loads(open("playlists.json", "r").read())
id = None
for playlist in playlists:
if playlist['name'] == username:
id = playlist['id']
break
if id == None:
print("Playlist not found")
return
else:
print("playlist id: " + id)
if currlist == id:
player.pause()
return
spl = player.get_sonos_playlists()
for l in spl:
if l.item_id == id:
if l.title == currlist:
if player.get_current_transport_info()['current_transport_state'] != "PLAYING":
print("played")
player.play()
else:
print("paused")
player.pause()
return
currlist = l.title
player.clear_queue()
tracks = player.browse(l)
print(tracks)
first = tracks.pop()
print(first)
self.addTrack(first)
player.play()
for track in tracks:
self.addTrack(track)
break
#player = soco.discover().pop() #ToDo: User pick which Sonos
currlist = None
#player.play_uri(list.uri)
class IndexHandler(web.RequestHandler):
def get(self, *args, **kwargs):
self.write("Hello World!") #Why not
conf = json.loads(open("conf.json", "r").read())
#playlists = json.loads(open("playlists.json", "r").read())
player = None
for s in soco.discover(): #Find SONOS from conf
if s.uid == conf['sonos_id']:
player = s
break
if player == None:
sys.exit("Specific Sonos not found. Try running config.py again?")
app = web.Application([
(r'/', IndexHandler),
#(r'/yocall', YoHandler),
#(r'/yocall/([^/]+)', YoHandler),
(r'/([^/]+)', YoHandler),
], debug=True)
if __name__ == '__main__':
app.listen(conf['portnum'])
ioloop.IOLoop.instance().start()