-
Notifications
You must be signed in to change notification settings - Fork 1
/
plenum.py
executable file
·74 lines (56 loc) · 1.66 KB
/
plenum.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
#!/usr/bin/env python3
"""
Plenum-Reminder, by Kunsi
To be executed by a cronjob every day at 00:01
Checks wether a Plenum is scheduled for the next day, if yes, it
sends out a mail notification to the intern mailing list.
"""
from datetime import date, timedelta
from locale import setlocale, LC_ALL
from os import environ
from sys import argv, exit
from email.mime.text import MIMEText
from requests import get
from smtplib import SMTP
URL = argv[1]
DEBUG = environ.get("DEBUG", "0") == "1"
DAYS = int(environ.get("DELTA_DAYS", 1))
tomorrow = date.today() + timedelta(days=DAYS)
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return
setlocale(LC_ALL, "de_DE.UTF-8")
wiki = get(URL).content.decode("utf-8")
plenum_tops = None
for date_format in ("%Y-%m-%d", "%d.%m.%Y"):
start = "{} ===".format(tomorrow.strftime(date_format))
plenum_tops = find_between(wiki, start, "=== ")
if plenum_tops:
break
else:
# Catch a corner case for the first plenum on a page
plenum_tops = find_between(wiki, start, "</textarea>")
if plenum_tops:
template = """Hallo,
morgen ist (laut Wiki) wieder mal Plenum. Nachfolgend die Tagesordnungs-
punkte aus dem Wiki:
{}""".format(
plenum_tops.strip()
)
if DEBUG:
print(template)
exit(0)
msg = MIMEText(template)
msg["Subject"] = "Plenum am %s" % tomorrow.strftime("%A, %d.%m.%Y")
msg["From"] = argv[2]
msg["To"] = argv[3]
smtpObj = SMTP("localhost")
smtpObj.send_message(msg)
smtpObj.quit()
elif DEBUG:
print(wiki)
exit(1)