Add initial script
This commit is contained in:
commit
fd1ef08486
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
calendars
|
||||||
|
config.ini
|
4
config.ini.example
Normal file
4
config.ini.example
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
[address]
|
||||||
|
city=Nürnberg
|
||||||
|
street=Am Pointgraben
|
||||||
|
house_number=7
|
103
generate_ical.py
Normal file
103
generate_ical.py
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
import configparser
|
||||||
|
import icalendar
|
||||||
|
import requests
|
||||||
|
import urllib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
def read_config():
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('config.ini')
|
||||||
|
return config
|
||||||
|
|
||||||
|
config = read_config()
|
||||||
|
|
||||||
|
# request locations supportet by the endpoint
|
||||||
|
# https://nuernberg-abfallapp.regioit.de/abfall-app-nuernberg/rest/orte
|
||||||
|
# Returns:
|
||||||
|
# [{"id":2049161,"name":"Nürnberg"}]
|
||||||
|
req = requests.get("https://nuernberg-abfallapp.regioit.de/abfall-app-nuernberg/rest/orte")
|
||||||
|
locations = req.json()
|
||||||
|
for location in locations:
|
||||||
|
if location.get("name") == config["address"]["city"]:
|
||||||
|
nbg_id = location.get("id")
|
||||||
|
|
||||||
|
# request all known streets for a given location id (can be obtained by previous request)
|
||||||
|
req = requests.get("https://nuernberg-abfallapp.regioit.de/abfall-app-nuernberg/rest/orte/{location_id}/strassen".format(location_id=nbg_id))
|
||||||
|
streets = req.json()
|
||||||
|
|
||||||
|
for street in streets:
|
||||||
|
if street.get("name") == config["address"]["street"]:
|
||||||
|
street_id = street.get("id")
|
||||||
|
|
||||||
|
# request all known house numbers for a given street id (can be obtained by previous request)
|
||||||
|
req = requests.get("https://nuernberg-abfallapp.regioit.de/abfall-app-nuernberg/rest/strassen/{street_id}".format(street_id=street_id))
|
||||||
|
housenumbers = req.json().get("hausNrList", [])
|
||||||
|
|
||||||
|
for number in housenumbers:
|
||||||
|
if number.get("nr") == config["address"]["house_number"]:
|
||||||
|
housenumber_id = number.get("id")
|
||||||
|
|
||||||
|
# request trash types picked up at that location
|
||||||
|
req = requests.get("https://nuernberg-abfallapp.regioit.de/abfall-app-nuernberg/rest/hausnummern/{housenumber_id}/fraktionen".format(housenumber_id=housenumber_id))
|
||||||
|
trash_types = req.json()
|
||||||
|
|
||||||
|
for trash_type in trash_types:
|
||||||
|
print(trash_type.get("name"))
|
||||||
|
|
||||||
|
#requested_trash_types = list(map(lambda x: str(x.get("id")), trash_types))
|
||||||
|
|
||||||
|
query = {
|
||||||
|
"format": ["ics"],
|
||||||
|
"jahr": ["2019"],
|
||||||
|
"ort": ["Nürnberg"],
|
||||||
|
"strasse": [street_id],
|
||||||
|
"hnr": [housenumber_id],
|
||||||
|
"fraktion": []
|
||||||
|
}
|
||||||
|
|
||||||
|
radicale_props = {
|
||||||
|
"C:supported-calendar-component-set": "VEVENT",
|
||||||
|
"D:displayname": None,
|
||||||
|
"ICAL:calendar-color": "#000000ff",
|
||||||
|
"tag": "VCALENDAR"
|
||||||
|
}
|
||||||
|
|
||||||
|
processed_cals = os.path.join(".", "calendars")
|
||||||
|
if not os.path.exists(processed_cals):
|
||||||
|
os.makedirs(processed_cals)
|
||||||
|
|
||||||
|
for trash_type in trash_types:
|
||||||
|
radicale_props["D:displayname"] = trash_type.get("name")
|
||||||
|
radicale_props["ICAL:calendar-color"] = "#{}ff".format(trash_type.get("farbeRgb", "000000"))
|
||||||
|
folder_name = trash_type.get("name").replace("/", "_")
|
||||||
|
trash_type_folder = os.path.join(processed_cals, folder_name)
|
||||||
|
if not os.path.exists(trash_type_folder):
|
||||||
|
os.makedirs(trash_type_folder)
|
||||||
|
|
||||||
|
radicale_prop_file = os.path.join(trash_type_folder, ".Radicale.props")
|
||||||
|
with open(radicale_prop_file, "w") as f:
|
||||||
|
f.write(json.dumps(radicale_props))
|
||||||
|
|
||||||
|
query["fraktion"] = [str(trash_type["id"])]
|
||||||
|
query_string = urllib.parse.urlencode(query, doseq=True)
|
||||||
|
query_url = urllib.parse.urlunparse(("https", "abfallkalender.regioit.de", "/kalender-nuernberg/downloadfile.jsp", "", query_string, ""))
|
||||||
|
req = requests.get(query_url)
|
||||||
|
ical = req.text
|
||||||
|
|
||||||
|
cal = icalendar.Calendar.from_ical(ical)
|
||||||
|
for event in cal.walk():
|
||||||
|
if isinstance(event, icalendar.cal.Event):
|
||||||
|
uid = event.get("UID")
|
||||||
|
new_cal = icalendar.Calendar()
|
||||||
|
new_cal.add('prodid', 'Trash dates')
|
||||||
|
new_cal.add('version', '2.0')
|
||||||
|
new_cal.add_component(event)
|
||||||
|
ical_name = "{}.ical".format(uid)
|
||||||
|
ical_path = os.path.join(trash_type_folder, ical_name)
|
||||||
|
with open(ical_path, "wb") as single_file:
|
||||||
|
single_file.write(new_cal.to_ical())
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("Skipping type {}".format(type(event)))
|
||||||
|
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
icalendar==4.0.7
|
||||||
|
requests==2.25.1
|
Loading…
Reference in a new issue