#!/usr/bin/python3 import sys import os from icalendar import Calendar, Event def parse_ics_file(fp): cal = Calendar.from_ical(fp.read()) for event in cal.walk("VEVENT"): event.subcomponents = list( filter(lambda sc: sc.name != "VALARM", event.subcomponents) ) if url := event.get("X-MICROSOFT-SKYPETEAMSMEETINGURL"): event["location"] = url desc = '\n'.join([ s.strip() for s in event["description"].strip().split("_" * 8) if s and not 'teams.microsoft.com' in s ]) event["description"] = desc for k in [k for k in event.keys() if k.startswith("X-MICROSOFT")]: del event[k] return cal.to_ical() if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] != "-": for f in sys.argv[1:]: with open(f) as fp: res = parse_ics_file(fp) if res: with open(f, 'w') as fp: fp.write(res.decode('utf-8')) else: print(parse_ics_file(sys.stdin))