Sending events

This commit is contained in:
Edgar P. Burkhart 2020-09-20 22:43:25 +02:00
parent e7d81f4811
commit f9d33f7b4e
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
1 changed files with 22 additions and 15 deletions

View File

@ -26,6 +26,7 @@ TIMETABLE = [
('17:45','17:45'),
]
DAVURL = "https://cal.edgarpierre.fr"
CALURL = "https://cal.edgarpierre.fr/edpibu/817427eb-be7c-4540-872a-dfb723a205a6/"
class GoogleSheetsCalParser(HTMLParser):
@ -40,8 +41,17 @@ class GoogleSheetsCalParser(HTMLParser):
self.row = -1
self.column = -1
self.rowspan = 0
self.date = 0
self.calendar = Calendar()
self.date = {}
import priv
client = caldav.DAVClient(url=DAVURL, username=priv.USERNAME,
password=priv.PASSWORD)
self.calendar = caldav.Calendar(client=client, url=CALURL)
for ev in self.calendar.events():
ev.delete()
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
@ -75,19 +85,25 @@ class GoogleSheetsCalParser(HTMLParser):
def handle_data(self, data):
if self.inDate:
self.date = dt.strptime(data, '%d-%b-%y').date()
self.date[self.column] = 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])]
cal = Calendar()
cal.add('prodid', '-//edpibu//edt-parser//FR')
cal.add('version', '2.0')
event = Event()
event.add('uid', f'{self.row}x{self.column}')
event.add('summary', data)
event.add('dtstart', dt.combine(self.date, times[0],
event.add('dtstamp', dt.now())
event.add('dtstart', dt.combine(self.date[self.column], times[0],
pytz.timezone('Europe/Paris')))
event.add('dtend', dt.combine(self.date, times[1],
event.add('dtend', dt.combine(self.date[self.column], times[1],
pytz.timezone('Europe/Paris')))
cal.add_component(event)
self.calendar.add_component(event)
self.calendar.save_event(cal.to_ical().decode('utf-8'))
# Getting the Google Sheet
@ -104,12 +120,3 @@ if r.status_code != 200:
# Parsing the Sheet
calParser = GoogleSheetsCalParser()
calParser.feed(r.text)
# Pushing events to caldav server
import priv
client = caldav.DAVClient(url=DAVURL, username=priv.USERNAME,
password=priv.PASSWORD)
pr = client.principal()
calendars = pr.calendars()
print(calendars)