2019-02-26 22:08:19 +01:00
|
|
|
from CalendarInterface import CalendarInterface
|
|
|
|
from CalendarEvent import CalendarEvent
|
|
|
|
from ics import Calendar
|
|
|
|
from datetime import datetime, timedelta, timezone
|
2019-03-24 11:07:22 +01:00
|
|
|
import re
|
2019-02-26 22:08:19 +01:00
|
|
|
from settings import week_starts_on
|
|
|
|
try:
|
|
|
|
from urllib.request import urlopen
|
|
|
|
except Exception as e:
|
|
|
|
print("Something didn't work right, maybe you're offline?" + e.reason)
|
|
|
|
|
|
|
|
class IcalEvents(CalendarInterface):
|
|
|
|
"""Fetches events from ical addresses."""
|
2019-03-10 18:32:20 +01:00
|
|
|
def __init__(self, urls, highlighted_urls=None):
|
2019-02-26 22:08:19 +01:00
|
|
|
self.urls = urls
|
2019-03-10 18:32:20 +01:00
|
|
|
self.highlighted_urls = highlighted_urls
|
2019-03-03 13:53:22 +01:00
|
|
|
super(IcalEvents, self).__init__()
|
2019-02-26 22:08:19 +01:00
|
|
|
|
2019-03-03 13:53:22 +01:00
|
|
|
def __get_events__(self):
|
2019-03-10 18:32:20 +01:00
|
|
|
events = self.__get_events_from_urls__(self.urls)
|
|
|
|
|
|
|
|
highlighted = self.__get_events_from_urls__(self.highlighted_urls)
|
2019-03-10 19:09:37 +01:00
|
|
|
highlighted = map(self.__highlight_event__, highlighted)
|
2019-03-10 18:32:20 +01:00
|
|
|
events.extend(highlighted)
|
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
def __highlight_event__(self, event):
|
|
|
|
event.highlight = True
|
|
|
|
return event
|
|
|
|
|
|
|
|
def __get_events_from_urls__(self, urls):
|
2019-03-03 13:53:22 +01:00
|
|
|
loaded_events = []
|
2019-02-26 22:08:19 +01:00
|
|
|
try:
|
2019-03-10 18:32:20 +01:00
|
|
|
if urls is None:
|
|
|
|
return loaded_events
|
|
|
|
|
|
|
|
for calendar in urls:
|
2019-02-26 22:08:19 +01:00
|
|
|
decode = str(urlopen(calendar).read().decode())
|
|
|
|
|
2019-03-24 11:07:22 +01:00
|
|
|
ical = Calendar(decode)
|
2019-02-26 22:08:19 +01:00
|
|
|
for event in ical.events:
|
|
|
|
cal_event = CalendarEvent()
|
|
|
|
|
|
|
|
cal_event.fetch_datetime = datetime.now()
|
|
|
|
cal_event.begin_datetime = event.begin.datetime
|
|
|
|
cal_event.end_datetime = event.end.datetime
|
2019-03-16 18:50:52 +01:00
|
|
|
cal_event.duration = event.duration
|
2019-02-26 22:08:19 +01:00
|
|
|
cal_event.title = event.name
|
|
|
|
cal_event.description = event.description
|
2019-03-11 19:07:59 +01:00
|
|
|
cal_event.location = event.location
|
2019-03-10 13:24:37 +01:00
|
|
|
cal_event.allday = event.all_day
|
2019-03-24 11:07:22 +01:00
|
|
|
cal_event.rrule = self.__extract_rrule__(event)
|
2019-02-26 22:08:19 +01:00
|
|
|
|
2019-03-21 22:52:42 +01:00
|
|
|
if cal_event.allday:
|
2019-03-24 11:07:22 +01:00
|
|
|
cal_event.end_datetime = cal_event.end_datetime - timedelta(1)
|
|
|
|
cal_event.duration = cal_event.duration - timedelta(1)
|
2019-03-21 22:52:42 +01:00
|
|
|
|
2019-03-03 13:53:22 +01:00
|
|
|
loaded_events.append(cal_event)
|
|
|
|
return loaded_events
|
2019-03-24 11:07:22 +01:00
|
|
|
except BaseException as ex:
|
|
|
|
print(ex)
|
2019-03-10 18:32:20 +01:00
|
|
|
return loaded_events
|
2019-02-26 22:08:19 +01:00
|
|
|
|
2019-03-11 19:07:59 +01:00
|
|
|
def __remove_alarms__(self, decode):
|
|
|
|
alarm_begin = 'BEGIN:VALARM'
|
|
|
|
alarm_end = 'END:VALARM'
|
|
|
|
lineseparation = '\r\n'
|
|
|
|
|
|
|
|
beginAlarmIndex = 0
|
|
|
|
while beginAlarmIndex >= 0:
|
|
|
|
beginAlarmIndex = decode.find(alarm_begin)
|
|
|
|
if beginAlarmIndex >= 0:
|
|
|
|
endAlarmIndex = decode.find(alarm_end, beginAlarmIndex)
|
|
|
|
decode = decode[:beginAlarmIndex] + decode[endAlarmIndex + len(alarm_end) + len(lineseparation):]
|
2019-03-24 11:07:22 +01:00
|
|
|
return decode
|
|
|
|
|
|
|
|
def __extract_rrule__(self, event):
|
|
|
|
if re.search('RRULE',str(event)) is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return re.search('RRULE:(.+?)\n',str(event)).group(1).rstrip()
|