-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathical-archiver.py
executable file
·60 lines (52 loc) · 2.08 KB
/
ical-archiver.py
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
#!/usr/bin/env python3
from io import BufferedReader, BufferedWriter
from datetime import datetime
from icalendar import Calendar
from argparse import ArgumentParser, FileType
parser = ArgumentParser()
parser.add_argument('inputFile', nargs=1, type=FileType('rb'),
help='''Input ICS file to read events from''')
parser.add_argument('-n', '--newFile', nargs=1, type=FileType('wb'),
help='''if this parameters is provided, the archived items are not removed from the inputFile but
added to a new file. Provide the file name using --newFile''')
parser.add_argument('archiveFile', nargs=1, type=FileType('wb'),
help='''Archive ICS file to output archived events into.''')
parser.add_argument('date', nargs=1, type=lambda s: datetime.strptime(s, '%Y-%m-%d'),
help='''date in the format "Y-m-d". All events before this date are moved into the archive file.
Example: 2019-12-31''')
args = parser.parse_args()
inputFile: BufferedReader = args.inputFile[0]
maxDate: datetime = args.date[0]
currentCal: Calendar = Calendar.from_ical(inputFile.read())
inputFile.close()
archiveCal = Calendar()
archiveCal.add('prodid', 'ical-archiver')
count = 0
kept = 0
for event in currentCal.subcomponents:
if event.name != 'VEVENT':
archiveCal.add_component(event)
continue
dtend = event.get('dtend')
if not hasattr(dtend, 'dt'):
continue
dtendDt = datetime.combine(dtend.dt, datetime.min.time())
if dtendDt < maxDate:
archiveCal.add_component(event)
count += 1
else:
kept += 1
for event in archiveCal.subcomponents:
if event.name != 'VEVENT':
continue
currentCal.subcomponents.remove(event)
try:
newFile = args.newFile[0]
except TypeError:
newFile = open(args.inputFile[0].name, 'wb')
newFile.write(currentCal.to_ical(False))
newFile.close()
archiveFile: BufferedWriter = args.archiveFile[0]
archiveFile.write(archiveCal.to_ical(False))
archiveFile.close()
print(str(count) + " events archived, " + str(kept) + ' events kept')