From f9d33f7b4ef6090f7d48f52b06f411f720923596 Mon Sep 17 00:00:00 2001 From: edpibu Date: Sun, 20 Sep 2020 22:43:25 +0200 Subject: [PATCH] Sending events --- parser.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/parser.py b/parser.py index 109570d..8bb2682 100755 --- a/parser.py +++ b/parser.py @@ -26,6 +26,7 @@ TIMETABLE = [ ('17:45','17:45'), ] DAVURL = "https://cal.edgarpierre.fr" +CALURL = "https://cal.edgarpierre.fr/edpibu/817427eb-be7c-4540-872a-dfb723a205a6/" class GoogleSheetsCalParser(HTMLParser): @@ -40,8 +41,17 @@ class GoogleSheetsCalParser(HTMLParser): self.row = -1 self.column = -1 self.rowspan = 0 - self.date = 0 - self.calendar = Calendar() + self.date = {} + + import priv + + client = caldav.DAVClient(url=DAVURL, username=priv.USERNAME, + password=priv.PASSWORD) + self.calendar = caldav.Calendar(client=client, url=CALURL) + + for ev in self.calendar.events(): + ev.delete() + HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): @@ -75,19 +85,25 @@ class GoogleSheetsCalParser(HTMLParser): def handle_data(self, data): if self.inDate: - self.date = dt.strptime(data, '%d-%b-%y').date() + self.date[self.column] = dt.strptime(data, '%d-%b-%y').date() elif self.inCell and data not in ['', '-']: 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') event = Event() + event.add('uid', f'{self.row}x{self.column}') event.add('summary', data) - event.add('dtstart', dt.combine(self.date, times[0], + event.add('dtstamp', dt.now()) + event.add('dtstart', dt.combine(self.date[self.column], times[0], pytz.timezone('Europe/Paris'))) - event.add('dtend', dt.combine(self.date, times[1], + event.add('dtend', dt.combine(self.date[self.column], times[1], pytz.timezone('Europe/Paris'))) + cal.add_component(event) - self.calendar.add_component(event) + self.calendar.save_event(cal.to_ical().decode('utf-8')) # Getting the Google Sheet @@ -104,12 +120,3 @@ if r.status_code != 200: # Parsing the Sheet calParser = GoogleSheetsCalParser() calParser.feed(r.text) - -# Pushing events to caldav server -import priv - -client = caldav.DAVClient(url=DAVURL, username=priv.USERNAME, - password=priv.PASSWORD) -pr = client.principal() -calendars = pr.calendars() -print(calendars)