-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·370 lines (282 loc) · 11.2 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/python3
from inky import InkyPHAT
from PIL import ImageFont, ImageDraw, Image
import textwrap
import requests
import time
from datetime import datetime
import yaml
from noaa_sdk import noaa
import requests
from requests.exceptions import HTTPError
import json
import logging
import os
import sys
import stockquotes
# Election
from urllib.request import urlopen
from xml.etree.ElementTree import parse
from datetime import datetime
logging.basicConfig(
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
logging.info("""Status Dashboard for InkyPhat eInk display. Displays Weather, Covid-19 info, PiHole stats, Air Pollution from my personal station and top Hacker News stories.
Press Ctrl+C to exit!
""")
font = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 10)
redBigFont = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16)
bigFont = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf", 15)
medWFont = ImageFont.truetype(
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 15)
''' Font Options
DejaVuSans-Bold.ttf DejaVuSansMono.ttf DejaVuSerif-Bold.ttf
DejaVuSansMono-Bold.ttf DejaVuSans.ttf DejaVuSerif.ttf
'''
'''
Lazy person code copying
sudo nano /usr/lib/systemd/system/eink.service
sudo systemctl start eink.service
sudo systemctl daemon-reload
journalctl -f
'''
# Display Setup
inky_display = InkyPHAT('red')
inky_display.set_border(inky_display.RED)
img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
draw = ImageDraw.Draw(img)
# Setup When I want particular feeds to display
covidHours = [16,17,18,19,20,21]
stockHours = [8,9,10,11,12,13,14,15]
forecastHours = [6,7,8,9,10,17,18,19,20,12,22]
hnHours = [9,11,13,15,17,19,21]
piHours = [12,15,17]
#res1day = ""
# Utility Functions
def getConfig():
# absolute to run as cron. PiTA
with open('/home/pi/inkydash/config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
zipcode = config[0]["zip"]
country = config[0]["country"]
state = config[0]["state"]
stocks = config[0]["stocks"]
return zipcode, country, state, stocks
def fetchFeed(url):
try:
response = requests.get(url)
response.raise_for_status()
if 'application/json' in response.headers['content-type']:
jsonResponse = response.json()
return jsonResponse
elif 'text/plain' in response.headers['content-type']:
textResponse = response.text
return textResponse
else:
logging.warning("Unknown format in jquery assume json")
jsonResponse = response.json()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
logging.warning(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
logging.warning(f'Other error occurred: {err}')
def drawClean(color):
if color == 'WHITE':
draw.rectangle((0, 0, inky_display.WIDTH,
inky_display.HEIGHT),
(inky_display.WHITE))
elif color == 'BLACK':
draw.rectangle((0, 0, inky_display.WIDTH,
inky_display.HEIGHT),
(inky_display.BLACK))
elif color == 'RED':
draw.rectangle((0, 0, inky_display.WIDTH,
inky_display.HEIGHT),
(inky_display.RED))
else:
draw.rectangle((0, 0, inky_display.WIDTH,
inky_display.HEIGHT),
(inky_display.WHITE))
def drawScreen():
inky_display.set_image(img.rotate(180))
inky_display.show()
def getWeather():
try:
n = noaa.NOAA()
res = n.get_forecasts(config[0], config[1], False)
res1day = res[0]["detailedForecast"]
# display on InkyPHAT
# Take string of text and wrap it at 38 chars
txt = textwrap.fill(res1day, 36)
# Get size of text
w, h = draw.multiline_textsize(txt, font)
# Center Center the text
x = (inky_display.WIDTH / 2) - (w / 2)
y = (inky_display.HEIGHT / 2) - (h / 2)
drawClean('WHITE')
# Draw multiline text on scren
if (w * h) <= 11000:
txt = textwrap.fill(res1day, 24)
draw.multiline_text((x, 0), txt, inky_display.BLACK, medWFont)
elif (w * h) > 11000:
draw.multiline_text((x, 0), txt, inky_display.BLACK, font)
drawScreen()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
logging.warning(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
logging.warning(f'Other error occurred: {err}')
def getCovid():
drawClean('WHITE')
jsonResponse = fetchFeed(f'https://api.covidtracking.com/v1/states/{config[2]}/daily.json')
todayCovid = jsonResponse[0]["hospitalizedCurrently"]
yesterdayCovid = jsonResponse[1]["hospitalizedCurrently"]
currentlyHospitalized = "Hospitalized: " + "{:,}".format(todayCovid)
hospitalizationChange = str(
todayCovid - yesterdayCovid) + " new hospitalizations"
newCases = "{:,}".format(jsonResponse[0]["positiveIncrease"]) + " new positivity"
covidUpdate = datetime.now().strftime('%a %b %d %-I:%M %p')
draw.text((0, 0), "CO Covid Update", inky_display.BLACK, bigFont)
draw.text((0, 20), currentlyHospitalized, inky_display.BLACK, bigFont)
draw.text((0, 40), hospitalizationChange, inky_display.RED, bigFont)
draw.text((0, 60), newCases, inky_display.RED, bigFont)
draw.text((0, 88), covidUpdate, inky_display.BLACK, font)
drawScreen()
def getCurrentConditions():
drawClean('WHITE')
# AQI
aqiResponse = fetchFeed(
'https://io.adafruit.com/api/v2/drkpxl/feeds/pollution.aqi')
printAqi = "Current AQI: " + aqiResponse["last_value"]
draw.text((0, 0), printAqi, inky_display.BLACK, bigFont)
# Current Temp
tempResponse = fetchFeed(
'https://io.adafruit.com/api/v2/drkpxl/feeds/temp')
printTemp = "Current Temp: " + tempResponse["last_value"] + " °F"
draw.text((0, 20), printTemp, inky_display.BLACK, bigFont)
# Current Humidity
humResponse = fetchFeed(
'https://io.adafruit.com/api/v2/drkpxl/feeds/humidity')
printHum = "Current Humidity: " + humResponse["last_value"] + " %"
draw.text((0, 40), printHum, inky_display.BLACK, bigFont)
# Feed Last Update
lastUpdate = aqiResponse["updated_at"]
draw.text((0, 88), lastUpdate, inky_display.BLACK, font)
drawScreen()
def getHackerNews():
topics = fetchFeed('https://hacker-news.firebaseio.com/v0/topstories.json')
toptopics = [topics[0], topics[1], topics[2]]
topic = ""
for x in toptopics:
r = fetchFeed(
'https://hacker-news.firebaseio.com/v0/item/' + str(x) + '.json')
topic = (r['title'])
score = str(r['score']) + " pts"
# Take string of text and wrap it at 38 chars
txt = textwrap.fill(topic, 24)
# Get size of text
w, h = draw.multiline_textsize(txt, bigFont)
# Center Center the text
x = (inky_display.WIDTH / 2) - (w / 2)
y = (inky_display.HEIGHT / 2) - (h / 2)
drawClean('BLACK')
# Draw multiline text on scren
draw.multiline_text((x, y), txt, inky_display.WHITE, bigFont)
draw.text((0, 0), score, inky_display.RED, redBigFont)
drawScreen()
time.sleep(60)
def getPihole():
drawClean('BLACK')
piholeResponse = fetchFeed('http://pi.hole/admin/api.php')
printPercentBlockedToday = "PiHoled: " + str(round(piholeResponse["ads_percentage_today"])) + " %"
printClientsSeen = "Total Clients: " + str(piholeResponse["clients_ever_seen"])
printQueriesToday = "Queries Today: {:,}".format(piholeResponse['dns_queries_today'])
#('{:,}'.format(value))
printStatus = "PiHole Status: " + piholeResponse['status']
draw.text((0, 0), printStatus, inky_display.WHITE, bigFont)
draw.text((0, 20), printQueriesToday, inky_display.WHITE, bigFont)
draw.text((0, 40), printPercentBlockedToday, inky_display.RED, bigFont)
draw.text((0, 60), printClientsSeen, inky_display.WHITE, bigFont)
# Get Public IP, both work.
#ipResponse = fetchFeed('https://api.ipify.org?format=json')
ipResponse = fetchFeed('https://icanhazip.com')
printIpResonse = "Public IP: " + ipResponse
draw.text((0, 80), printIpResonse, inky_display.WHITE, bigFont)
drawScreen()
def getStocks():
stockSymbols = config[3]
for x in stockSymbols:
drawClean('WHITE')
symbolData = stockquotes.Stock(x)
printName = symbolData.name + " (" + symbolData.symbol + ")"
# Get currnet price, adjust value to have $, comma, and 2 decimal points, also crop photo to show a max char of 7
printCurrentPrice = str("${:,.2f}".format(symbolData.current_price))[0:7]
printIncreasePercent = str(symbolData.increase_percent) + "%"
printHighLow = "H: " + str("${:,.2f}".format(symbolData.historical[0]['high']))[0:7] + " / L: " + str("${:,.2f}".format(symbolData.historical[0]['low']))[0:7]
draw.text((0, 20), printName, inky_display.BLACK, bigFont)
draw.text((0, 40), printCurrentPrice, inky_display.BLACK, bigFont)
if symbolData.increase_percent <= 0:
draw.text((68, 40), ("▼ " + printIncreasePercent), inky_display.RED, bigFont)
else:
draw.text((68, 40), ("▲ " + printIncreasePercent), inky_display.BLACK, bigFont)
draw.text((0, 60), printHighLow, inky_display.BLACK, bigFont)
drawScreen()
time.sleep(30)
def getElection():
var_url = urlopen('https://raw.githubusercontent.com/alex/nyt-2020-election-scraper/master/battleground-state-changes.xml')
xmldoc = parse(var_url)
for item in xmldoc.iterfind('channel/item'):
description = item.findtext('description')
date = item.findtext('pubDate')
print(description)
# Take string of text and wrap it at 38 chars
txt = textwrap.fill(description, 24)
# Get size of text
w, h = draw.multiline_textsize(txt, bigFont)
# Center Center the text
x = (inky_display.WIDTH / 2) - (w / 2)
y = (inky_display.HEIGHT / 2) - (h / 2)
drawClean('WHITE')
currentLead = (int(''.join(list(filter(str.isdigit, description)))))
currentLead = "{:,}".format(currentLead)
if "Trump" in txt:
draw.multiline_text((x, y), txt, inky_display.RED, bigFont)
#logging.info(f'Trump Current Lead: {currentLead}')
else:
draw.multiline_text((x, y), txt, inky_display.BLACK, bigFont)
#logging.info(f'Biden Current Lead: {currentLead}')
drawScreen()
time.sleep(20)
# Get initial YAML
config = getConfig()
# Main Loop
while True:
try:
currentTime = ((datetime.now()).hour)
getElection()
if currentTime in covidHours:
getCovid()
time.sleep(180)
if currentTime in stockHours:
getStocks()
if currentTime in hnHours:
getHackerNews()
if currentTime in piHours:
getPihole()
time.sleep(180)
getCurrentConditions()
time.sleep(180)
if currentTime in forecastHours:
getWeather()
time.sleep(180)
except (KeyboardInterrupt, SystemExit):
print('\nkeyboardinterrupt found!')
sys.exit(0)
print('\n...Program Stopped Manually!')
raise