-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweather.py
More file actions
73 lines (63 loc) · 2.21 KB
/
weather.py
File metadata and controls
73 lines (63 loc) · 2.21 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
import threading
import time
import urllib
import json
import urllib2
import Image
import ImageDraw
import constants
import requests
import logs
import math
class weather:
def __init__(self, base):
self.base = base
self.config = base.config
self.weather = {'weather': '75',
'conditions': 'SUNNY',
'state': 'demo'}
self.start = time.time()
t = threading.Thread(target=self.thread)
t.daemon = True
t.start()
def queryWeatherEndpoint(self):
try:
url = \
'https://api.trainsignapi.com/prod-weather/weather/zipCode/' \
+ self.config["weather"]["zip_code"]
querystring = {"":""}
headers = {'x-api-key': self.config["settings"]["prod_api_key"]}
response = requests.request(
'GET', url, headers=headers, params=querystring)
data = json.loads(response.text)
self.weather["weather"] = str(int(data['data']['temperature']))
self.weather["conditions"] = str(data['data']['summary']).upper()
self.weather["state"] = "live"
except Exception as e:
logs.logger.info(
'Weather module', extra={
'status': 0,
'job': 'weather_module',
'error': str(e)
})
def thread(self):
while True:
self.config = self.base.config
while self.config['settings']['state'] == 'online':
self.queryWeatherEndpoint()
time.sleep(120)
def draw(self):
image = Image.new('RGB', (constants.width, constants.height))
draw = ImageDraw.Draw(image)
draw.text(
(2, 0),
'IT IS ' + self.weather["weather"] + ' DEGREES' ,
font=constants.font, fill=constants.red
)
draw.text(
(2, 16),
'& ' + self.weather["conditions"] + ' OUTSIDE',
font=constants.font, fill=constants.green
)
self.base.matrix.SetImage(image, 0, 0)
time.sleep(self.base.getTransitionTime())