-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_changes.py
More file actions
72 lines (49 loc) · 1.63 KB
/
test_changes.py
File metadata and controls
72 lines (49 loc) · 1.63 KB
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
import json
import requests
from requests import HTTPError
def load_data():
with open("/tmp/jedeschule-changes.json") as infile:
return json.load(infile)
def fetch_data(school_id):
response = requests.get(f"https://jedeschule.codefor.de/schools/{school_id}")
response.raise_for_status()
return response.json()
def get_clean_item(data):
return {
key: value
for key, value in data.items()
if value is not None and key != "update_timestamp"
}
def dict_diff(dict1, dict2):
all_keys = set(dict1.keys()) | set(dict2.keys())
differences = {}
for key in all_keys:
val1 = dict1.get(key, None)
val2 = dict2.get(key, None)
if val1 != val2:
differences[key] = (val1, val2)
return differences
def compare_schools(new_school, old_school):
new_values = get_clean_item(new_school)
old_values = get_clean_item(old_school)
differences = dict_diff(new_values, old_values)
if len(differences) == 0:
print(" no changes \n")
return
print(f"Difference found:")
print(json.dumps(differences, indent=2))
def main():
data = load_data()
for school in data[:10]:
school_id = school.get("info").get("id")
print()
print("#" * 10, f"Comparing {school_id}")
upstream_data = {}
try:
upstream_data = fetch_data(school_id)
except HTTPError as e:
print(f"WARN: Could not fetch old data for school-id {school_id}: {e}")
print()
compare_schools(school.get("info"), upstream_data)
if __name__ == "__main__":
main()