Skip to content

Instantly share code, notes, and snippets.

@dylanferguson
Created July 12, 2018 15:58
Show Gist options
  • Select an option

  • Save dylanferguson/3500f7c705beaa866e8aacb2586dfc15 to your computer and use it in GitHub Desktop.

Select an option

Save dylanferguson/3500f7c705beaa866e8aacb2586dfc15 to your computer and use it in GitHub Desktop.
from ics import Calendar, Event
from datetime import datetime, timedelta
from pytz import timezone
import csv
import re
if __name__ == "__main__":
with open('timetable.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
time_zone = timezone('Australia/Melbourne')
events = []
for row in reader:
for date in [x.strip() for x in row['Dates'].split(',')]:
if (re.match('.*-.*', date)):
start_date = time_zone.localize(datetime.strptime(date.split(
'-')[0] + '/18' + row['Time'], '%d/%m/%y%H:%M'))
end_date = time_zone.localize(datetime.strptime(date.split(
'-')[1] + '/18' + row['Time'], '%d/%m/%y%H:%M')) + timedelta(hours=1)
while start_date <= end_date:
e = Event(name=row['Subject Code'][:7] + '-' + row['Description'] + ' - ' + row[
'Group'], begin=start_date, end=start_date + timedelta(hours=int(row['Duration'][:1])), location=row['Location'])
events.append(e)
start_date += timedelta(days=7)
else:
day, month = [int(x) for x in date.split('/')]
hour, minute = [int(x) for x in row['Time'].split(':')]
begin = time_zone.localize(
datetime(2018, month, day, hour, minute))
end = begin + timedelta(hours=int(row['Duration'][:1]))
e = Event(name=row['Subject Code'][:7] + '-' + row['Description'] + ' - ' + row[
'Group'], begin=begin, end=end, location=row['Location'])
events.append(e)
with open('uni.ics', 'w') as f:
f.writelines(Calendar(events=events))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment