forked from timvonwerne/KVBMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkvbmonitor.py
More file actions
70 lines (59 loc) · 2 KB
/
Copy pathkvbmonitor.py
File metadata and controls
70 lines (59 loc) · 2 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
import schedule
import time
from bs4 import BeautifulSoup
import requests
import threading
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
STATION_ID = '632'
MATRIX_COLS = 32
MATRIX_ROWS = 64
HEADERS = {
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36"
}
options = RGBMatrixOptions()
options.rows = MATRIX_ROWS
options.cols = MATRIX_COLS
options.chain_length = 1
options.parallel = 1
options.hardware_mapping = 'regular'
matrix = RGBMatrix(options = options)
canvas = matrix.CreateFrameCanvas()
font = graphics.Font()
font.LoadFont("resources/fonts/4x6.bdf")
textColor = graphics.Color(255, 255, 0)
pos = canvas.width
def get_departures():
url = "https://kvb.koeln/qr/%d/" % int(STATION_ID)
r = requests.get(url, headers=HEADERS)
soup = BeautifulSoup(r.text, features="html.parser")
tables = soup.find_all("table", class_="display")
departures = []
for row in tables[0].find_all("tr"):
tds = row.find_all("td")
(line_id, direction, time) = (tds[0].text, tds[1].text, tds[2].text)
line_id = line_id.replace(u"\xa0", "")
direction = direction.replace(u"\xa0", "")
time = time.replace(u"\xa0", " ").strip().lower()
try:
line_id = int(line_id)
except:
pass
if(direction == "Bocklemünd" or direction == "Rochusplatz"):
departures.append({
"line_id": line_id,
"direction": direction,
"wait_time": time
})
return departures
def print_departures_to_matrix():
canvas.Clear()
lines = []
for d in get_departures():
line = "{0} {1} {2}".format(str(d['line_id']), d['direction'], d['wait_time'])
print(line)
graphics.DrawText(canvas, font, pos, 10, textColor, line)
print_departures_to_matrix()
schedule.every(10).seconds.do(print_departures_to_matrix)
while 1:
schedule.run_pending()
time.sleep(1)