Created
November 7, 2013 09:43
-
-
Save pudquick/7351922 to your computer and use it in GitHub Desktop.
This file automatically pulls the current calendar information for the 4 calendars published at schedule.mactech.com for MacTech 2013 and builds 4 separate .ics files with the information.
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
from icalendar import Event, Calendar | |
from datetime import datetime | |
import urllib2, urllib, pytz, dateutil.parser | |
import xml.etree.ElementTree as ET | |
ical_rss_base_url = 'http://schedule.mactech.com/rss/rss2.0.php?cal=%s&cpath=&rssview=month' | |
ical_rss_calendars = ['Dev Track', 'General Schedule', 'IT Track', 'Special'] | |
# X-APPLE-CALENDAR-COLOR | |
ical_colors = {'Dev Track': '#0E61B9', | |
'General Schedule': '#44A703', | |
'IT Track': '#F64F00', | |
'Special': '#711A76'} | |
timezone = pytz.timezone('America/Los_Angeles') | |
mod_time = datetime.now(timezone) | |
for cal_name in ical_rss_calendars: | |
extended_name = 'MacTech 2013 - %s' % cal_name | |
# Get the RSS for the calendar week in question | |
http_conn = urllib2.urlopen((ical_rss_base_url % cal_name).replace(' ', '%20')) | |
rss_xml = http_conn.read() | |
http_conn.close() | |
# Create the base calendar object | |
cal = Calendar() | |
cal.add('X-WR-CALNAME', extended_name) | |
cal.add('prodid', '-//frogor//is awesome//EN') | |
cal.add('version', '2.0') | |
cal.add('X-APPLE-CALENDAR-COLOR', ical_colors[cal_name]) | |
cal.add('X-WR-TIMEZONE', 'America/Los_Angeles') | |
# Parse in event objects | |
xml_cal = ET.fromstring(rss_xml) | |
# Pull out all the event objects | |
for an_event in xml_cal.find('channel').findall('item'): | |
# Pull the attributes we need: | |
event = Event() | |
# title = summary | |
# title.text needs to have the date split out: Wed Nov 6 2013: Dev: D.... | |
event.add('summary', urllib.unquote(an_event.find('title').text.split(': ',1)[-1])) | |
# If a location is specified, add one: | |
if an_event.find('{http://purl.org/rss/1.0/modules/event/}location') is not None: | |
event.add('location', urllib.unquote(an_event.find('{http://purl.org/rss/1.0/modules/event/}location').text.replace('+','%20'))) | |
# If notes/description is specified, add one: | |
if (an_event.find('description') is not None) and (an_event.find('description').text.split('M: ',1)[-1]): | |
event.add('description', urllib.unquote(an_event.find('description').text.split('M: ',1)[-1])) | |
# dtstart = startdate | |
event.add('dtstart', dateutil.parser.parse(an_event.find('{http://purl.org/rss/1.0/modules/event/}startdate').text).replace(tzinfo=timezone)) | |
# dtend = enddate | |
event.add('dtend', dateutil.parser.parse(an_event.find('{http://purl.org/rss/1.0/modules/event/}enddate').text).replace(tzinfo=timezone)) | |
# dtstamp = mod_time | |
event.add('dtstamp', mod_time) | |
event['uid'] = an_event.find('guid').text.split('uid=',1)[-1][:36] | |
cal.add_component(event) | |
f = open('%s.ics' % extended_name, 'wb') | |
f.write(cal.to_ical()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment