|
#!/usr/bin/env python |
|
# -*- coding: utf-8 -*- |
|
"""Small script for parsing the time estate to a correctly displayed enddate in ICS format. |
|
Please do 'pip install ics' before using.""" |
|
|
|
# Python 2 and 3 switch for importing urllib |
|
try: |
|
from urllib.request import urlopen |
|
except ImportError: |
|
from urllib2 import urlopen |
|
|
|
# Python 2 and 3 switch for setting UTF8 as default string encoding |
|
try: |
|
import sys |
|
reload(sys) |
|
sys.setdefaultencoding('utf8') |
|
except (AttributeError, NameError): |
|
pass |
|
|
|
from ics import Calendar, Event |
|
from datetime import datetime, timedelta |
|
import dateutil.parser |
|
|
|
url = "INSERT URL HERE" |
|
cal = Calendar(urlopen(url).read().decode('utf-8')) |
|
|
|
newCal = Calendar() |
|
|
|
i = 0 |
|
|
|
while i < len(cal.events): |
|
checkBegin = dateutil.parser.parse(str(cal.events[i].begin)) |
|
|
|
# check for presence of time estimate and full date (including hours) |
|
if 'Time estimate' in (cal.events[i].description) and checkBegin.hour != 0: |
|
|
|
# get the time estimate |
|
s = cal.events[i].description |
|
|
|
# estimates can be in minutes or hours |
|
hourStart = s.find('Time estimate: PT') + 17 |
|
hourEnd = s.find('H', hourStart) |
|
hourDuration = (s[hourStart:hourEnd]) |
|
|
|
minuteStart = s.find('Time estimate: PT') + 17 |
|
minuteEnd = s.find('M', minuteStart) |
|
minuteDuration = (s[minuteStart:minuteEnd]) |
|
|
|
e = Event() |
|
e.name = cal.events[i].name |
|
e.location = cal.events[i].location |
|
e.uid = cal.events[i].uid |
|
e.created = cal.events[i].created |
|
e.description = cal.events[i].description |
|
|
|
endTime = dateutil.parser.parse(str(cal.events[i].begin)) |
|
|
|
# switch for minutes and hours; if it's minutes, hourDuration will |
|
# contain the whole description string from the event |
|
if len(hourDuration) > 2: |
|
endTime += timedelta(minutes=int(minuteDuration)) |
|
else: |
|
endTime += timedelta(hours=int(hourDuration)) |
|
e.begin = cal.events[i].begin |
|
e.end = endTime |
|
newCal.events.append(e) |
|
|
|
else: |
|
e = Event() |
|
e.name = cal.events[i].name |
|
e.location = cal.events[i].location |
|
e.uid = cal.events[i].uid |
|
e.created = cal.events[i].created |
|
e.description = cal.events[i].description |
|
e.begin = cal.events[i].begin |
|
|
|
newCal.events.append(e) |
|
|
|
i += 1 |
|
|
|
with open('rtm_parsed.ics', 'w') as f: |
|
f.writelines(newCal) |