Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created May 30, 2012 19:06
Show Gist options
  • Save slinkp/2838329 to your computer and use it in GitHub Desktop.
Save slinkp/2838329 to your computer and use it in GitHub Desktop.
Convert PyGotham schedule into iCal format
"""
Parse the pygotham json schedule and make ical output
for use w/ google calendar, etc.
NOTE if you are going to serve the result, apparently google cal won't parse
it unless the header Content-Type: text/calendar is set!
Really useful for testing output: http://icalvalid.cloudapp.net
and http://severinghaus.org/projects/icv/
"""
import json
import urllib2
import icalendar
import datetime
import pytz
url = "https://pygotham.org/talkvote/full_schedule/"
def get_schedule(url=url):
req = urllib2.Request(url=url, headers={'Accept': 'application/json'})
raw = urllib2.urlopen(req, timeout=30).read()
return json.loads(raw)
def parse_date(date):
zone = pytz.timezone('US/Eastern')
dt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
# Deal with timezones.
# Note that if we pass tz-aware datetimes to Event.add(),
# then the resulting output makes validators barf if it doesn't include
# a timezone definition section, and I dunno how to add one.
# So as a crappy workaround, we convert to UTC and then strip the zone info.
dt = zone.localize(dt)
dt = dt.astimezone(pytz.utc)
dt = dt.replace(tzinfo=None)
return dt
def make_ical_event(info):
event = icalendar.Event()
start = parse_date(info['talk_day_time'])
try:
room = 'Room %d' % int(info['room_number'])
except:
room = info['room_number'] # eg Plenary space
speaker = info['full_name']
event.add('summary', '%s (%s / %s)' % (info['title'], room, speaker)) # Subject.
levels = '/'.join(info['levels']) or 'unknown'
ttype = info['talktype']
# TODO: no idea what the talk types mean. a, b, c ...?
description = 'Speaker: %s. Levels: %s. Description: %s' % (speaker, levels, info['desc'])
event.add('description', description)
event.add('dtstart', start)
event.add('dtend', parse_date(info['talk_end_time']))
# Outlook demands this, per wikipedia; same as dtstart.
event.add('dtstamp', start)
event.add('location', room)
# TODO: info['outline']?
# Outlook demands this too, per wikipedia.
# Value is arbitrary; we'll use the 'key'.
event.add('uid', 'pygotham2/%s@pygotham.org' % info['key'])
return event
def make_icalendar(url):
sched = get_schedule()
cal = icalendar.Calendar()
cal.add('prodid', '-//PyGotham II ical script//pygotham.org//')
cal.add('version', '2.0')
for event_info in sched:
event = make_ical_event(event_info)
cal.add_component(event)
return cal.to_ical()
if __name__ == '__main__':
import sys
output = make_icalendar(url=url)
sys.stdout.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment