diff --git a/__init__.py b/__init__.py index dc84ffd..0bc487b 100644 --- a/__init__.py +++ b/__init__.py @@ -135,7 +135,7 @@ def toNaturalColor(colormap, orgColor): # uses the event color id to convert it to a hex color # does this for every event in events # this is used for Google Calendars -def colorizeEvents(events, colors): +def colorizeGoogleEvents(events, colors): COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json') with open(COLORS_FILE) as f: colormap = json.load(f) @@ -190,17 +190,18 @@ def getTimeStamps(): # define today and tomorrow now = datetime.datetime.now(datetime.timezone.utc).astimezone() - today = now.replace(hour=0, minute=0, second = 0).isoformat() + today = now.replace(hour=0, minute=0, second = 0) tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00' twodaysfromnow = datetime.datetime.today() + datetime.timedelta(days=2) - twodaysfromnow = twodaysfromnow.replace(hour=23, minute=59, second=59).astimezone().isoformat() + twodaysfromnow = twodaysfromnow.replace(hour=23, minute=59, second=59).astimezone() return today, twodaysfromnow def generateJsonFromCalendarEntries(events, colors): # fix all colors in events that have a different event color - colorizeEvents(events, colors) + if colors != None: + colorizeGoogleEvents(events, colors) # if not events: # print('No upcoming events found.') diff --git a/icalHandler.py b/icalHandler.py new file mode 100644 index 0000000..0973a38 --- /dev/null +++ b/icalHandler.py @@ -0,0 +1,63 @@ +from server import db +from database.models import Calendar as dbCalendar +from backend import Event as bEvent +from ics import Calendar as iCalendar +import requests + +def icalToCalendarDb(url, name, user): + try: + c = iCalendar(requests.get(url).text) + except: + return False + + + c = dbCalendar(calendar_id = url, + calendar_type = 'ical', + name = name, + toggle = "False", + color = "#000000") + db.session.add(c) + user.calendars.append(c) + db.session.commit() + +def fetchCalendarEvents(calendars, startDate, endDate): + + all_events = [] + for calendar in calendars: + if calendar.toggle == "True" and calendar.calendar_type == "ical": + ical = iCalendar(requests.get(calendar.calendar_id).text) + for event in ical.events: + + name = event.name + start = event.begin.format() + end = event.end.format() + + newEvent = bEvent(name, start, end) + + + if eventWithinStartEnd(startDate, endDate, event): + if not eventAllDay(event): + + all_events.append(newEvent) + + return all_events + + +# converts everything to epoch seconds and utc +# and then checks if at either event start or event end +# is is within startDate and endDate +def eventWithinStartEnd(startDate, endDate, event): + if((startDate.timestamp() <= event.begin.timestamp and + event.begin.timestamp <= endDate.timestamp()) or + startDate.timestamp() <= event.end.timestamp and + event.end.timestamp <= endDate.timestamp()): + return True + return False + +def eventAllDay(event): + beginDay = event.begin.replace(hour=0, minute=0, second=0) + endDay = event.end.replace(hour=0, minute=0, second=0) + delta = endDay - beginDay + if delta.days >= 2: + return True + return False