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.
This commit is contained in:
sqozz 2021-10-07 12:10:03 +02:00
parent 2771d67f74
commit 83c1fe95b1
1 changed files with 12 additions and 1 deletions

View File

@ -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())