This repository has been archived on 2020-10-14. You can view files and clone it, but cannot push or open issues or pull requests.
edt-parser/parser.py
2020-09-20 22:11:47 +02:00

107 lines
3 KiB
Python
Executable file

#!/usr/bin/env python
#-*- encoding: utf-8
import sys
import requests
from html.parser import HTMLParser
from datetime import datetime as dt, time
import caldav
from icalendar import Calendar, Event
import pytz
# 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
self.calendar = Calendar()
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 = int(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').date()
elif self.inCell and data not in ['', '-']:
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
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)
# Pushing events to caldav server