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 -