#!/usr/bin/python from datetime import datetime, timedelta from dateutil.parser import parse # Take these from https://www.ox.ac.uk/about/facts-and-figures/dates-of-term?wssl=1 # Sunday's of Week 1 in each term calender_starts = [['MT', '08-10-2017'], ['HT', '14-01-2018'], ['TT', '22-04-2018'], ['MT', '07-10-2018'], ['HT', '13-01-2019'], ['TT', '28-04-2019']] prelim = '''BEGIN:VCALENDAR PRODID:-//Oxford Weeks//Haji-Ali//EN VERSION:2.0 CALSCALE:GREGORIAN METHOD:PUBLISH X-WR-CALNAME;VALUE=TEXT:Oxford Weeks X-WR-TIMEZONE:Europe/London ''' event = '''BEGIN:VEVENT DTSTART;VALUE=DATE:{start} DTEND;VALUE=DATE:{end} DTSTAMP:{stamp} DESCRIPTION: LOCATION:Oxford SEQUENCE:{seq} SUMMARY:{summary} TRANSP:TRANSPARENT END:VEVENT ''' post = '''END:VCALENDAR''' with open('oxford_weeks.ics', 'w') as f: f.write(prelim) stamp = datetime.now().strftime('%Y%m%dT%H%M%SZ') for term, week1 in calender_starts: week0 = parse(week1, dayfirst=True) + timedelta(days=-6) for i in range(9): f.write(event.format(summary='{} - Week {}'.format(term, i), start=week0.strftime('%Y%m%d'), end=(week0 + timedelta(days=5)).strftime('%Y%m%d'), stamp=stamp, seq=i)) week0 += timedelta(days=7) f.write(post)