-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsonos_user_data_legacy.py
99 lines (81 loc) · 4.13 KB
/
sonos_user_data_legacy.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
# Note: this is not the file where you change your user settings for Sonos
# if you're looking for that then try sonos_settings.py
# sorry, I know it's confusingly named - but it's too late to change now!
import requests
import json
import sonos_settings
import time
DEFAULT_TIMEOUT = 5
def find_unknown_radio_station_name(filename):
# BBC streams started via alexa don't return their real name, which is annoying... but fixable:
# if you find other stations which are not shown then you can add them below. Please put up on github will a pull request if you do
if filename == "bbc_radio_two.m3u8": return "BBC Radio 2"
if filename == "bbc_6music.m3u8": return "BBC Radio 6 Music"
if filename == "bbc_radio_hereford_worcester.m3u8": return "BBC Hereford & Worcester"
if filename == "bbc_radio_one.m3u8": return "BBC Radio 1"
if filename == "bbc_1xtra.m3u8": return "BBC Radio 1Xtra"
if filename == "bbc_radio_two.m3u8": return "BBC Radio 2"
if filename == "bbc_radio_three.m3u8": return "BBC Radio 3"
if filename == "bbc_radio_fourfm.m3u8": return "BBC Radio 4"
if filename == "bbc_radio_five_live.m3u8": return "BBC Radio 5 Live"
if filename == "bbc_radio_five_live_sports_extra.m3u8": return "BBC Radio 5 Live Sports Extra"
if filename == "bbc_world_service.m3u8": return "BBC World Service"
# if not found:
return "Radio"
def current(sonos_room):
# reset all the variables so we return a blank if it's not set by the function
current_trackname = ""
current_artist = ""
current_album = ""
current_image =""
playing_status = ""
# convert any spaces to url-suitable character
sonos_room = sonos_room.replace(" ", "%20")
# build URL
url = "http://" + sonos_settings.sonos_http_api_address + ":" + sonos_settings.sonos_http_api_port + "/" + sonos_room + "/state"
# download the raw json object and parse the json data
try:
data = requests.get(url, timeout=DEFAULT_TIMEOUT)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
print ("Error: http-sonos-api failed to answer; pausing 20 seconds to give it a chance to catch up")
time.sleep (20)
return "", "", "", "", "API error"
obj = json.loads(data.text)
# extract relevant data
try:
playing_status = obj['playbackState']
except KeyError:
print ("Error: http-sonos-api object is missing playbackState")
time.sleep (10)
return "", "", "", "", "API error"
type_playing = obj['currentTrack']['type']
# detect if its coming from Sonos radio, in which case forget that it's radio and pretend it's a normal track
uri = obj['currentTrack']['uri']
if uri.startswith('x-sonosapi-radio:sonos'):
type_playing = "sonos_radio"
if type_playing == "radio":
if 'stationName' in obj['currentTrack']:
# if Sonos has given us a nice station name then use that
current_trackname = obj['currentTrack']['stationName']
else:
# if not then try to look it up (usually because its played from Alexa)
current_trackname = str(find_unknown_radio_station_name(obj['currentTrack']['title']))
current_artist = ""
current_album = ""
if 'absoluteAlbumArtUri' in obj['currentTrack']:
current_image = obj['currentTrack']['absoluteAlbumArtUri']
else:
current_image = ""
if type_playing != "radio":
try:
current_trackname = obj['currentTrack']['title']
except:
return "", "", "", "", "API error"
if 'artist' in obj['currentTrack']: current_artist = obj['currentTrack']['artist']
if 'album' in obj['currentTrack']: current_album = obj['currentTrack']['album']
album_art_uri = obj['currentTrack'].get('albumArtUri')
if album_art_uri and album_art_uri.startswith('http'):
current_image = album_art_uri
elif 'absoluteAlbumArtUri' in obj['currentTrack']:
current_image = obj['currentTrack']['absoluteAlbumArtUri']
return current_trackname, current_artist, current_album, current_image, playing_status