-
Notifications
You must be signed in to change notification settings - Fork 0
/
pihole-robot-chomp.py
executable file
·76 lines (65 loc) · 1.87 KB
/
pihole-robot-chomp.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
#!/usr/bin/env python
import json
import random
import requests
import time
try:
from PIL import Image
except ImportError:
exit("This script requires the pillow module\nInstall with: sudo pip install pillow")
import scrollphathd
IMAGE_BRIGHTNESS = 0.5
FILE_BASE = '/home/pi/chompbot'
images = []
def load_image(filename):
img = Image.open(filename)
buffer = []
for x in range(0, 17):
buffer.append([])
for y in range(0, 7):
buffer[x].append(get_pixel(img, x, y))
images.append(buffer)
def get_pixel(img, x, y):
p = img.getpixel((x,y))
if img.getpalette() is not None:
r, g, b = img.getpalette()[p:p+3]
p = max(r, g, b)
elif type(p) is tuple:
p = max(p)
return (p / 255.0)
load_image(FILE_BASE+'/robot_chomp_open.bmp')
load_image(FILE_BASE+'/robot_chomp.bmp')
def chomp():
for c in range(6):
for img in images:
for x in range(0, 17):
for y in range(0, 7):
brightness = img[x][y]
scrollphathd.pixel(x, 6-y, brightness * IMAGE_BRIGHTNESS)
#time.sleep(0.00001)
scrollphathd.show()
time.sleep(0.2)
f = open(FILE_BASE+'/apitoken')
API_TOKEN = f.readline().rstrip()
api_url = "http://localhost/admin/api.php?summaryRaw&auth="+API_TOKEN
blocked_count = 0
try:
while True:
try:
r = requests.get(api_url)
data = json.loads(r.text)
blocked_today = data['ads_blocked_today']
except KeyError:
time.sleep(1)
continue
if blocked_today > blocked_count:
print(blocked_today)
chomp()
blocked_count = blocked_today
else:
scrollphathd.fill(0)
scrollphathd.show()
time.sleep(1)
except KeyboardInterrupt:
scrollphathd.clear()
scrollphathd.show()