Calendar creation

This commit is contained in:
Edgar P. Burkhart 2020-09-20 22:11:47 +02:00
parent b71c295eee
commit a2a803d77c
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -4,8 +4,10 @@
import sys import sys
import requests import requests
from html.parser import HTMLParser from html.parser import HTMLParser
from datetime import datetime as dt from datetime import datetime as dt, time
import caldav, icalendar import caldav
from icalendar import Calendar, Event
import pytz
# Constants definition # Constants definition
@ -38,6 +40,7 @@ class GoogleSheetsCalParser(HTMLParser):
self.column = -1 self.column = -1
self.rowspan = 0 self.rowspan = 0
self.date = 0 self.date = 0
self.calendar = Calendar()
HTMLParser.__init__(self) HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs): def handle_starttag(self, tag, attrs):
@ -71,11 +74,19 @@ class GoogleSheetsCalParser(HTMLParser):
def handle_data(self, data): def handle_data(self, data):
if self.inDate: 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 ['', '-']: elif self.inCell and data not in ['', '-']:
times = [TIMETABLE[(self.row - 2) % 12 - 2][1], times = [time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2][1]),
TIMETABLE[(self.row - 2) % 12 - 2 + self.rowspan][0]] time.fromisoformat(TIMETABLE[(self.row - 2) % 12 - 2 + self.rowspan][0])]
print(data, self.date, times)
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 # Getting the Google Sheet
@ -93,8 +104,4 @@ if r.status_code != 200:
calParser = GoogleSheetsCalParser() calParser = GoogleSheetsCalParser()
calParser.feed(r.text) calParser.feed(r.text)
# Transforming the cells into events
# Pushing events to caldav server # Pushing events to caldav server