Skip to content

Instantly share code, notes, and snippets.

@seamustuohy
Last active April 14, 2020 22:09
Show Gist options
  • Save seamustuohy/29a8ba0b3bef3a7f495427d3ec59147c to your computer and use it in GitHub Desktop.
Save seamustuohy/29a8ba0b3bef3a7f495427d3ec59147c to your computer and use it in GitHub Desktop.
A simple ical parser for mutt
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2020 seamus tuohy, <code@seamustuohy.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the included LICENSE file for details.
# ========== NOTES ========================
# - This does not deal with ics attachments because python icalendar doesn't handle them well
# - Example Mailcap entry below (Remove the # in front of it.)
#
# text/calendar; python3 ~/PATH/TO/ical_simple_parse.py -i %s
import argparse
# To install icalendar run the following
# sudo -H pip3 install icalendar
import icalendar
import logging
logging.basicConfig(level=logging.ERROR)
log = logging.getLogger(__name__)
def main():
args = parse_arguments()
set_logging(args.verbose, args.debug)
raw_ical_data = load_file(args.ical_file)
ical_obj = icalendar.Event.from_ical(raw_ical_data)
event_info = {}
for _obj in ical_obj.walk():
if isinstance(_obj, icalendar.cal.Event):
log.debug("Available Ical keys are {0}".format(_obj.keys())
event_info['attendees'] = [str(i.strip("mailto:")) for i in _obj.get('ATTENDEE',[])]
event_info['summary'] = str(_obj.get('SUMMARY',"UNKNOWN"))
event_info['location'] = str(_obj.get('LOCATION',"UNKNOWN"))
event_info['status'] = str(_obj.get('STATUS',"UNKNOWN"))
event_info['organizer'] = str(_obj.get('ORGANIZER',"UNKNOWN")).strip("mailto:")
event_info['ends'] = _obj['DTEND'].dt.strftime("%H:%M %A, %B %e, %Y %Z/%z")
event_info['starts'] = _obj['DTSTART'].dt.strftime("%H:%M %A, %B %e, %Y %Z/%z")
event_info['description'] = _obj.get('DESCRIPTION', "NONE")
formatted = format_event(event_info)
print(formatted)
def format_event(event):
txt = ""
txt += "\nSUMMARY: {0}".format(event['summary'])
txt += "\nSTATUS: {0}".format(event['status'])
txt += "\nSTARTS: {0}".format(event['starts'])
txt += "\nENDS: {0}".format(event['ends'])
txt += "\nLOCATION: {0}".format(event['location'])
txt += "\nORGANIZER: {0}".format(event['organizer'])
if len(event['attendees']) > 0:
txt += "\nATTENDEES:"
for i in event['attendees']:
txt += "\n - {0}".format(i)
txt += "\n===== DESCRIPTION =====\n\n"
txt += event['description']
return txt
def load_file(path):
with open(path, 'rb') as fp:
return fp.read()
# Command Line Functions below this point
def set_logging(verbose=False, debug=False):
if debug == True:
log.setLevel("DEBUG")
elif verbose == True:
log.setLevel("INFO")
def parse_arguments():
parser = argparse.ArgumentParser("package name")
parser.add_argument("--verbose", "-v",
help="Turn verbosity on",
action='store_true')
parser.add_argument("--debug", "-d",
help="Turn debugging on",
action='store_true')
parser.add_argument("--ical_file", "-i",
help="path to ical file",
required=True)
args = parser.parse_args()
return args
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment