Py script to parse calendars

Posted on
Tue Mar 01, 2016 7:29 am
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Py script to parse calendars

Hi all,

I always wanted to have my calendar spoken in the morning but:

[*] iCal processor does not handle dates in EU format
[*] Plugin seems to have problem in El Capitan and will be outdated when the Calendar Store will be removed as (it is now in El Capitan deprecated and access to calendars/reminders shall be done with EventKit, so a bridge Python-Objective-C/Cocoa is needed (PyObjC); I will investigate for sure this solution but it takes time that at the moment I do not have).

==> I have therefore found another solution combining iCalBuddy which is very flexible and can parse calendar and a Python script (wrote by me).

The script is written on my needs and on my "calendar format", but feel free to modify and adapt it to you use cases
It also handles soccer matches (of my favourite team only) :)

Code: Select all
# 25.02 - First Version
# 26.02 - Added logging and moved completely to Python (no shell script)
# 27.02 - Separate handling 'calendars' and 'birthdays' + Indigo integration
# 27.02 - Events in time order
# 27.02 - Handling of football matches
# 29.02 - check length of events

indigo.server.log("Calendar - Execution Start")
import re, subprocess
p = subprocess.Popen("/usr/local/bin/icalBuddy -nc -eed -b '## ' -ps ' / ' eventsToday", shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
raw_events = out.replace("\n", " ").lstrip().rstrip().replace("     ", " ")
string_events = raw_events[3:]

## to modify according the calendar settings
sBirthday = "Birthday"
## to modify with your Indigo variable ID
idIndigoVar = 1047659443
## to modify according the system language (Eng: My Birthday - Ita: Mio Compleanno)
sMyBirthday = "My Birthday"
## your name
sMyName = "Marco"
sMilan = "AC Milan"

## !!! DO NOT EDIT !!! ##
delimiter = "##"
events = string_events.split(delimiter)
events = string_events.split(delimiter)
if len(string_events) > 3:
    eventsCount = len(events)
else:
    eventsCount = 0
seventsCount = ""
birthdaysCount = 0
sbirthdaysCount = ""
sIndigoVar = ""

indigo.variable.updateValue(idIndigoVar, value="")

for event in events:
    formatted_event = event.lstrip().rstrip()
    if sBirthday in formatted_event:
       birthdaysCount += 1
       eventsCount -= 1
       

if birthdaysCount == 0:
    sbirthdaysCount += "No birthdays today. "
    sIndigoVar += sbirthdaysCount
    indigo.server.log("Calendar - No birthdays")
elif birthdaysCount == 1:
    sbirthdaysCount += "Today there is one birthday. "
    sIndigoVar += sbirthdaysCount
    indigo.server.log("Calendar - One birthday")
else:
    sbirthdaysCount += "Today there are " + str(birthdaysCount) + " birthdays"
    sIndigoVar += sbirthdaysCount
    indigo.server.log("Calendar - >= 2 birthdays")

for event in events:
    if sBirthday in event:
        if sMyBirthday in event.lstrip().rstrip():
            bd = "Today is your birthday, happy birthday " + sMyName
            sIndigoVar += bd + ". "
            indigo.server.log("Calendar - My birthday")
        else:
            parts = event.split("'s")
            age = re.sub("[^0-9]", "", parts[1][-4:])
            bd = parts[0] + " will be " + age + " years old"
            indigo.server.log("Calendar - Other birthday")
            sIndigoVar += bd + ". "
       
if eventsCount == 1:
    seventsCount += "You have one appointment today. "
    indigo.server.log("Calendar - Only one appointment")
    sIndigoVar += seventsCount
else:
    seventsCount += "You have " + str(eventsCount) + " appointments today. "
    sIndigoVar += seventsCount
   
for event in events:
    if sBirthday not in event:
        if sMilan in event:
            event_parts = event.lstrip().rstrip().split("/")
            eventMilan = event_parts[0] + " at " + event_parts[1]
            sIndigoVar += eventMilan + ". "
        elif len(event) > 5:
           event_parts = event.lstrip().rstrip().split("/")
           eventMilan = event_parts[0] + " at " + event_parts[1]
           sIndigoVar += eventMilan + ". "
       
indigo.variable.updateValue(idIndigoVar, value=sIndigoVar)
indigo.server.log("Calendar - End of execution")


At the moment it works fine with my setup, if you need some changes, I can put it into GitHub :)

Posted on
Tue Mar 01, 2016 10:09 am
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Py script to parse calendars

Cool, thanks for the contribution!

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests