Skip to content

Instantly share code, notes, and snippets.

@tkan tkan/icalParseRTM.py
Last active Aug 6, 2018

Embed
What would you like to do?
RememberTheMilk - iCal duration/time-estimate parser
#!/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)

RememberTheMilk - iCal duration/time-estimate parser

This small script will get your secret iCal events from a URL and parse it in a way that an enddate is added. iCal compatible programs should display the correct length of an event. You will need a (virtual) server with cron jobs.

Install / Use

  1. pip install ics
  2. replace 'INSERT URL HERE' with your secret iCal URL
  3. set up cronjob, e.g. */15 * * * * python3.5 icalParseRTM.py
  4. the output file rtm_parsed.ics will be written
  5. point your iCal software to your ics file

Tested in Thunderbird 45.4.0 on Ubuntu 16.04 with Python 3.5; requires at least Python 2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.