-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
171 lines (141 loc) · 4.63 KB
/
__init__.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
import gc
import json
import re
import time
import buttons
import display
import mch22
import system
import urequests as requests
import wifi
sample_json = ""
# mastadon api response and current selection
toots = []
toot_index = 0
# a translation table for common characters that the badge has issues with (str.translate is not supported)
char_replacements = {'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'ß': 'ss', 'Ä': 'Ae', 'Ö': 'Oe', 'Ü': 'Ue'}
# some common html entities, since we don't have a full html unescape function in micropython
html_entities = {
'&': '&',
'"': '"',
''': '\'',
' ': ' ',
}
# font stuff
font = 'Roboto_Regular12'
font_height = display.getTextHeight("Happy", font)
max_text_width = display.width() - 10
line_margin = 3
def wlan_connect():
if not wifi.status():
wifi.connect()
display.drawFill(display.WHITE)
display.drawText(10, 10, "Connecting to WiFi...", 0x000000, font)
display.flush()
if not wifi.wait(10):
display.drawFill(display.WHITE)
display.drawText(10, 10, "Sorry, WiFi connection failed :(", 0x000000, font)
display.drawText(10, 25, "Try getting closer to the next Datenklo and", 0x000000, font)
display.drawText(10, 40, "avoid blocking the WiFi chip with the battery.", 0x000000, font)
display.drawText(10, 65, "Exit with HOME button", 0x000000, font)
display.flush()
while True:
time.sleep(1)
else:
print("connected!")
def draw_splash():
display.drawFill(display.WHITE)
display.drawText(10, 10, "Getting #mch2022 toots from chaos.social ...", 0x000000, font)
display.flush()
def fetch_toots():
r = requests.get("https://chaos.social/api/v1/timelines/tag/mch2022")
gc.collect()
data = r.json()
r.close()
return data
def button_exit_app(pressed):
if pressed:
mch22.exit_python()
def callback_next_toot(p):
if p:
switch_toot(toot_index + 1)
def callback_prev_toot(p):
if p:
switch_toot(toot_index - 1)
def switch_toot(index):
if index >= len(toots):
index = 0
if index < 0:
index = len(toots) - 1
print(f"toot #{index}")
global toot_index
toot_index = index
print_toot(toots[index])
def print_toot(toot_json):
# get content
content_html = toot_json['content']
content_plain = simplify_text(content_html)
author = toot_json['account']['username']
created = toot_json['created_at']
# author & date
display.drawFill(0xFFFFFF)
display.drawText(5, 5, f"{toot_index + 1}/{len(toots)}: @{author}, {created}:", 0x000000, font)
# print content lines
lines = text_to_lines(content_plain)
for i, line in enumerate(lines):
y = 10 + ((i + 1) * (font_height + line_margin))
display.drawText(5, y, line, 0x000000, font)
display.flush()
def simplify_text(text):
# strip html tags
plain = re.sub('<br.*?>', ' ', text)
plain = re.sub('<[^<]+?>', '', plain)
# replace non-ascii chars (not supported by badge)
plain = ''.join(char_replacements.get(c, c) for c in plain)
plain = ''.join(c if isascii(c) else '?' for c in plain)
# replace common html entities
if '&' in plain:
for html_entity, replacement in html_entities.items():
plain = plain.replace(html_entity, replacement)
return plain
def text_to_lines(text):
words = text.split()
line = ''
lines = []
for word in words:
line_tmp = line + ' ' + word
line_width = display.getTextWidth(line_tmp, font)
if line_width > max_text_width:
lines.append(line)
line = word
else:
line = line_tmp
if (not lines) or (lines[-1] != line):
lines.append(line)
return lines
def isascii(c):
return 0 <= ord(c) <= 0x7F
def main(debug=False):
global toots
buttons.attach(buttons.BTN_HOME, button_exit_app)
if debug:
draw_splash()
toots = json.loads(sample_json)
else:
wlan_connect()
draw_splash()
toots = fetch_toots()
if not toots:
display.drawFill(display.WHITE)
display.drawText(10, 10, "No toots received :'(", 0x000000, font)
display.flush()
else:
print_toot(toots[0])
buttons.attach(buttons.BTN_A, callback_next_toot)
buttons.attach(buttons.BTN_UP, callback_prev_toot)
buttons.attach(buttons.BTN_DOWN, callback_next_toot)
buttons.attach(buttons.BTN_LEFT, callback_prev_toot)
buttons.attach(buttons.BTN_RIGHT, callback_next_toot)
while True:
time.sleep(1)
main(debug=False)