From 0f19dc902dfff2546e79bd9970d24dac4ae5212f Mon Sep 17 00:00:00 2001
From: edpibu <ed.pi.burkhart@gmail.com>
Date: Sat, 3 Oct 2020 17:43:46 +0200
Subject: [PATCH] Update calendar rather than delete all events
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

	modifié :         parser.py
---
 parser.py | 42 ++++++++++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 10 deletions(-)

diff --git a/parser.py b/parser.py
index 3b1cb95..ebad0a9 100755
--- a/parser.py
+++ b/parser.py
@@ -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
