Created
July 20, 2017 16:09
-
-
Save Zren/1a3fe2d9226224720e5fc58fdd14bcee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys | |
import datetime | |
from icalendar import Calendar | |
import json | |
def dateToJson(dateObj): | |
if isinstance(dateObj.dt, datetime.date): | |
# { "date": "2010-08-04" } | |
dateStr = dateObj.dt.isoformat() | |
return { 'date': dateStr } | |
else: # datetime | |
# { "dateTime": "2010-08-04T02:44:20.063Z" } | |
dateTimeStr = dateObj.dt.isoformat() # 2014-10-02T18:00:00+00:00 | |
return { 'dateTime': dateTimeStr } | |
def eventsToJson(eventList=None, indent=4): | |
if eventList is None: | |
eventList = list(self.cal.walk('vevent')) | |
data = {} | |
data['items'] = [] | |
for event in eventList: | |
item = {} | |
item['kind'] = 'calendar#event' | |
item['etag'] = '\"0123456789012345\"' | |
item['iCalUID'] = event['UID'] | |
item['id'] = "ics_{}_{}_{}".format(item['iCalUID'], | |
event['DTSTART'].dt.isoformat(), | |
event['DTEND'].dt.isoformat() | |
) | |
item['status'] = 'confirmed' # TODO: event['STATUS'] | |
item['htmlLink'] = '' | |
item['created'] = event['CREATED'].dt.isoformat() | |
item['updated'] = event['LAST-MODIFIED'].dt.isoformat() | |
item['summary'] = event['SUMMARY'] | |
if event['LOCATION']: | |
item['location'] = event['LOCATION'] | |
item['start'] = dateToJson(event['DTSTART']) | |
item['end'] = dateToJson(event['DTEND']) | |
# item['transparency'] = event['TRANSP'] # 'transparent' | |
# item['recurringEventId'] = '' | |
data['items'].append(item) | |
return json.dumps(data, indent=indent) | |
def ensureDateTime(dt): | |
if isinstance(dt, datetime.date): | |
return datetime.datetime.combine(dt, datetime.time.min) | |
else: | |
return dt | |
def eventWithin(event, startTime, endTime): | |
eventStart = ensureDateTime(event['DTSTART'].dt) | |
eventEnd = ensureDateTime(event['DTEND'].dt) | |
startTime = ensureDateTime(startTime) | |
endTime = ensureDateTime(endTime) | |
# If it starts before endTime and it ends after startTime | |
return eventStart <= endTime and eventEnd >= startTime | |
class CalendarManager: | |
def __init__(self, filename): | |
self.filename = filename | |
self.cal = None | |
def read(self): | |
with open(self.filename, "r") as f: | |
text = f.read() | |
self.cal = Calendar.from_ical(text) | |
@property | |
def events(self): | |
return self.cal.walk('vevent') | |
def query(self, startTime, endTime): | |
for event in self.events: | |
if eventWithin(event, startTime, endTime): | |
print("within", event['DTSTART'].dt, event['DTEND'].dt) | |
yield event | |
else: | |
print("out", event['DTSTART'].dt, event['DTEND'].dt) | |
def toJson(self): | |
return eventsToJson(self.events) | |
if __name__ == '__main__': | |
manager = CalendarManager('basic.ics') | |
manager.read() | |
# for e in manager.cal.walk('vevent'): | |
# print(e) | |
# print(manager.toJson()) | |
startTime = datetime.datetime.strptime('2016-09-15', '%Y-%m-%d') | |
endTime = datetime.datetime.strptime('2016-09-16', '%Y-%m-%d') | |
eventList = manager.query(startTime, endTime) | |
print(eventsToJson(eventList)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment