2020-09-20 20:53:24 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#-*- encoding: utf-8
|
|
|
|
|
2020-09-20 21:11:48 +02:00
|
|
|
import sys
|
2020-09-20 20:53:24 +02:00
|
|
|
import requests
|
|
|
|
from html.parser import HTMLParser
|
2020-09-20 22:11:47 +02:00
|
|
|
from datetime import datetime as dt, time
|
|
|
|
import caldav
|
|
|
|
from icalendar import Calendar, Event
|
|
|
|
import pytz
|
2020-09-20 20:53:24 +02:00
|
|
|
|
|
|
|
|
2020-09-20 21:11:48 +02:00
|
|
|
# Constants definition
|
|
|
|
URL = "https://docs.google.com/spreadsheets/u/0/d/e/2PACX-1vQ9yzFLr5mXbIZVK3ucdUZuScAbLoCyPqzHr-5V0aYeCFEz7LuidPdk_EnkkJT-zjemzQQHaKvpeXW2/pubhtml/sheet?headers=false&gid=1619638924"
|
2020-09-20 21:23:23 +02:00
|
|
|
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'),
|
|
|
|
]
|
2020-09-20 21:11:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GoogleSheetsCalParser(HTMLParser):
|
2020-09-20 20:55:27 +02:00
|
|
|
"""
|
|
|
|
Definition of a Google Sheets parser providing a table with each cell
|
|
|
|
and its position and size
|
|
|
|
"""
|
2020-09-20 21:11:48 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.inTable = False
|
2020-09-20 21:26:53 +02:00
|
|
|
self.inCell = False
|
2020-09-20 21:51:41 +02:00
|
|
|
self.inDate = False
|
2020-09-20 21:23:23 +02:00
|
|
|
self.row = -1
|
|
|
|
self.column = -1
|
2020-09-20 21:51:41 +02:00
|
|
|
self.rowspan = 0
|
|
|
|
self.date = 0
|
2020-09-20 22:11:47 +02:00
|
|
|
self.calendar = Calendar()
|
2020-09-20 21:14:12 +02:00
|
|
|
HTMLParser.__init__(self)
|
2020-09-20 21:11:48 +02:00
|
|
|
|
2020-09-20 20:53:24 +02:00
|
|
|
def handle_starttag(self, tag, attrs):
|
2020-09-20 21:51:41 +02:00
|
|
|
dAttrs = dict(attrs)
|
|
|
|
|
2020-09-20 21:14:12 +02:00
|
|
|
if self.inTable:
|
2020-09-20 21:23:23 +02:00
|
|
|
if tag == 'tr':
|
|
|
|
self.row += 1
|
|
|
|
self.column = -1
|
|
|
|
elif tag == 'td':
|
2020-09-20 21:51:41 +02:00
|
|
|
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):
|
2020-09-20 21:26:53 +02:00
|
|
|
self.inCell = True
|
2020-09-20 21:51:41 +02:00
|
|
|
if 'rowspan' in dAttrs.keys():
|
2020-09-20 21:54:57 +02:00
|
|
|
self.rowspan = int(dAttrs['rowspan'])
|
2020-09-20 21:51:41 +02:00
|
|
|
else: self.rowspan = 1
|
|
|
|
elif (self.row - 2) % 12 == 1:
|
|
|
|
self.inDate = True
|
2020-09-20 21:23:23 +02:00
|
|
|
elif tag == 'tbody':
|
2020-09-20 21:14:12 +02:00
|
|
|
self.inTable = True
|
2020-09-20 20:53:24 +02:00
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
2020-09-20 21:26:53 +02:00
|
|
|
if tag == 'td':
|
|
|
|
self.inCell = False
|
2020-09-20 21:51:41 +02:00
|
|
|
self.inDate = False
|
2020-09-20 21:26:53 +02:00
|
|
|
elif tag == 'tbody':
|
|
|
|
self.inTable = False
|
2020-09-20 20:53:24 +02:00
|
|
|
|
|
|
|
def handle_data(self, data):
|
2020-09-20 21:51:41 +02:00
|
|
|
if self.inDate:
|
2020-09-20 22:11:47 +02:00
|
|
|
self.date = dt.strptime(data, '%d-%b-%y').date()
|
2020-09-20 21:51:41 +02:00
|
|
|
elif self.inCell and data not in ['', '-']:
|
2020-09-20 22:11:47 +02:00
|
|
|
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)
|
2020-09-20 20:53:24 +02:00
|
|
|
|
|
|
|
|
2020-09-20 20:55:27 +02:00
|
|
|
# Getting the Google Sheet
|
2020-09-20 21:11:48 +02:00
|
|
|
try:
|
|
|
|
r = requests.get(URL)
|
|
|
|
except ConnectionError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit(1)
|
2020-09-20 20:53:24 +02:00
|
|
|
|
2020-09-20 21:11:48 +02:00
|
|
|
if r.status_code != 200:
|
|
|
|
print(f'Status Code {r.status_code}; could not continue.')
|
|
|
|
sys.exit(1)
|
2020-09-20 20:55:27 +02:00
|
|
|
|
|
|
|
# Parsing the Sheet
|
2020-09-20 21:14:12 +02:00
|
|
|
calParser = GoogleSheetsCalParser()
|
2020-09-20 21:11:48 +02:00
|
|
|
calParser.feed(r.text)
|
2020-09-20 20:55:27 +02:00
|
|
|
|
|
|
|
# Pushing events to caldav server
|