2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
.. moduleauthor:: Fabio Manganiello <blacklight86@gmail.com>
|
|
|
|
"""
|
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
import datetime
|
|
|
|
import requests
|
2022-01-05 18:04:32 +01:00
|
|
|
from typing import Optional
|
2018-05-30 15:59:07 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-05-30 15:59:07 +02:00
|
|
|
from platypush.plugins.calendar import CalendarInterface
|
|
|
|
|
|
|
|
|
2019-06-18 18:29:49 +02:00
|
|
|
class CalendarIcalPlugin(Plugin, CalendarInterface):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
iCal calendars plugin. Interact with remote calendars in iCal format.
|
2018-06-26 23:19:35 +02:00
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **icalendar** (``pip install icalendar``)
|
2021-09-16 17:53:40 +02:00
|
|
|
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
def __init__(self, url, *args, **kwargs):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
:param url: iCal URL to parse
|
|
|
|
:type url: str
|
|
|
|
"""
|
2018-05-30 15:59:07 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.url = url
|
|
|
|
|
2021-09-16 17:53:40 +02:00
|
|
|
@staticmethod
|
2022-01-05 18:04:32 +01:00
|
|
|
def _convert_timestamp(event, attribute: str) -> Optional[str]:
|
2022-01-05 13:31:07 +01:00
|
|
|
t = event.get(attribute)
|
|
|
|
if not t:
|
|
|
|
return
|
|
|
|
|
2022-01-05 18:04:32 +01:00
|
|
|
if type(t.dt) == datetime.date:
|
|
|
|
return (
|
|
|
|
datetime.datetime(
|
|
|
|
t.dt.year, t.dt.month, t.dt.day, tzinfo=datetime.timezone.utc
|
|
|
|
).isoformat()
|
|
|
|
)
|
|
|
|
|
2022-01-05 13:31:07 +01:00
|
|
|
return (
|
2022-01-05 18:04:32 +01:00
|
|
|
datetime.datetime.utcfromtimestamp(t.dt.timestamp())
|
|
|
|
.replace(tzinfo=datetime.timezone.utc).isoformat()
|
|
|
|
)
|
2022-01-05 13:31:07 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _translate_event(cls, event):
|
2018-05-30 15:59:07 +02:00
|
|
|
return {
|
|
|
|
'id': str(event.get('uid')) if event.get('uid') else None,
|
|
|
|
'kind': 'calendar#event',
|
|
|
|
'summary': str(event.get('summary')) if event.get('summary') else None,
|
|
|
|
'description': str(event.get('description')) if event.get('description') else None,
|
|
|
|
'status': str(event.get('status')).lower() if event.get('status') else None,
|
|
|
|
'responseStatus': str(event.get('partstat')).lower() if event.get('partstat') else None,
|
|
|
|
'location': str(event.get('location')) if event.get('location') else None,
|
|
|
|
'htmlLink': str(event.get('url')) if event.get('url') else None,
|
|
|
|
'organizer': {
|
|
|
|
'email': str(event.get('organizer')).replace('MAILTO:', ''),
|
2022-02-17 10:55:35 +01:00
|
|
|
'displayName': event.get('organizer').params.get('cn')
|
2018-05-30 15:59:07 +02:00
|
|
|
} if event.get('organizer') else None,
|
|
|
|
|
2022-01-05 13:31:07 +01:00
|
|
|
'created': cls._convert_timestamp(event, 'created'),
|
|
|
|
'updated': cls._convert_timestamp(event, 'last-modified'),
|
2018-05-30 15:59:07 +02:00
|
|
|
'start': {
|
2022-01-05 13:31:07 +01:00
|
|
|
'dateTime': cls._convert_timestamp(event, 'dtstart'),
|
|
|
|
'timeZone': 'UTC',
|
2018-05-30 15:59:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'end': {
|
2022-01-05 13:31:07 +01:00
|
|
|
'dateTime': cls._convert_timestamp(event, 'dtend'),
|
2018-05-30 15:59:07 +02:00
|
|
|
'timeZone': 'UTC',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-30 15:59:07 +02:00
|
|
|
def get_upcoming_events(self, max_results=10, only_participating=True):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
Get the upcoming events. See
|
|
|
|
:func:`~platypush.plugins.calendar.CalendarPlugin.get_upcoming_events`.
|
|
|
|
"""
|
|
|
|
|
2019-12-01 23:35:05 +01:00
|
|
|
from icalendar import Calendar
|
2019-06-18 18:29:49 +02:00
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
events = []
|
2021-03-05 11:16:14 +01:00
|
|
|
response = requests.get(self.url)
|
|
|
|
assert response.ok, \
|
2021-09-16 17:53:40 +02:00
|
|
|
"HTTP error while getting events from {}: {}".format(self.url, response.text)
|
2021-03-05 11:16:14 +01:00
|
|
|
|
|
|
|
calendar = Calendar.from_ical(response.text)
|
|
|
|
for event in calendar.walk():
|
|
|
|
if event.name != 'VEVENT':
|
|
|
|
continue # Not an event
|
|
|
|
|
|
|
|
event = self._translate_event(event)
|
|
|
|
|
2021-11-21 23:50:35 +01:00
|
|
|
if (
|
|
|
|
event['status'] != 'cancelled'
|
2022-01-05 13:31:07 +01:00
|
|
|
and event['end'].get('dateTime')
|
2022-01-05 18:04:32 +01:00
|
|
|
and event['end']['dateTime'] >= datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
|
2021-03-05 11:16:14 +01:00
|
|
|
and (
|
2021-09-16 17:53:40 +02:00
|
|
|
(only_participating
|
2021-11-21 23:50:35 +01:00
|
|
|
and event.get('responseStatus') in [None, 'accepted', 'tentative'])
|
|
|
|
or not only_participating)
|
|
|
|
):
|
2021-03-05 11:16:14 +01:00
|
|
|
events.append(event)
|
2018-05-30 15:59:07 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return events
|
2018-05-30 15:59:07 +02:00
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|