Update calendar rather than delete all events

modifié :         parser.py
This commit is contained in:
Edgar P. Burkhart 2020-10-03 17:43:46 +02:00
parent 2766866f95
commit 0f19dc902d
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -39,6 +39,7 @@ class GoogleSheetsCalParser(HTMLParser):
self.inTable = False
self.inCell = False
self.inDate = False
self.inEvent = False
self.row = -1
self.column = -1
self.nextCol = -1
@ -54,8 +55,9 @@ class GoogleSheetsCalParser(HTMLParser):
password=priv.PASSWORD)
self.calendar = caldav.Calendar(client=client, url=CALURL)
for ev in progressbar(self.calendar.events()):
ev.delete()
#print(self.calendar.event_by_uid('42x26').data)
#for ev in progressbar(self.calendar.events()):
# print(ev.url, ev.canonical_url())
HTMLParser.__init__(self)
@ -79,8 +81,7 @@ class GoogleSheetsCalParser(HTMLParser):
self.inCell = True
if 'rowspan' in dAttrs.keys():
self.rowspan = int(dAttrs['rowspan'])
self.multirows[self.column] = [self.nextCol -
self.column, self.rowspan]
self.multirows[self.column] = [self.nextCol - self.column, self.rowspan]
else: self.rowspan = 1
elif (self.row - 2) % 12 == 1:
self.inDate = True
@ -91,6 +92,32 @@ class GoogleSheetsCalParser(HTMLParser):
if tag == 'td':
self.inCell = False
self.inDate = False
if self.inEvent:
self.inEvent = False
cal = Calendar()
cal.add('prodid', '-//edpibu//edt-parser//FR')
cal.add('version', '2.0')
cal.add_component(self.event)
try:
print(self.event['uid'])
ev = self.calendar.event_by_uid(self.event['uid'])
except caldav.lib.error.NotFoundError:
ev = None
if ev:
icev = Calendar.from_ical(ev.data)
ex = True
for key in ['summary', 'dtstart', 'dtend']:
if icev.subcomponents[1][key] != self.event[key]:
ex = False
if ex: return
print(f'Change on {self.event["dtstart"]}')
ev.data = cal.to_ical().decode('utf-8')
ev.save()
return
print(f'New event on {self.event["dtstart"]}')
self.calendar.save_event(cal.to_ical().decode('utf-8'))
elif tag == 'tr':
for mr in self.multirows.values():
mr[1] -= 1
@ -101,12 +128,10 @@ class GoogleSheetsCalParser(HTMLParser):
if self.inDate:
self.date[self.column] = dt.strptime(data, '%d-%b-%y').date()
elif self.inCell and data not in ['', '-']:
self.inEvent = True
times = [time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2][1]),
time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2 + self.rowspan][0])]
cal = Calendar()
cal.add('prodid', '-//edpibu//edt-parser//FR')
cal.add('version', '2.0')
if self.event['uid'] == f'{self.row}x{self.column}':
self.event['summary'] = f'{self.event["summary"]} {data}'
else:
@ -118,9 +143,6 @@ class GoogleSheetsCalParser(HTMLParser):
pytz.timezone('Europe/Paris')))
self.event.add('dtend', dt.combine(self.date[self.column], times[1],
pytz.timezone('Europe/Paris')))
cal.add_component(self.event)
self.calendar.save_event(cal.to_ical().decode('utf-8'))
# Getting the Google Sheet