-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebService.py
More file actions
74 lines (50 loc) · 1.5 KB
/
WebService.py
File metadata and controls
74 lines (50 loc) · 1.5 KB
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
import web
import json
import soco
from soco import SoCo
class SonosDevice:
def __init__(self, name, ip):
self.name = name
self.ip = ip
def toJSON(self):
return dict(name=self.name, ip=self.ip)
urls = (
'/', 'index',
'/play', 'play',
'/list', 'listDevices',
'/device', 'deviceStatus'
)
class index:
def GET(self):
return "SoCo webapi"
class listDevices:
def GET(self):
web.header('Content-Type', 'application/json')
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
deviceList = []
for zone in soco.discover():
o = SonosDevice(zone.player_name, zone.ip_address)
deviceList.append(o)
return json.dumps([item.toJSON() for item in deviceList])
class play:
def GET(self):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
data = web.input(uri="no", player="no")
sonos = SoCo('192.168.1.105')
sonos.play_uri(data.uri)
track = sonos.get_current_track_info()
return track['title'] + " - " + data.player
class deviceStatus:
def GET(self):
web.header('Content-Type', 'application/json')
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
data = web.input(player="blank")
sonos = SoCo(data.player)
track = sonos.get_current_track_info()
return json.dumps(track)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()