Created
October 25, 2017 23:17
-
-
Save Frimkron/eeaf165cbdc110e2cc1256b061a6b869 to your computer and use it in GitHub Desktop.
PyCon UK 2017 Schedule to iCalendar script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import sys | |
import os | |
import os.path | |
import begin | |
import icalendar | |
import yaml | |
import pytz | |
from datetime import date, datetime, timedelta | |
@begin.start | |
def main(sitepath: "Path to website source", | |
output: "File to write iCalendar to"='pyconuk2017.ics', | |
website: "URL of online website to link to"='http://2017.pyconuk.org'): | |
"""Creates ical data from the schedule data in the PyConUK website source""" | |
schedpath = os.path.join(sitepath, 'schedule') | |
sesspath = os.path.join(sitepath, 'sessions') | |
tz = pytz.timezone('Europe/London') | |
now = datetime.now(tz=tz) | |
cal = icalendar.Calendar() | |
cal.add('prodid', '-//pyconical.py//Mark Frimston//') | |
cal.add('version', '2.0') | |
for day in os.listdir(schedpath): | |
daydate = int(re.sub(r'\D', '', day)) | |
daypath = os.path.join(schedpath, day) | |
for room in os.listdir(daypath): | |
roompath = os.path.join(daypath, room) | |
for time in os.listdir(roompath): | |
hour,minute = map(int,time.split(':')) | |
with open(os.path.join(roompath,time),'r') as f: | |
slotdata = yaml.load(f) | |
ev = icalendar.Event() | |
ev.add('uid', 'pcuk17-{}-{}-{}-{}'.format(room.lower().replace(' ',''), | |
daydate, hour, minute)) | |
ev.add('dtstart', datetime(2017, 10, daydate, hour, minute, 0, tzinfo=tz)) | |
ev.add('duration', timedelta(0, 60*30)) | |
ev.add('dtstamp', now) | |
ev.add('location', room) | |
title = slotdata.get('title', None) | |
session = slotdata.get('session', None) | |
if title: | |
ev.add('summary', title) | |
ev.add('categories', ['non-session']) | |
elif session: | |
with open(os.path.join(sesspath, '{}.md'.format(session)), 'r') as f: | |
sessdata = next(yaml.load_all(f)) | |
f.seek(0) | |
desc = re.sub(r'^.*?---\n','',f.read(), flags=re.DOTALL) | |
stype, sname = session.split('/') | |
title = '[{}] {}'.format(stype, sessdata.get('title', None)) | |
subtitle = sessdata.get('subtitle', None) | |
if subtitle: title += ': {}'.format(subtitle) | |
cats = [stype] | |
track = sessdata.get('track', None) | |
if track: cats.append(track) | |
ev.add('summary', title) | |
ev.set_inline('categories', cats) | |
ev.add('description', desc) | |
ev.add('url', '{}/sessions/{}/{}'.format(website, stype+'s', sname)) | |
cal.add_component(ev) | |
with open(output, 'wb') as f: | |
f.write(cal.to_ical()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment