-
Notifications
You must be signed in to change notification settings - Fork 17
/
fetch-next-koma.py
executable file
·73 lines (47 loc) · 1.63 KB
/
fetch-next-koma.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
#!/usr/bin/env nix-shell
#! nix-shell -i python -p python3
import json
from datetime import date
from urllib.request import urlopen, Request
QUERY = f"""
https://de.komapedia.org/api.php?origin=*&action=ask&format=json&query=[[Category:KoMa]][[ende::%3E{date.today().isoformat()}]]|sort=KoMaNr|order=asc|limit=5|?Ort|?Beginn|?Ende|?Anmeldung|?Infoseite
"""
LINKS = ["Anmeldung", "Infoseite"]
IGNORED_STATUS_CODES = [401]
def get_next_komata():
results = json.loads(urlopen(QUERY).read().decode())
return results["query"]["results"]
def verify_url(url):
try:
response = urlopen(Request(url, method="HEAD"))
except:
return False
else:
return response.status in [200] + IGNORED_STATUS_CODES
def clean_and_verify_printounts(printouts):
result = printouts
for key, value in printouts.items():
if key in LINKS:
result[key] = []
for url in value:
if verify_url(url):
result[key].append(url)
return result
def clean_and_verify_koma(koma):
result = koma
for key, value in koma.items():
if key == "printouts":
result[key] = clean_and_verify_printounts(value)
return result
def clean_and_verify(komata):
verified = {}
for koma in komata:
verified[koma] = clean_and_verify_koma(komata[koma])
return json.dumps(verified, indent=2, sort_keys=True)
def main():
next_komata = get_next_komata()
cleaned_and_verified = clean_and_verify(next_komata)
with open("next-koma.json", mode="w") as file:
file.write(cleaned_and_verified)
if __name__ == "__main__":
main()