Yet another Python-based vcalendar parser you can use with “Mutt”

The Mutt email client at work… thanks to symlink.dk

I’m still using “Mutt” as my main email client if you can believe it, and yes, I know it’s 2015. Still for me personally nothing allows me to work through my daily inbox at work (which can easily grow to 1,000 new messages a day) quicker than a keyboard-controlled, ASCII only email client like the venerable mutt (before that, I migrated from Unix’ old “mail” to “elm” in case you remember those).

No matter what tool I tried when attpempting to teach Mutt to show event invitations, parsing exchange-generated vcalendar events never worked properly for me, so I decided to come up with my own (much like every Python hacker worth his salt has to come up with an entire scratch built CMS framework at some point ;))

You can find the results below — as usual, the code is butt ugly (for some reason, my Python has started looking even worse than my Perl recently if such a thing is possible), but it does the job and works for me; YMMV.  You’ll certainly need to translate the field labels into your own language, but that shouldn’t be too difficult. You can then use mutt’s “auto_display” function to call the script automatically on text/calendar entries.

 

#!/usr/bin/env python
import sys

from datetime import *


def date_to_string(d):
 # 20151006T103000
 now=datetime.now()
 dt = datetime.strptime(d, "%Y%m%dT%H%M%S")
 return "%s (%d days)" % (dt.strftime("%A, %d. %B %Y, %H:%M Uhr"),
 abs (dt-now).days)

 

if __name__=="__main__":
 attendees=[]
 data=open(sys.argv[1]).readlines()
 for l in data:
 if l.find("ATTENDEE") > -1:
 attendees.append(l.strip().split(";")[4].replace('CN="', '').split(",")[0])
 if l.find('DTSTART;')==0:
 event_start=l.strip().split(":")[1]
 if l.find('DTEND;')==0:
 event_end=l.strip().split(":")[1]
 if l.find('SUMMARY;')==0:
 event_subject=l.strip().split(":")[1]
 if l.find('ORGANIZER;')==0:
 organizer = l.strip().split(";")[1].replace('"', '')
 orga_name, bla, orga_mail = organizer.split(":")
 orga_name=orga_name.replace('CN=', '')
 if l.find("LOCATION;")==0:
 location=l.strip().split(":")[1].replace('\\', '')

 if l.find("TZID:")==0:
 time_zone=l.strip().split(":")[1]
 
 
 print "===================================================================="
 print "Thema : ", event_subject
 print "Ersteller : ", orga_name
 print " ", orga_mail
 print "Start : ", date_to_string(event_start)
 print "Ende : ", date_to_string(event_end)
 print "Zeitzone : ", time_zone
 print "Ort : ", location
 print "Teilnehmer: ", ", ".join(attendees) 
 print "===================================================================="
 
 sys.exit(0)

Leave a Reply

Your email address will not be published. Required fields are marked *