Skip to content

Instantly share code, notes, and snippets.

@MartijnBraam
Last active August 29, 2015 14:06
Show Gist options
  • Save MartijnBraam/04dd7039ad5fddcb02a6 to your computer and use it in GitHub Desktop.
Save MartijnBraam/04dd7039ad5fddcb02a6 to your computer and use it in GitHub Desktop.
Convert the drupalcon 2014 "My schedule" page to ical format
#!/usr/bin/env python3
import re, datetime, sys
# Clone https://gist.github.com/MartijnBraam/04dd7039ad5fddcb02a6
# Save your page schedule page to current directory as schedule.html
# ./schedule.py < schedule.html > schedule.ical
#
# Or use curl:
# curl -o - https://amsterdam2014.drupal.org/user/YOUR-USER-ID/schedule | ./schedule.py > schedule.ical
p = re.compile(r'<h3 class="section-title">\n<div class="slot_day">\n(?P<day>.*?)</div>.*?<div class="slot_time">\n (?P<start>.*?)-(?P<end>.*?)</div>.*?<a.*?>(?P<name>.*?)</a>.*?<div class="field-content">(?P<location>.*?)</div>', re.IGNORECASE | re.DOTALL)
html = sys.stdin.read()
print("BEGIN:VCALENDAR")
print("VERSION:2.0")
print("PRODID:-//hacksw/handcal//NONSGML v1.0//EN")
days = {
"Monday": datetime.date(2014, month=9, day=29),
"Tuesday": datetime.date(2014, month=9, day=30),
"Wednesday": datetime.date(2014, month=10, day=1),
"Thursday": datetime.date(2014, month=10, day=2),
"Friday": datetime.date(2014, month=10, day=3),
"Saturday": datetime.date(2014, month=10, day=4),
"Sunday": datetime.date(2014, month=10, day=5)
}
for event in re.findall(p, html):
day = days[event[0].strip()]
startsplit = event[1].replace('-', ':').split(':')
starthour = int(startsplit[0])-2
if starthour < 0:
starthour+=24
start = datetime.datetime(day.year, day.month, day.day, starthour, int(startsplit[1]))
endsplit = event[2].replace('-', ':').split(':')
endhour = int(endsplit[0])-2
if endhour < 0:
endhour+=24
if endsplit[0] == '24':
endsplit[0] = '0'
end = datetime.datetime(day.year, day.month, day.day, endhour, int(endsplit[1]))
print("BEGIN:VEVENT")
print("DTSTART:"+start.strftime("%Y%m%dT%H%M00Z"))
print("DTEND:"+end.strftime("%Y%m%dT%H%M00Z"))
print("SUMMARY:"+event[3])
print("LOCATION:"+event[4])
print("END:VEVENT")
print("END:VCALENDAR")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment