From 83c1fe95b1bbec524d764d13c700247aaf26c9f4 Mon Sep 17 00:00:00 2001 From: sqozz Date: Thu, 7 Oct 2021 12:10:03 +0200 Subject: [PATCH] Add automatic cleanup for previously generated files Unfortunately the UIDs of the downloaded icals change. To still catch changes in the trash date schedule over the year, this makes sure to delete all events with the same year as we're currently scraping. --- generate_ical.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/generate_ical.py b/generate_ical.py index c61a024..8f4372f 100644 --- a/generate_ical.py +++ b/generate_ical.py @@ -77,6 +77,17 @@ for trash_type in trash_types: if not os.path.exists(trash_type_folder): os.makedirs(trash_type_folder) + for f in next(os.walk(trash_type_folder), (None, None, []))[2]: + ical_path = os.path.join(trash_type_folder, f) + with open(ical_path, "rb") as fh: + try: + cal = icalendar.Calendar.from_ical(fh.read()) + except ValueError: + print("{} is not an parsable ical file".format(ical_path)) + continue + if cal.walk()[1]["DTSTAMP"].dt.year == current_year: + os.remove(ical_path) + 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)) @@ -95,7 +106,7 @@ for trash_type in trash_types: new_cal.add('prodid', 'Trash dates') new_cal.add('version', '2.0') new_cal.add_component(event) - ical_name = "{}.ical".format(uid) + ical_name = "{}_{}.ical".format(current_year, 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())