forked from ipeacocks/lametric-sonos-indicator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
75 lines (66 loc) · 2.74 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
import json
import requests
import time
import os
class LaSo:
def __init__(self, lametric_ip, lametric_user, lametric_api_key, node_sonos_http_api_ip):
self.lametric_ip = lametric_ip
self.lametric_user = lametric_user
self.lametric_api_key = lametric_api_key
self.node_sonos_http_api_ip = node_sonos_http_api_ip
def get_track(self):
node_sonos_http_api_url = "http://%s:5005/state" % (self.node_sonos_http_api_ip)
response = requests.get(node_sonos_http_api_url)
if response.status_code == 200:
json_response = response.json()
track = {
"title": json_response['currentTrack']['title'],
"artist": json_response['currentTrack']['artist'],
}
status = json_response['playbackState']
else:
track = {
"title": "",
"artist": "",
}
status = "STOPPED"
return track, status
def send_notification(self):
track, status = self.get_track()
if status == 'PLAYING':
artist = track['artist']
title = track['title']
api_url = "http://%s:8080/api/v2/device/notifications" % (self.lametric_ip)
headers = {'Content-Type': 'application/json; charset=utf-8'}
basicAuthCredentials = (self.lametric_user, self.lametric_api_key)
data = '{"priority":"warning","model":{"frames":[{"icon":"19113","text":"%s: %s"}],"cycles":3}}' % (artist, title)
try:
response = requests.post(api_url,
headers=headers,
auth=basicAuthCredentials,
data=data.encode('utf-8'),
timeout=3)
# for debugging purpose
print(f"{artist} - {title}")
except requests.exceptions.RequestException as err:
print ("OOps: Something Else", err)
except requests.exceptions.HTTPError as errh:
print ("Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
print ("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print ("Timeout Error:", errt)
def main():
laso = LaSo(os.environ["LAMETRIC_IP"],
"dev",
os.environ["LAMETRIC_API_KEY"],
os.environ["NODE_SONOS_HTTP_API_IP"])
# if envar DELAY isn't set than it equals 60
delay = os.getenv('DELAY', 60)
# print(delay)
while True:
laso.get_track()
laso.send_notification()
time.sleep(int(delay))
if __name__ == "__main__":
main()