From a2a803d77c9308e8d42fbc5dda2cc105cf0aa4bc Mon Sep 17 00:00:00 2001 From: edpibu Date: Sun, 20 Sep 2020 22:11:47 +0200 Subject: [PATCH] Calendar creation --- parser.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/parser.py b/parser.py index 256bbd7..fa36ffd 100755 --- a/parser.py +++ b/parser.py @@ -4,8 +4,10 @@ import sys import requests from html.parser import HTMLParser -from datetime import datetime as dt -import caldav, icalendar +from datetime import datetime as dt, time +import caldav +from icalendar import Calendar, Event +import pytz # Constants definition @@ -38,6 +40,7 @@ class GoogleSheetsCalParser(HTMLParser): self.column = -1 self.rowspan = 0 self.date = 0 + self.calendar = Calendar() HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): @@ -71,11 +74,19 @@ class GoogleSheetsCalParser(HTMLParser): def handle_data(self, data): if self.inDate: - self.date = dt.strptime(data, '%d-%b-%y') + self.date = dt.strptime(data, '%d-%b-%y').date() elif self.inCell and data not in ['', '-']: - times = [TIMETABLE[(self.row - 2) % 12 - 2][1], - TIMETABLE[(self.row - 2) % 12 - 2 + self.rowspan][0]] - print(data, self.date, times) + times = [time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2][1]), + time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2 + self.rowspan][0])] + + event = Event() + event.add('summary', data) + event.add('dtstart', dt.combine(self.date, times[0], + pytz.timezone('Europe/Paris'))) + event.add('dtend', dt.combine(self.date, times[1], + pytz.timezone('Europe/Paris'))) + + self.calendar.add_component(event) # Getting the Google Sheet @@ -93,8 +104,4 @@ if r.status_code != 200: calParser = GoogleSheetsCalParser() calParser.feed(r.text) -# Transforming the cells into events - - # Pushing events to caldav server -