unitoolkit 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ladok_vakt/__init__.py +7 -0
- ladok_vakt/__main__.py +51 -0
- ladok_vakt/core.py +553 -0
- ladok_vakt/login.py +232 -0
- tools.json +623 -0
- unitoolkit/__init__.py +1 -0
- unitoolkit/__main__.py +63 -0
- unitoolkit/agent.py +270 -0
- unitoolkit/browser.py +147 -0
- unitoolkit/canvas.py +189 -0
- unitoolkit/chainproxy.py +113 -0
- unitoolkit/citation.py +165 -0
- unitoolkit/databases.py +497 -0
- unitoolkit/docs.py +394 -0
- unitoolkit/fulltext.py +260 -0
- unitoolkit/koha.py +729 -0
- unitoolkit/mcp.py +85 -0
- unitoolkit/programme.py +147 -0
- unitoolkit/search.py +507 -0
- unitoolkit/serve.py +153 -0
- unitoolkit/sharepoint.py +86 -0
- unitoolkit/surface.py +86 -0
- unitoolkit/timeedit.py +209 -0
- unitoolkit/tools.py +555 -0
- unitoolkit-0.7.0.dist-info/METADATA +177 -0
- unitoolkit-0.7.0.dist-info/RECORD +30 -0
- unitoolkit-0.7.0.dist-info/WHEEL +5 -0
- unitoolkit-0.7.0.dist-info/entry_points.txt +4 -0
- unitoolkit-0.7.0.dist-info/licenses/LICENSE +21 -0
- unitoolkit-0.7.0.dist-info/top_level.txt +2 -0
ladok_vakt/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""ladok-vakt: mejl när Ladok-anmälan öppnar (and nothing else).
|
|
2
|
+
|
|
3
|
+
Read-only. The tool never registers anything for you; it watches the
|
|
4
|
+
registration windows that Ladok already shows and tells you when one opens or
|
|
5
|
+
is about to close.
|
|
6
|
+
"""
|
|
7
|
+
__version__ = '0.1.0'
|
ladok_vakt/__main__.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""CLI: `python3 -m ladok_vakt` (silent unless something is worth saying).
|
|
2
|
+
|
|
3
|
+
python3 -m ladok_vakt # cron mode: prints only alerts/faults/heartbeat
|
|
4
|
+
python3 -m ladok_vakt --all # every window, always
|
|
5
|
+
python3 -m ladok_vakt --json # machine-readable
|
|
6
|
+
python3 -m ladok_vakt --test-mail # prove the notification path works
|
|
7
|
+
python3 -m ladok_vakt --login # optional: re-login with the browser's saved password
|
|
8
|
+
|
|
9
|
+
Exit codes: 0 ok (including "nothing to report"), 2 configuration problem.
|
|
10
|
+
"""
|
|
11
|
+
import json
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
from . import core
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main(argv):
|
|
18
|
+
cfg = core.Config()
|
|
19
|
+
want_alert = not ('--all' in argv or '--json' in argv)
|
|
20
|
+
email = '--no-email' not in argv
|
|
21
|
+
|
|
22
|
+
if '--login' in argv:
|
|
23
|
+
from .login import login
|
|
24
|
+
try:
|
|
25
|
+
print('inloggning: %s' % login(cfg))
|
|
26
|
+
return 0
|
|
27
|
+
except core.Fault as exc:
|
|
28
|
+
print('inloggning misslyckades: %s' % exc)
|
|
29
|
+
return 2
|
|
30
|
+
|
|
31
|
+
if '--test-mail' in argv:
|
|
32
|
+
subject = 'ladok-vakt: test av notifieringen'
|
|
33
|
+
body = ('Det här är ett test. Inget har hänt i Ladok.\n\n'
|
|
34
|
+
'Kanaler som svarade: %s\n')
|
|
35
|
+
done = core.notify(cfg, subject, body % 'test')
|
|
36
|
+
print('kanaler: %s' % (', '.join(done) if done else 'inga'))
|
|
37
|
+
return 0 if done else 2
|
|
38
|
+
|
|
39
|
+
lines = core.run(cfg, alert=want_alert, email=email)
|
|
40
|
+
|
|
41
|
+
if '--json' in argv:
|
|
42
|
+
print(json.dumps({'lines': lines, 'state': str(cfg.state_file)}, ensure_ascii=False, indent=1))
|
|
43
|
+
return 0
|
|
44
|
+
|
|
45
|
+
if lines:
|
|
46
|
+
print('\n'.join(lines))
|
|
47
|
+
return 0
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
if __name__ == '__main__':
|
|
51
|
+
sys.exit(main(sys.argv[1:]))
|
ladok_vakt/core.py
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
"""Core of ladok-vakt: fetch Ladok registration windows, decide, notify.
|
|
2
|
+
|
|
3
|
+
Design rules, in order of importance:
|
|
4
|
+
|
|
5
|
+
1. Read only. Only GETs against Ladok's student API. Nothing is ever registered,
|
|
6
|
+
unregistered or changed. The human still does the anmälan.
|
|
7
|
+
2. Silence means success. A run that found data and has nothing urgent to say
|
|
8
|
+
prints nothing, so it can run from cron every morning without noise.
|
|
9
|
+
3. Silence may never hide a fault. A run that could not fetch, that got no
|
|
10
|
+
courses, or that has not succeeded in ``stale_hours`` always says so, even
|
|
11
|
+
from cron. Faults are also emailed, because a chat message is exactly what a
|
|
12
|
+
sleeping laptop misses.
|
|
13
|
+
4. No credentials in the repo. The session comes from a browser the human
|
|
14
|
+
logged in to; mail credentials come from files or env vars.
|
|
15
|
+
|
|
16
|
+
Ladok has no student API with a token, so the data is read out of a
|
|
17
|
+
LibreOffice-free, already-authenticated Chrome over the DevTools protocol: the
|
|
18
|
+
student pages call ``https://student.ladok.se/student/proxy/<larosate>/...``
|
|
19
|
+
with the session cookie, and so do we.
|
|
20
|
+
"""
|
|
21
|
+
import asyncio
|
|
22
|
+
import datetime as dt
|
|
23
|
+
import json
|
|
24
|
+
import os
|
|
25
|
+
import re
|
|
26
|
+
import smtplib
|
|
27
|
+
import urllib.error
|
|
28
|
+
import urllib.request
|
|
29
|
+
import xml.etree.ElementTree as ET
|
|
30
|
+
from email.message import EmailMessage
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
S = '{http://schemas.ladok.se/studiedeltagande}'
|
|
34
|
+
B = '{http://schemas.ladok.se}'
|
|
35
|
+
R = '{http://schemas.ladok.se/resultat}'
|
|
36
|
+
|
|
37
|
+
LADOK_ORIGIN = 'https://www.student.ladok.se'
|
|
38
|
+
START_PATH = '/student/app/studentwebb/start'
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Config:
|
|
42
|
+
"""Everything comes from env vars so the tool is publishable as-is."""
|
|
43
|
+
|
|
44
|
+
def __init__(self, env=None):
|
|
45
|
+
e = os.environ if env is None else env
|
|
46
|
+
self.cdp = e.get('LADOK_CDP', 'http://127.0.0.1:9233')
|
|
47
|
+
self.larosate = e.get('LADOK_LAROSATE', '38') # 38 = Sophiahemmet
|
|
48
|
+
self.state_dir = Path(e.get('LADOK_STATE_DIR',
|
|
49
|
+
str(Path.home() / '.local' / 'state' / 'ladok-vakt')))
|
|
50
|
+
self.stale_hours = float(e.get('LADOK_STALE_HOURS', '36'))
|
|
51
|
+
self.close_days = int(e.get('LADOK_CLOSE_DAYS', '3'))
|
|
52
|
+
self.mail_to = e.get('LADOK_MAIL_TO', '')
|
|
53
|
+
self.mail_from = e.get('LADOK_MAIL_FROM', 'ladok-vakt <vakt@example.org>')
|
|
54
|
+
self.mail_min_hours = float(e.get('LADOK_MAIL_MIN_HOURS', '20'))
|
|
55
|
+
self.resend_key_file = Path(e.get('LADOK_RESEND_KEY_FILE',
|
|
56
|
+
str(Path.home() / '.secrets' / 'RESEND_KEY')))
|
|
57
|
+
self.smtp_password_file = Path(e.get('LADOK_SMTP_PASSWORD_FILE', '')) \
|
|
58
|
+
if e.get('LADOK_SMTP_PASSWORD_FILE') else None
|
|
59
|
+
self.smtp_user = e.get('LADOK_SMTP_USER', '')
|
|
60
|
+
self.smtp_host = e.get('LADOK_SMTP_HOST', 'smtp.gmail.com')
|
|
61
|
+
self.smtp_port = int(e.get('LADOK_SMTP_PORT', '587'))
|
|
62
|
+
self.monday_heartbeat = e.get('LADOK_HEARTBEAT', '1') not in ('0', 'false', 'no')
|
|
63
|
+
# Optional re-login through the browser's own saved password. Off by default;
|
|
64
|
+
# see ladok_vakt/login.py for exactly what it does and does not touch.
|
|
65
|
+
self.autologin = e.get('LADOK_AUTOLOGIN', '0').lower() not in ('0', 'false', 'no', '')
|
|
66
|
+
self.idp_entity = e.get('LADOK_IDP_ENTITY', '')
|
|
67
|
+
# Any of these can carry the fault notice instead of (or as well as) mail.
|
|
68
|
+
self.webhook_url = e.get('LADOK_WEBHOOK_URL', '')
|
|
69
|
+
self.webhook_format = e.get('LADOK_WEBHOOK_FORMAT', 'slack') # slack|discord|ntfy|generic
|
|
70
|
+
self.telegram_token = e.get('LADOK_TELEGRAM_TOKEN', '')
|
|
71
|
+
self.telegram_chat = e.get('LADOK_TELEGRAM_CHAT', '')
|
|
72
|
+
self.notify_command = e.get('LADOK_NOTIFY_COMMAND', '')
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def state_file(self):
|
|
76
|
+
return self.state_dir / 'state.json'
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def log_file(self):
|
|
80
|
+
return self.state_dir / 'ladok-vakt.log'
|
|
81
|
+
|
|
82
|
+
def proxy(self, path):
|
|
83
|
+
"""Relative to the Ladok origin on purpose: an absolute URL with the wrong
|
|
84
|
+
host (www. vs not) is a different origin and carries no session cookie."""
|
|
85
|
+
return '/student/proxy/%s%s' % (self.larosate, path)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# --------------------------------------------------------------------------
|
|
89
|
+
# Chrome / DevTools
|
|
90
|
+
# --------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
def _tab(cdp):
|
|
93
|
+
try:
|
|
94
|
+
tabs = json.load(urllib.request.urlopen(cdp + '/json/list', timeout=5))
|
|
95
|
+
except Exception as exc:
|
|
96
|
+
raise Fault('hittar ingen Chrome på %s (%s)' % (cdp, exc))
|
|
97
|
+
for t in tabs:
|
|
98
|
+
if t.get('type') == 'page' and 'ladok' in t.get('url', ''):
|
|
99
|
+
return t
|
|
100
|
+
try:
|
|
101
|
+
# Chrome 111+ rejects GET /json/new with 405; it must be PUT.
|
|
102
|
+
return json.load(urllib.request.urlopen(urllib.request.Request(
|
|
103
|
+
cdp + '/json/new?' + LADOK_ORIGIN + START_PATH, method='PUT'), timeout=10))
|
|
104
|
+
except Exception as exc:
|
|
105
|
+
raise Fault('kan inte öppna en Ladok-flik (%s)' % exc)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
async def _fetch_all(cdp, paths):
|
|
109
|
+
import websockets
|
|
110
|
+
tab = _tab(cdp)
|
|
111
|
+
out = {}
|
|
112
|
+
async with websockets.connect(tab['webSocketDebuggerUrl'], max_size=80_000_000) as ws:
|
|
113
|
+
i = 0
|
|
114
|
+
for p in paths:
|
|
115
|
+
i += 1
|
|
116
|
+
expr = ("fetch(%s,{credentials:'include'})"
|
|
117
|
+
".then(r=>r.text().then(t=>r.status+'|'+t))" % json.dumps(p))
|
|
118
|
+
await ws.send(json.dumps({'id': i, 'method': 'Runtime.evaluate',
|
|
119
|
+
'params': {'expression': expr, 'awaitPromise': True,
|
|
120
|
+
'returnByValue': True}}))
|
|
121
|
+
while True:
|
|
122
|
+
m = json.loads(await ws.recv())
|
|
123
|
+
if m.get('id') == i:
|
|
124
|
+
break
|
|
125
|
+
v = (m.get('result', {}).get('result') or {}).get('value') or ''
|
|
126
|
+
if not isinstance(v, str): # evaluate threw: an error object, not text
|
|
127
|
+
v = ''
|
|
128
|
+
st, _, body = v.partition('|')
|
|
129
|
+
out[p] = (int(st or 0), body)
|
|
130
|
+
return out
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def fetch(cfg):
|
|
134
|
+
"""Return (student_uid, registration_xml, exam_xml). Raises Fault."""
|
|
135
|
+
who = cfg.proxy('/studiedeltagande/internal/student/inloggad/som')
|
|
136
|
+
res = asyncio.run(_fetch_all(cfg.cdp, [who]))
|
|
137
|
+
st, body = res[who]
|
|
138
|
+
if st != 200 or 'StudentElement' not in body:
|
|
139
|
+
raise Fault('ingen Ladok-session: logga in i agent-Chrome och kör igen')
|
|
140
|
+
m = re.search(r'/student/([0-9a-f-]{36})', body)
|
|
141
|
+
if not m:
|
|
142
|
+
raise Fault('Ladok svarade utan student-id (inloggad med annan roll?)')
|
|
143
|
+
uid = m.group(1)
|
|
144
|
+
paths = [cfg.proxy('/studiedeltagande/internal/tillfallesdeltagande/'
|
|
145
|
+
'kurstillfallesdeltagande/student/' + uid),
|
|
146
|
+
cfg.proxy('/resultat/internal/studentweb/aktivitetstillfalle/'
|
|
147
|
+
'inloggadstudent/relevanta')]
|
|
148
|
+
res = asyncio.run(_fetch_all(cfg.cdp, paths))
|
|
149
|
+
# One retry: the proxy occasionally answers 404 for a moment under load.
|
|
150
|
+
for p in paths:
|
|
151
|
+
st, body = res[p]
|
|
152
|
+
if st != 200 or 'schemas.ladok.se' not in body:
|
|
153
|
+
res.update(asyncio.run(_fetch_all(cfg.cdp, [p])))
|
|
154
|
+
return uid, res[paths[0]][1], res[paths[1]][1]
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class Fault(Exception):
|
|
158
|
+
"""Something is wrong with the watch itself, not with the student's Ladok."""
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# --------------------------------------------------------------------------
|
|
162
|
+
# Parsing
|
|
163
|
+
# --------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
def _text(el, tag):
|
|
166
|
+
x = el.find(tag)
|
|
167
|
+
return (x.text or '').strip() if x is not None and x.text else None
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _first(el, tag):
|
|
171
|
+
for x in el.iter(tag):
|
|
172
|
+
if x.text and x.text.strip():
|
|
173
|
+
return x.text.strip()
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _name(el):
|
|
178
|
+
"""Swedish course name from a Benamningar block, else the first name given.
|
|
179
|
+
|
|
180
|
+
Scope matters: the first <base:Benamning> in a course element is the
|
|
181
|
+
*tillfällestyp* ("Kurs, grundnivå"), not the course name.
|
|
182
|
+
"""
|
|
183
|
+
if el is None:
|
|
184
|
+
return None
|
|
185
|
+
sv = first = None
|
|
186
|
+
for b in el.iter(B + 'Benamning'):
|
|
187
|
+
txt = _text(b, B + 'Text')
|
|
188
|
+
if not txt:
|
|
189
|
+
continue
|
|
190
|
+
first = first or txt
|
|
191
|
+
if _text(b, B + 'Sprakkod') == 'sv':
|
|
192
|
+
sv = txt
|
|
193
|
+
return sv or first
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def parse_registrations(xml_text):
|
|
197
|
+
if 'schemas.ladok.se' not in (xml_text or ''):
|
|
198
|
+
raise Fault('Ladok svarade inte med kurstillfällen (trasig hämtning, inte tom)')
|
|
199
|
+
root = ET.fromstring(xml_text)
|
|
200
|
+
rows = []
|
|
201
|
+
for td in root.iter(S + 'Tillfallesdeltagande'):
|
|
202
|
+
info = td.find(S + 'Utbildningsinformation')
|
|
203
|
+
name = code = None
|
|
204
|
+
if info is not None:
|
|
205
|
+
ben = info.find(S + 'Benamningar')
|
|
206
|
+
name = _name(ben if ben is not None else info)
|
|
207
|
+
code = _text(info, S + 'Utbildningskod')
|
|
208
|
+
rp = td.find(S + 'Registreringsperiod')
|
|
209
|
+
rows.append({
|
|
210
|
+
'kind': 'kursregistrering',
|
|
211
|
+
'course': name,
|
|
212
|
+
'code': code,
|
|
213
|
+
'state': _text(td, S + 'Tillstand'),
|
|
214
|
+
'reg_opens': _text(rp, B + 'Startdatum') if rp is not None else None,
|
|
215
|
+
'reg_closes': _text(rp, B + 'Slutdatum') if rp is not None else None,
|
|
216
|
+
'course_start': _first(td, S + 'UtbildningsPeriodFran'),
|
|
217
|
+
'course_end': _first(td, S + 'UtbildningsPeriodTill'),
|
|
218
|
+
})
|
|
219
|
+
return rows
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def parse_exams(xml_text):
|
|
223
|
+
"""Exam registration windows. An empty list is the normal state."""
|
|
224
|
+
if 'schemas.ladok.se' not in (xml_text or ''):
|
|
225
|
+
return []
|
|
226
|
+
root = ET.fromstring(xml_text)
|
|
227
|
+
rows = []
|
|
228
|
+
for at in root.iter():
|
|
229
|
+
if not at.tag.endswith('Aktivitetstillfalle'):
|
|
230
|
+
continue
|
|
231
|
+
ben = None
|
|
232
|
+
for b in at.iter(B + 'Benamning'):
|
|
233
|
+
ben = _text(b, B + 'Text')
|
|
234
|
+
break
|
|
235
|
+
rows.append({
|
|
236
|
+
'kind': 'tentamen',
|
|
237
|
+
'course': ben,
|
|
238
|
+
'reg_opens': _text(at, R + 'AnmalningsperiodFran') or _first(at, B + 'Startdatum'),
|
|
239
|
+
'reg_closes': _text(at, R + 'AnmalningsperiodTill'),
|
|
240
|
+
'exam_date': _first(at, B + 'Startdatum'),
|
|
241
|
+
})
|
|
242
|
+
return rows
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def is_registered(row):
|
|
246
|
+
return (row.get('state') or '').endswith('.registrerad')
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def pending_actions(rows):
|
|
250
|
+
"""What is worth telling the human: courses not yet registered for, plus exams."""
|
|
251
|
+
out = []
|
|
252
|
+
for r in rows:
|
|
253
|
+
if r['kind'] == 'kursregistrering':
|
|
254
|
+
if is_registered(r):
|
|
255
|
+
continue
|
|
256
|
+
if r.get('reg_closes') is None:
|
|
257
|
+
continue
|
|
258
|
+
out.append(r)
|
|
259
|
+
return out
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def days_until(date_str):
|
|
263
|
+
if not date_str:
|
|
264
|
+
return None
|
|
265
|
+
try:
|
|
266
|
+
return (dt.date.fromisoformat(date_str[:10]) - dt.date.today()).days
|
|
267
|
+
except ValueError:
|
|
268
|
+
return None
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def urgent(rows, close_days):
|
|
272
|
+
out = []
|
|
273
|
+
for r in rows:
|
|
274
|
+
opens = days_until(r.get('reg_opens'))
|
|
275
|
+
closes = days_until(r.get('reg_closes'))
|
|
276
|
+
if (opens if opens is not None else 99) <= 0:
|
|
277
|
+
out.append(r)
|
|
278
|
+
elif (closes if closes is not None else 99) <= close_days:
|
|
279
|
+
out.append(r)
|
|
280
|
+
return out
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def describe(rows):
|
|
284
|
+
lines = []
|
|
285
|
+
for r in sorted(rows, key=lambda x: x.get('reg_opens') or '9999'):
|
|
286
|
+
left = days_until(r.get('reg_opens'))
|
|
287
|
+
when = 'öppen nu' if left is not None and left <= 0 else ('om %d d' % left if left is not None else '?')
|
|
288
|
+
line = '- %s (%s): anmälan %s till %s [%s]' % (
|
|
289
|
+
r.get('course') or '?', r.get('code') or r['kind'],
|
|
290
|
+
r.get('reg_opens') or '?', r.get('reg_closes') or '?', when)
|
|
291
|
+
if r.get('exam_date'):
|
|
292
|
+
line += ', tentamen %s' % r['exam_date']
|
|
293
|
+
if r.get('course_start'):
|
|
294
|
+
line += ', kursstart %s' % r['course_start']
|
|
295
|
+
lines.append(line)
|
|
296
|
+
return lines
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# --------------------------------------------------------------------------
|
|
300
|
+
# State + log
|
|
301
|
+
# --------------------------------------------------------------------------
|
|
302
|
+
|
|
303
|
+
def load_state(cfg):
|
|
304
|
+
try:
|
|
305
|
+
return json.loads(cfg.state_file.read_text())
|
|
306
|
+
except Exception:
|
|
307
|
+
return {}
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def save_state(cfg, state):
|
|
311
|
+
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
312
|
+
cfg.state_file.write_text(json.dumps(state, indent=1))
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def log(cfg, line):
|
|
316
|
+
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
317
|
+
with cfg.log_file.open('a') as f:
|
|
318
|
+
f.write(dt.datetime.now().isoformat(timespec='seconds') + ' ' + line + '\n')
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def stale_note(cfg, state):
|
|
322
|
+
last = state.get('last_success')
|
|
323
|
+
if not last:
|
|
324
|
+
return None
|
|
325
|
+
try:
|
|
326
|
+
age = (dt.datetime.now() - dt.datetime.fromisoformat(last)).total_seconds() / 3600
|
|
327
|
+
except ValueError:
|
|
328
|
+
return None
|
|
329
|
+
if age >= cfg.stale_hours:
|
|
330
|
+
return ('- VARNING: ingen lyckad Ladok-hämtning på %.0f h (senast %s). '
|
|
331
|
+
'Kontrollera Chrome och inloggningen.' % (age, last))
|
|
332
|
+
return None
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
# --------------------------------------------------------------------------
|
|
336
|
+
# Mail: Resend or SMTP, whichever the human configured
|
|
337
|
+
# --------------------------------------------------------------------------
|
|
338
|
+
|
|
339
|
+
def send_mail(cfg, subject, text):
|
|
340
|
+
if not cfg.mail_to:
|
|
341
|
+
log(cfg, 'mail skipped: LADOK_MAIL_TO is not set')
|
|
342
|
+
return False
|
|
343
|
+
if cfg.smtp_password_file:
|
|
344
|
+
return _send_smtp(cfg, subject, text)
|
|
345
|
+
return _send_resend(cfg, subject, text)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _send_resend(cfg, subject, text):
|
|
349
|
+
try:
|
|
350
|
+
key = cfg.resend_key_file.read_text().strip()
|
|
351
|
+
except OSError:
|
|
352
|
+
log(cfg, 'mail skipped: no Resend key at %s' % cfg.resend_key_file)
|
|
353
|
+
return False
|
|
354
|
+
body = json.dumps({'from': cfg.mail_from, 'to': [cfg.mail_to],
|
|
355
|
+
'subject': subject, 'text': text}).encode()
|
|
356
|
+
req = urllib.request.Request('https://api.resend.com/emails', body, {
|
|
357
|
+
'Authorization': 'Bearer ' + key,
|
|
358
|
+
'Content-Type': 'application/json',
|
|
359
|
+
# Cloudflare in front of Resend rejects python-urllib's default agent
|
|
360
|
+
# with a bare 403, so name ourselves.
|
|
361
|
+
'User-Agent': 'ladok-vakt/0.1 (+https://github.com/vhsgreed/ladok-vakt)'})
|
|
362
|
+
try:
|
|
363
|
+
with urllib.request.urlopen(req, timeout=20) as r:
|
|
364
|
+
ok = 200 <= r.status < 300
|
|
365
|
+
log(cfg, 'mail via resend ok=%s' % ok)
|
|
366
|
+
return ok
|
|
367
|
+
except urllib.error.HTTPError as e:
|
|
368
|
+
try:
|
|
369
|
+
detail = e.read().decode()[:300]
|
|
370
|
+
except Exception:
|
|
371
|
+
detail = ''
|
|
372
|
+
log(cfg, 'mail via resend failed: %s %s' % (e, detail))
|
|
373
|
+
return False
|
|
374
|
+
except Exception as e:
|
|
375
|
+
log(cfg, 'mail via resend failed: %s' % e)
|
|
376
|
+
return False
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def _send_smtp(cfg, subject, text):
|
|
380
|
+
try:
|
|
381
|
+
pw = cfg.smtp_password_file.read_text().strip()
|
|
382
|
+
except OSError:
|
|
383
|
+
log(cfg, 'mail skipped: no SMTP password at %s' % cfg.smtp_password_file)
|
|
384
|
+
return False
|
|
385
|
+
msg = EmailMessage()
|
|
386
|
+
msg['Subject'] = subject
|
|
387
|
+
msg['From'] = cfg.mail_from
|
|
388
|
+
msg['To'] = cfg.mail_to
|
|
389
|
+
msg.set_content(text)
|
|
390
|
+
try:
|
|
391
|
+
with smtplib.SMTP(cfg.smtp_host, cfg.smtp_port, timeout=20) as s:
|
|
392
|
+
s.starttls()
|
|
393
|
+
s.login(cfg.smtp_user or cfg.mail_from, pw)
|
|
394
|
+
s.send_message(msg)
|
|
395
|
+
log(cfg, 'mail via smtp ok')
|
|
396
|
+
return True
|
|
397
|
+
except Exception as e:
|
|
398
|
+
log(cfg, 'mail via smtp failed: %s' % e)
|
|
399
|
+
return False
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _post_json(url, payload, headers=None):
|
|
403
|
+
body = json.dumps(payload).encode()
|
|
404
|
+
h = {'Content-Type': 'application/json',
|
|
405
|
+
'User-Agent': 'ladok-vakt/0.1 (+https://github.com/vhsgreed/ladok-vakt)'}
|
|
406
|
+
h.update(headers or {})
|
|
407
|
+
req = urllib.request.Request(url, body, h)
|
|
408
|
+
with urllib.request.urlopen(req, timeout=20) as r:
|
|
409
|
+
return 200 <= r.status < 300
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def send_webhook(cfg, text):
|
|
413
|
+
if not cfg.webhook_url:
|
|
414
|
+
return None
|
|
415
|
+
fmt = cfg.webhook_format.lower()
|
|
416
|
+
if fmt == 'discord':
|
|
417
|
+
payload = {'content': text}
|
|
418
|
+
elif fmt == 'ntfy':
|
|
419
|
+
payload = {'topic': cfg.webhook_url.rstrip('/').split('/')[-1], 'message': text}
|
|
420
|
+
try:
|
|
421
|
+
with urllib.request.urlopen(urllib.request.Request(
|
|
422
|
+
cfg.webhook_url, text.encode(), {'User-Agent': 'ladok-vakt/0.1'},
|
|
423
|
+
method='POST'), timeout=20) as r:
|
|
424
|
+
ok = 200 <= r.status < 300
|
|
425
|
+
log(cfg, 'webhook ntfy ok=%s' % ok)
|
|
426
|
+
return ok
|
|
427
|
+
except Exception as e:
|
|
428
|
+
log(cfg, 'webhook ntfy failed: %s' % e)
|
|
429
|
+
return False
|
|
430
|
+
elif fmt == 'generic':
|
|
431
|
+
payload = {'text': text, 'source': 'ladok-vakt'}
|
|
432
|
+
else:
|
|
433
|
+
payload = {'text': text}
|
|
434
|
+
try:
|
|
435
|
+
ok = _post_json(cfg.webhook_url, payload)
|
|
436
|
+
log(cfg, 'webhook %s ok=%s' % (fmt, ok))
|
|
437
|
+
return ok
|
|
438
|
+
except Exception as e:
|
|
439
|
+
log(cfg, 'webhook %s failed: %s' % (fmt, e))
|
|
440
|
+
return False
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def send_telegram(cfg, text):
|
|
444
|
+
if not (cfg.telegram_token and cfg.telegram_chat):
|
|
445
|
+
return None
|
|
446
|
+
url = 'https://api.telegram.org/bot%s/sendMessage' % cfg.telegram_token
|
|
447
|
+
try:
|
|
448
|
+
ok = _post_json(url, {'chat_id': cfg.telegram_chat, 'text': text})
|
|
449
|
+
log(cfg, 'telegram ok=%s' % ok)
|
|
450
|
+
return ok
|
|
451
|
+
except Exception as e:
|
|
452
|
+
log(cfg, 'telegram failed: %s' % e)
|
|
453
|
+
return False
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def send_command(cfg, subject, text):
|
|
457
|
+
"""Escape hatch: pipe the message into any command (ntfy, curl, notify-send)."""
|
|
458
|
+
if not cfg.notify_command:
|
|
459
|
+
return None
|
|
460
|
+
import subprocess
|
|
461
|
+
try:
|
|
462
|
+
p = subprocess.run(cfg.notify_command, shell=True, input=subject + '\n' + text,
|
|
463
|
+
text=True, timeout=30, capture_output=True)
|
|
464
|
+
log(cfg, 'command ok=%s rc=%s' % (p.returncode == 0, p.returncode))
|
|
465
|
+
return p.returncode == 0
|
|
466
|
+
except Exception as e:
|
|
467
|
+
log(cfg, 'command failed: %s' % e)
|
|
468
|
+
return False
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def notify(cfg, subject, text):
|
|
472
|
+
"""Fault notice out over every configured channel. Returns channels that worked."""
|
|
473
|
+
done = []
|
|
474
|
+
if send_mail(cfg, subject, text):
|
|
475
|
+
done.append('mail')
|
|
476
|
+
for name, fn in (('telegram', lambda: send_telegram(cfg, subject + '\n\n' + text)),
|
|
477
|
+
('webhook', lambda: send_webhook(cfg, subject + '\n\n' + text)),
|
|
478
|
+
('command', lambda: send_command(cfg, subject, text))):
|
|
479
|
+
try:
|
|
480
|
+
if fn():
|
|
481
|
+
done.append(name)
|
|
482
|
+
except Exception as e:
|
|
483
|
+
log(cfg, '%s failed: %s' % (name, e))
|
|
484
|
+
if not done:
|
|
485
|
+
log(cfg, 'no channel delivered the notice')
|
|
486
|
+
return done
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def mail_allowed(cfg, state, kind):
|
|
490
|
+
last = (state.get('mail') or {}).get(kind)
|
|
491
|
+
if not last:
|
|
492
|
+
return True
|
|
493
|
+
try:
|
|
494
|
+
age = (dt.datetime.now() - dt.datetime.fromisoformat(last)).total_seconds() / 3600
|
|
495
|
+
except ValueError:
|
|
496
|
+
return True
|
|
497
|
+
return age >= cfg.mail_min_hours
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
# --------------------------------------------------------------------------
|
|
501
|
+
# One run
|
|
502
|
+
# --------------------------------------------------------------------------
|
|
503
|
+
|
|
504
|
+
def run(cfg, alert=True, email=True, write_files=True):
|
|
505
|
+
"""Do one check. Returns the lines that should be sent to the human."""
|
|
506
|
+
prev = load_state(cfg)
|
|
507
|
+
try:
|
|
508
|
+
try:
|
|
509
|
+
uid, reg_xml, exam_xml = fetch(cfg)
|
|
510
|
+
except Fault:
|
|
511
|
+
if not cfg.autologin:
|
|
512
|
+
raise
|
|
513
|
+
from .login import login # late import: the module is optional
|
|
514
|
+
log(cfg, 'session gone, trying the saved login')
|
|
515
|
+
login(cfg)
|
|
516
|
+
uid, reg_xml, exam_xml = fetch(cfg)
|
|
517
|
+
rows = pending_actions(parse_registrations(reg_xml) + parse_exams(exam_xml))
|
|
518
|
+
except Fault as exc:
|
|
519
|
+
note = '- VARNING: ladok-vakt kunde inte läsa Ladok: %s' % exc
|
|
520
|
+
log(cfg, 'fault: %s' % exc)
|
|
521
|
+
out = [note]
|
|
522
|
+
if email and mail_allowed(cfg, prev, 'fault'):
|
|
523
|
+
if notify(cfg, 'Ladok-vakt: hämtningen fungerar inte', note + '\n'):
|
|
524
|
+
st = load_state(cfg)
|
|
525
|
+
st.setdefault('mail', {})['fault'] = dt.datetime.now().isoformat(timespec='seconds')
|
|
526
|
+
save_state(cfg, st)
|
|
527
|
+
return out
|
|
528
|
+
|
|
529
|
+
save_state(cfg, {'last_success': dt.datetime.now().isoformat(timespec='seconds'),
|
|
530
|
+
'student': uid, 'windows': len(rows), 'mail': prev.get('mail', {})})
|
|
531
|
+
log(cfg, 'ok windows=%d' % len(rows))
|
|
532
|
+
if write_files:
|
|
533
|
+
cfg.state_dir.mkdir(parents=True, exist_ok=True)
|
|
534
|
+
|
|
535
|
+
hot = urgent(rows, cfg.close_days)
|
|
536
|
+
stale = stale_note(cfg, prev)
|
|
537
|
+
heartbeat = None
|
|
538
|
+
if cfg.monday_heartbeat and dt.date.today().weekday() == 0 and not hot:
|
|
539
|
+
heartbeat = ('- Veckokoll ladok-vakt %s: körningen gick igenom, %d anmälningsfönster, '
|
|
540
|
+
'inget inom %d dagar.' % (dt.date.today().isoformat(), len(rows), cfg.close_days))
|
|
541
|
+
|
|
542
|
+
if not alert:
|
|
543
|
+
return describe(rows)
|
|
544
|
+
|
|
545
|
+
out = [x for x in (stale, heartbeat) if x]
|
|
546
|
+
if hot:
|
|
547
|
+
out += describe(hot)
|
|
548
|
+
if stale and email and mail_allowed(cfg, prev, 'stale'):
|
|
549
|
+
if notify(cfg, 'Ladok-vakt: hämtningen har stannat', stale + '\n'):
|
|
550
|
+
st = load_state(cfg)
|
|
551
|
+
st.setdefault('mail', {})['stale'] = dt.datetime.now().isoformat(timespec='seconds')
|
|
552
|
+
save_state(cfg, st)
|
|
553
|
+
return out
|