#!/usr/bin/env python #-*- encoding: utf-8 import sys import requests from html.parser import HTMLParser from datetime import datetime as dt import caldav, icalendar # Constants definition URL = "https://docs.google.com/spreadsheets/u/0/d/e/2PACX-1vQ9yzFLr5mXbIZVK3ucdUZuScAbLoCyPqzHr-5V0aYeCFEz7LuidPdk_EnkkJT-zjemzQQHaKvpeXW2/pubhtml/sheet?headers=false&gid=1619638924" TIMETABLE = [ ('08:00','08:00'), ('09:00','09:00'), ('10:00','10:15'), ('11:15','11:15'), ('12:15','12:15'), ('13:00','13:00'), ('13:30','13:30'), ('14:30','14:30'), ('15:30','15:45'), ('16:45','16:45'), ('17:45','17:45'), ] class GoogleSheetsCalParser(HTMLParser): """ Definition of a Google Sheets parser providing a table with each cell and its position and size """ def __init__(self): self.inTable = False self.inCell = False self.inDate = False self.row = -1 self.column = -1 self.rowspan = 0 self.date = 0 HTMLParser.__init__(self) def handle_starttag(self, tag, attrs): dAttrs = dict(attrs) if self.inTable: if tag == 'tr': self.row += 1 self.column = -1 elif tag == 'td': if 'colspan' in dAttrs.keys(): self.column += int(dAttrs['colspan']) else: self.column += 1 if not (self.row < 3 or (self.row - 2) % 12 < 2): self.inCell = True if 'rowspan' in dAttrs.keys(): self.rowspan = dAttrs['rowspan'] else: self.rowspan = 1 elif (self.row - 2) % 12 == 1: self.inDate = True elif tag == 'tbody': self.inTable = True def handle_endtag(self, tag): if tag == 'td': self.inCell = False self.inDate = False elif tag == 'tbody': self.inTable = False def handle_data(self, data): if self.inDate: self.date = dt.strptime(data, '%d-%b-%y') print(self.date) elif self.inCell and data not in ['', '-']: print(data, self.row, self.column, self.rowspan) # Getting the Google Sheet try: r = requests.get(URL) except ConnectionError as e: print(e) sys.exit(1) if r.status_code != 200: print(f'Status Code {r.status_code}; could not continue.') sys.exit(1) # Parsing the Sheet calParser = GoogleSheetsCalParser() calParser.feed(r.text) # Transforming the cells into events # Pushing events to caldav server