#!/usr/bin/env python3 import sys from datetime import datetime, timedelta from dateutil.parser import parse from dateutil.rrule import rruleset, rrulestr import icalendar import locale import logging from pprint import pprint def vevent_to_event(event, rrstart=None): if rrstart == None: begin = parse(event['DTSTART'].to_ical()) else: begin = rrstart return { "name": event['SUMMARY'].to_ical(), "url": event['URL'].to_ical(), "begin": begin } def parse_single_event(event, start, end): logging.info("Processing single event %s" % event['SUMMARY'].to_ical().decode('utf-8')) dtstart = parse(event['DTSTART'].to_ical()) if dtstart >= start and dtstart < end: return vevent_to_event(event) else: return None def parse_recurring_event(event, start, end): logging.info("Processing recurring event %s" % event['SUMMARY'].to_ical().decode('utf-8')) dtstart = parse(event['DTSTART'].to_ical()) rs = rruleset() rs.rrule(rrulestr(event['RRULE'].to_ical().decode('utf-8'), dtstart=dtstart)) if 'EXDATE' in event.keys(): exdates = event['EXDATE'] for exdate in exdates: rs.exdate(parse(exdate.to_ical())) dates = list(rs) events = [] for date in dates: if date >= start and date < end: events.append(vevent_to_event(event, date)) return events def find_events(icsfilestr, start, end, num): with open(icsfilestr, 'r') as icsfile: cal=icalendar.Calendar.from_ical(icsfile.read()) events=[] for event in cal.subcomponents: if event.name == 'VEVENT': if 'RRULE' in event.keys(): events = events + parse_recurring_event(event, start, end) else: ev = parse_single_event(event, start, end) if ev != None: events.append(ev) events = sorted(events, key=lambda k: k['begin']) events = events[0:num] return events def format_events(events): print('
%s | %s |