Skip to content

Instantly share code, notes, and snippets.

@maximz
Created December 21, 2015 07:37
Show Gist options
  • Save maximz/975273b907bbdae5763d to your computer and use it in GitHub Desktop.
Save maximz/975273b907bbdae5763d to your computer and use it in GitHub Desktop.
Make ical
from icalendar import Calendar, Event
cal = Calendar()
from datetime import datetime
cal.add('prodid', '-//Recal Course Planner//recal.io//')
cal.add('version', '2.0')
cal.add('X-WR-CALNAME', 'ReCal S16 (maximz)')
cal.add('X-WR-CALDESC', 'ReCal Schedule')
cal.add('X-PUBLISHED-TTL', 'PT15M') # https://msdn.microsoft.com/en-us/library/ee178699(v=exchg.80).aspx. 15 minute updates.
import pytz
event = Event()
tz = pytz.timezone("US/Eastern") # pytz.utc
event.add('summary', 'COS 333 L01')
#event.add('dtstart', datetime(2005,4,4,8,0,0,tzinfo=tz))
#event.add('dtend', datetime(2005,4,4,10,0,0,tzinfo=tz))
#event.add('dtstamp', datetime(2005,4,4,0,10,0,tzinfo=tz))
# http://pytz.sourceforge.net/
event.add('dtstart', tz.localize(datetime(2016,2,2,10,0,0))) #2/2 is first Tuesday. encoding Tue/Thurs 10:00 - 10:50am class.
event.add('dtend', tz.localize(datetime(2016,2,2,10,50,0)))
event.add('dtstamp', tz.localize(datetime(2016,2,1,0,0,0))) # "property specifies the DATE-TIME that iCalendar object was created". per 3.8.7.2 of RFC 5545, must be in UTC
from icalendar import vCalAddress, vText, vDatetime, vRecur
event.add('location', vText('50 McCosh Hall, Princeton, NJ'))
# recurrence
days = {
0: 'MO',
1: 'TU',
2: 'WE',
3: 'TH',
4: 'FR'
}
selected_days = [1,3]
selected_days = sorted(selected_days)
end_date = tz.localize(datetime(2016,5,31,0,0,0)) #[LAST DAY OF SEMESTER + 1]
event.add('rrule', vRecur({'FREQ': 'WEEKLY', 'UNTIL': end_date, 'WKST': 'SU', 'BYDAY': [days[s] for s in selected_days]}))
#RRULE:FREQ=WEEKLY;UNTIL=[LAST DAY OF SEMESTER + 1];WKST=SU;BYDAY=TU,TH
#others are MO,WE,FR
cal.add_component(event)
ical = cal.to_ical()
print ical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment