-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·107 lines (84 loc) · 2.98 KB
/
app.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
import logging
import os
import requests
import sys
from time import sleep
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
logger.addHandler(ch)
# Screenly config
COLD_PLAYLIST_ID = os.getenv('COLD_PLAYLIST_ID')
SCREENLY_TOKEN = os.getenv('TOKEN')
HOST = 'https://api.screenlyapp.com'
# Weather service API key and target location
DARKSKY_API_KEY = os.getenv('DARKSKY_API_KEY')
LAT = os.getenv('LAT')
LNG = os.getenv('LNG')
# LOGIC
TEMP_THRESHOLD = os.getenv('TEMP_THRESHOLD')
ABOVE_OR_BELOW = os.getenv('ABOVE_OR_BELOW')
RETRY_TIMEOUT = float(os.getenv('RETRY_TIMEOUT', default=5 * 60))
def get_weather():
payload = {'units': 'si'}
weather_lookup = requests.get(
'https://api.darksky.net/forecast/{}/{},{}'.format(
DARKSKY_API_KEY,
LAT,
LNG
),
params=payload,
)
if not weather_lookup.ok:
logger.error('Failed to perform weather lookup.')
return
return weather_lookup.json()['currently']['temperature'], weather_lookup.json()['currently']['precipProbability']
def control_playlist(requestor, enable=True):
requestor.patch(
'{}/api/v3/playlists/{}/'.format(HOST, COLD_PLAYLIST_ID),
{"is_enabled": enable}
)
def main():
# Do weather lookup
if not (DARKSKY_API_KEY and LAT and LNG):
logger.error('Missing weather variables.')
sys.exit(1)
# Build logic
if not (TEMP_THRESHOLD and ABOVE_OR_BELOW):
logger.error('Missing logic variables.')
sys.exit(1)
session = requests.Session()
session.headers["Authorization"] = "Token {}".format(SCREENLY_TOKEN)
below = 'below' in ABOVE_OR_BELOW.lower()
above = 'above' in ABOVE_OR_BELOW.lower()
# local state
enabled = None
if below and above:
logger.error('Specify above or below. Not both.')
sys.exit(1)
switch_criteria = (
lambda temperature: temperature < float(TEMP_THRESHOLD) if below
else lambda temperature: temperature > float(TEMP_THRESHOLD)
)
while True:
temperature, precipProbability = get_weather()
logger.info('Got temperature reading: {}'.format(temperature))
logger.info('Got precipitation probability reading: {}'.format(precipProbability))
if switch_criteria(temperature):
if enabled in [False, None]:
control_playlist(session, enable=True)
enabled = True
logger.info('Enabling cold playlist')
else:
logger.info('Cold playlist is already enabled')
else:
if enabled in [None, True]:
control_playlist(session, enable=False)
enabled = False
logger.info('Disabling cold playlist')
else:
logger.info('Cold playlist is already disabled')
logger.info('Sleeping for {} seconds'.format(RETRY_TIMEOUT))
sleep(RETRY_TIMEOUT)
if __name__ == '__main__':
main()