-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
416 lines (313 loc) · 13.6 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import io
import json
import os
from urllib.request import Request, urlopen
import pygame
import typing
import enum
import math
import datetime
import api
import logger
size = (0, 0)
# TODO: settings and colorschemes
pygame.init()
logger.reset_log()
zermelo = None
username = ''
password = ''
tenant = 'gymnasiumnovum'
week_nr = str(int(datetime.datetime.now().strftime('%Y%U')))
week = None
# print(week_nr)
config_file = "config.json"
custom_background = False
custom_background_url = ""
clean_ui = False
display_informations = True
background_image = None
class State(enum.IntEnum):
username = enum.auto()
password = enum.auto()
login = enum.auto() # logging into zermelo
main = enum.auto()
state = State.username
state_login_fail = False
def save_config():
logger.log('Saving config...')
filename = config_file
with open(filename, 'w') as file:
config_json_data = {
"config": {
"custom_background": custom_background,
"background_url": custom_background_url,
"clean_ui": clean_ui,
"display_informations": display_informations
}
}
json.dump(config_json_data, file)
def load_config():
global custom_background # TODO: fix this trash code
global custom_background_url
global clean_ui
global background_image
global display_informations
filename = config_file
logger.log(f"Loading config '{filename}'")
if filename not in os.listdir('.'):
logger.warn('No config, creating new...')
with open(filename, 'w') as file:
config_json_data = {
"config": {
"custom_background": False,
"background_url": "",
"clean_ui": False,
"display_informations": True
}
}
json.dump(config_json_data, file)
return
with open(filename, "r+") as file:
try:
config_json_data: dict = json.load(file)
try:
custom_background = config_json_data['config']['custom_background']
custom_background_url = config_json_data['config']['background_url']
clean_ui = config_json_data['config']['clean_ui']
display_informations = config_json_data['config']['display_informations']
# request background image
if custom_background and custom_background_url != "":
try:
request_site = Request(custom_background_url, headers={"User-Agent": "Mozilla/5.0"})
background_file = io.BytesIO(urlopen(request_site).read())
background_image = pygame.image.load(background_file)
except Exception as le:
logger.error(le)
except KeyError as ke:
logger.warn("Broken config file detected! Resetting...")
logger.error(ke)
file.truncate()
save_config()
except json.decoder.JSONDecodeError as jde:
logger.error(f'{filename} JSONDecodeError')
logger.error(jde)
logger.error(f'{filename}:')
file.seek(0)
logger.error("'" + str(file.read()) + "'")
try:
with open('credentials.txt', 'r') as file:
data = file.read()
split = data.split('\n')
if len(split) != 3:
raise ValueError
username = split[0]
password = split[1]
tenant = split[2]
state = State.login
zermelo = api.Api(username, password, tenant)
load_config()
except FileNotFoundError:
pass
except ValueError:
pass
def loading_spinner(x: int, y: int):
global screen, size, frame
speed = .06
s = size[1] // 10
pygame.draw.rect(screen, (0, 0, 0), (x, y, s, s))
# pygame.draw.lines(screen, (255, 255, 255), False, [
# (x + math.sin(frame * speed) * s / 2, y + math.cos(frame * speed) * s / 2),
# (x + math.sin(frame * speed + math.pi / 2) * s / 2, y + math.cos(frame * speed + math.pi / 2) * s / 2),
# (x + math.sin(frame * speed + math.pi) * s / 2, y + math.cos(frame * speed + math.pi) * s / 2)
# ], size[1] // 30)
circles = 7
for i in range(circles):
pygame.draw.circle(screen, (255, 255, 255),
(x + math.sin(frame * speed + i / circles * math.pi * ((frame + i / circles) * .01)) * s / 2,
y + math.cos(frame * speed + i / circles * math.pi * ((frame + i / circles) * .01)) * s / 2),
size[1] // 80)
def add_week(delta: int):
global week_nr
if delta not in [-1, 0, 1]:
raise NotImplementedError('Bigger changes than 1')
if delta == 0:
return week_nr
w = [week_nr[:4], week_nr[-2:]]
if (int(w[1]) < 52 and delta == 1) or (int(w[1]) > 1 and delta == -1):
w[1] = str(int(w[1]) + delta)
if len(w[1]) == 1:
w[1] = '0' + w[1]
else:
assert len(w[1]) == 2, f'Week not 1 or 2 number(s) but {len(w[1])}'
else:
w[0] = str(int(w[0]) + delta)
if delta == -1:
w[1] = '52'
else:
w[1] = '01'
return ''.join(w)
def start_of_week():
global week_nr
start_day = datetime.datetime.strptime(week_nr[:4], '%Y')
t = datetime.datetime.strptime(week_nr[:4] + ' ' + str(int(week_nr[-2:]) * 7 - start_day.isoweekday() + 2), '%Y %j')
return t
pre_mouse_press = [False, False, False]
font = pygame.font.Font('fonts/ubuntu.ttf', 10)
big_font = pygame.font.Font('fonts/ubuntu.ttf', 20)
def resize():
global size, font, big_font, dash_line
font = pygame.font.Font('fonts/ubuntu.ttf', size[1] // 30)
big_font = pygame.font.Font('fonts/ubuntu.ttf', size[1] // 20)
dash_line = pygame.Surface((size[0], 1))
lines = 7 * 3
for i in range(lines):
pygame.draw.rect(dash_line, (100, 100, 100),
(int((i / lines + .25/lines) * size[0]), 0,
int(.5 / lines * size[0]), 1))
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
clock = pygame.time.Clock()
run = True
size = screen.get_size() # different because (0, 0) makes it fit to the screen
dash_line = pygame.Surface((size[0], 1))
resize()
frame = 0
logger.log("Initialized smort-agenda")
while run:
clock.tick(60)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.WINDOWRESIZED:
size = screen.get_size()
resize()
elif event.type == pygame.TEXTINPUT: # TODO: TEXTEDITING event
if state == State.username:
username += event.text
if state == State.password:
password += event.text
elif event.type == pygame.KEYDOWN:
if state == State.username:
if event.key == pygame.K_BACKSPACE:
username = username[:-1]
elif event.key == pygame.K_RETURN:
state = State.password
elif state == State.password:
if event.key == pygame.K_BACKSPACE:
password = password[:-1]
elif event.key == pygame.K_RETURN:
state = State.login
zermelo = api.Api(username, password, tenant)
elif state == State.main:
if event.key == pygame.K_RIGHT:
week_nr = add_week(1)
elif event.key == pygame.K_LEFT:
week_nr = add_week(-1)
elif event.key == pygame.K_r:
week_nr = str(int(datetime.datetime.now().strftime('%Y%U')))
if zermelo is not None and state == State.main:
zermelo.update()
week = zermelo.get(week_nr)
zermelo.get(add_week(-1))
zermelo.get(add_week(1))
if state != State.main:
screen.blit(font.render(str(state)[6:] + "...", True, (255, 255, 255)), (0, 0))
if state == State.username:
screen.blit(big_font.render(username, True, (255, 255, 255)), (100, 100))
if state_login_fail:
s = font.render('Incorrect password and/or username!', True, (255, 0, 0))
screen.blit(s, (size[0] - s.get_width(), 0))
elif state == State.password:
screen.blit(big_font.render(username, True, (255, 255, 255)), (100, 100))
screen.blit(big_font.render('#' * len(password), True, (255, 255, 255)), (100, 100 + size[1] // 20))
state_login_fail = False
elif state == State.login:
loading_spinner(size[1] // 10, size[1] // 10 + 10)
pygame.draw.rect(screen, (255, 255, 255),
(0, size[1] // 5, round(zermelo.state / zermelo.max_state * size[0]), size[1] // 20))
if zermelo.state == zermelo.max_state:
state = State.main
logger.log("Login successfull!")
with open('credentials.txt', 'w') as file:
file.write(username + '\n' + password + '\n' + tenant) # TODO: prompt the user if they want to store
if not zermelo.successfull:
if not zermelo.credentials_correct:
state = State.username
state_login_fail = True
username = ''
password = ''
logger.error("Incorrect password and/or username!")
else:
zermelo = api.Api(username, password, tenant) # TODO: notify user
logger.warn("Server crash??")
elif state == State.main:
cancelled_subjects = []
if week is not None:
# print(week, week.appointments, week.raw)
height = int(size[1] / 23.983)
width = size[0] // 7
# TODO: maybe fill weekdays with (50, 50, 50)?
if custom_background and background_image is not None:
background_image = pygame.transform.smoothscale(background_image, size)
screen.blit(background_image, (0, 0))
else:
for i in range(24):
screen.blit(dash_line, (0, i * height))
for i in range(7):
pygame.draw.rect(screen, (100, 100, 100), (i * width, 0, 1, size[1]))
y = 0
x = 0
for appointment in week.appointments:
# print(appointment.start.isoweekday(), appointment.start.hour + appointment.start.minute / 60)
x = width * (appointment.start.isoweekday() - 1)
y = int(height * (appointment.start.hour + appointment.start.minute / 60))
h = int(height * (appointment.end.hour + appointment.end.minute / 60) - y)
# y = round(height * (23 + 59 / 60))
if appointment.valid:
c = (30, 30, 30)
else:
c = (100, 0, 0)
if not clean_ui: pygame.draw.rect(screen, c, (x, y, width, h))
str_subjects = ', '.join(appointment.subjects)
str_teachers = ', '.join(appointment.teachers)
str_locations = ', '.join(appointment.locations)
if not appointment.optional:
s = font.render((str_subjects
+ (' - ' if str_teachers != '' else '') + str_teachers
+ (' - ' if str_locations != '' else '') + str_locations),
True, (255, 255, 255))
else:
s = font.render(str(len(appointment.options)), True, (150, 255, 150))
if s.get_height() > h:
ratio = s.get_width() / s.get_height()
# print(ratio, s.get_width(), s.get_height() * ratio)
s = pygame.transform.smoothscale(s, (h * ratio, h))
screen.blit(s, (x, y))
if appointment.cancelled:
c = (255, 50, 50)
cancelled_subjects.append(appointment)
elif appointment.optional:
c = (50, 255, 50)
else:
c = (255, 255, 255)
pygame.draw.rect(screen, c, (x, y, width, h), 1)
y += height
else:
loading_spinner(size[1] // 10, size[1] // 10 + 50)
if display_informations:
screen.blit(font.render(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'), True, (255, 255, 255)), (0, 0)) # TODO: this will render over anything planned for 0 AM monday
screen.blit(font.render(start_of_week().strftime('%d %B'), True, (255, 255, 255)), (0, 25))
if len(cancelled_subjects) > 0:
cy = 0
for c in cancelled_subjects:
cy += 25
ny = size[1] - cy - 40
screen.blit(font.render("Uitgevallen: ", True, (255, 255, 255)), (0, ny))
for cs in cancelled_subjects:
screen.blit(font.render((" " + (', '.join(cs.subjects) if ', '.join(cs.subjects) != "" else "leeg subject") + " > " + datetime.datetime.strftime(cs.start, "%H:%M") + " ~ " + datetime.datetime.strftime(cs.end, "%H:%M") + (" - " + ', '.join(cs.locations if ', '.join(cs.locations) != "" else "?") + " (" + datetime.datetime.strftime(cs.start, "%d %B") + ")")) , True, (255, 255, 255)), (0, ny + 25))
ny += 25
# screen.blit(font.render('Hello, World', True, (255, 255, 255)), (0, 0))
# screen.blit(big_font.render('Hello, World', True, (255, 255, 255)), (0, size[1] // 30))
pygame.display.update()
frame += 1
pygame.quit()