html5-infobeamer-dhcp/download.py

64 lines
2.0 KiB
Python
Raw Normal View History

2024-10-30 21:43:44 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2024-10-30 22:08:56 +01:00
"""
Script to Download Schedule
and rename rooms
"""
from pathlib import Path
2024-10-30 21:43:44 +01:00
import json
2024-10-30 22:08:56 +01:00
import requests
2024-10-30 21:43:44 +01:00
2024-10-30 22:08:56 +01:00
URL = "https://talks.dhcp.cfhn.it/dhcp-2024/schedule/export/schedule.json"
response = requests.get(URL, timeout=23)
2024-10-30 21:43:44 +01:00
if response.status_code == 200:
schedule_data = response.json() # JSON-Daten laden
2024-10-30 22:08:56 +01:00
# pylint: disable=too-many-branches
2024-10-30 21:43:44 +01:00
def replace_room_names(data):
2024-10-30 22:08:56 +01:00
"""
Funktion zum Umbenennen der Namen
"""
2024-10-30 21:43:44 +01:00
if isinstance(data, dict):
2024-10-30 22:08:56 +01:00
# edit room value
keys_to_replace = {}
2024-10-30 21:43:44 +01:00
for key, value in data.items():
if value == "D002 Vortragsraum":
data[key] = "D002"
elif value == "Haupteingang":
data[key] = "Eingang"
elif value == "A017 Workshopraum":
data[key] = "A017"
else:
replace_room_names(value)
2024-10-30 22:08:56 +01:00
# edit room key
new_key = key
if key == "D002 Vortragsraum":
new_key = "D002"
elif key == "Haupteingang":
new_key = "Eingang"
elif key == "A017 Workshopraum":
new_key = "A017"
# and apply changes if needed
if new_key != key:
keys_to_replace[key] = new_key
for old_key, new_key in keys_to_replace.items():
data[new_key] = data.pop(old_key)
2024-10-30 21:43:44 +01:00
elif isinstance(data, list):
for item in data:
replace_room_names(item)
replace_room_names(schedule_data)
2024-10-30 22:08:56 +01:00
# save JSON
OUTPUT_PATH = Path("static/schedule.json")
OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
with OUTPUT_PATH.open("w", encoding="utf-8") as f:
2024-10-30 21:43:44 +01:00
json.dump(schedule_data, f, ensure_ascii=False, indent=4)
print("Die Datei wurde erfolgreich unter 'static/schedule.json' gespeichert.")