With a python program, I saved a ics file to a json one.The json file contains calendar info. My purpose is to replace a few specific strings (hours) by different ones (Keywords). basically 8:00 to Meeting1 ; 9:00 to Meeting 2 and so on. The Json content looks something like this "11/18/21 09:00 UTC-12/19/25 09:45 UTC Meeting-boss: - None". This being done by a python program would probably be to painful to change so I have to work with that.
This is the python program that parses the ics file into a json one :
I have no idea how to this. The json could be a txt file if it makes things easier btw. All help appreciated :)
This is the python program that parses the ics file into a json one :
Code:
from datetime import datetime, timedelta, timezone
import icalendar
from dateutil.rrule import *
f = open('myschool.json', 'w')
def parse_recurrences(recur_rule, start, exclusions):
""" Find all reoccuring events """
rules = rruleset()
first_rule = rrulestr(recur_rule, dtstart=start)
rules.rrule(first_rule)
if not isinstance(exclusions, list):
exclusions = [exclusions]
for xdate in exclusions:
try:
rules.exdate(xdate.dts[0].dt)
except AttributeError:
pass
now = datetime.now(timezone.utc)
this_year = now + timedelta(days=60)
dates = []
for rule in rules.between(now, this_year):
dates.append(rule.strftime("%D %H:%M UTC "))
return dates
icalfile = open('myschool.ics', 'rb')
gcal = icalendar.Calendar.from_ical(icalfile.read())
for component in gcal.walk():
if component.name == "VEVENT":
summary = component.get('summary')
description = component.get('description')
location = component.get('location')
startdt = component.get('dtstart').dt
enddt = component.get('dtend').dt
exdate = component.get('exdate')
if component.get('rrule'):
reoccur = component.get('rrule').to_ical().decode('utf-8')
for item in parse_recurrences(reoccur, startdt, exdate):
print("{0} {1}: {2} - {3}\n".format(item, summary, description, location), file = f)
else:
print("{0}-{1} {2}: {3} - {4}\n".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location), file = f)
icalfile.close()
Comment