-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.py
50 lines (35 loc) · 1.1 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
import os
import json
import urllib3
from sys import exit
from inky import InkyPHAT
from PIL import Image, ImageFont, ImageDraw
from font_fredoka_one import FredokaOne
# Set current directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Load graphic
img = Image.open("./logo.png")
draw = ImageDraw.Draw(img)
# get api data
http = urllib3.PoolManager()
try:
r = http.request('GET', 'http://10.0.7.230/admin/api.php')
if r.status >= 200 or r.status <= 299:
exit("http request is unsuccessful")
except:
adsblocked = '?'
ratioblocked = '?'
parsed_json = json.loads(r.data)
adsblocked = parsed_json['ads_blocked_today']
ratioblocked = parsed_json['ads_percentage_today']
font = ImageFont.truetype(FredokaOne, 32)
inky_display = InkyPHAT("red")
inky_display.set_border(inky_display.WHITE)
draw.text((20,20), str(adsblocked), inky_display.BLACK, font)
if isinstance(ratioblocked, str):
draw.text((20,50), f"{ratioblocked}%", inky_display.BLACK, font)
else:
draw.text((20,50), f"{ratioblocked:.2f}%", inky_display.BLACK, font)
img = img.rotate(180)
inky_display.set_image(img)
inky_display.show()