2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
.. moduleauthor:: Fabio Manganiello <blacklight86@gmail.com>
|
|
|
|
"""
|
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
import dateutil.parser
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-05-30 15:59:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CalendarInterface:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_upcoming_events(self, max_results=10):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class CalendarPlugin(Plugin, CalendarInterface):
|
|
|
|
"""
|
2018-06-23 01:00:43 +02:00
|
|
|
The CalendarPlugin allows you to keep track of multiple calendars (Google or
|
|
|
|
iCal URLs) and get joined events from all of them.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
* **dateutil** (``pip install python-dateutil``)
|
2018-05-30 15:59:07 +02:00
|
|
|
"""
|
|
|
|
|
2019-06-18 18:29:49 +02:00
|
|
|
def __init__(self, calendars=None, *args, **kwargs):
|
2018-05-30 15:59:07 +02:00
|
|
|
"""
|
2018-06-23 01:00:43 +02:00
|
|
|
:param calendars: List of calendars to be queried. Supported types so far: Google Calendar and iCal URLs.
|
|
|
|
:type calendars: list
|
2018-05-30 15:59:07 +02:00
|
|
|
|
2018-06-23 01:00:43 +02:00
|
|
|
Example format::
|
|
|
|
|
|
|
|
calendars = [
|
2018-05-30 15:59:07 +02:00
|
|
|
{
|
|
|
|
"type": "platypush.plugins.google.calendar.GoogleCalendarPlugin"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"type": "platypush.plugins.calendar.ical.IcalCalendarPlugin",
|
|
|
|
"url": "https://www.facebook.com/ical/u.php?uid=USER_ID&key=FB_KEY"
|
|
|
|
},
|
|
|
|
|
|
|
|
...
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-06-18 18:29:49 +02:00
|
|
|
|
|
|
|
if calendars is None:
|
|
|
|
calendars = []
|
2018-05-30 15:59:07 +02:00
|
|
|
self.calendars = []
|
|
|
|
|
|
|
|
for calendar in calendars:
|
|
|
|
if 'type' not in calendar:
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.warning("Invalid calendar with no type specified: {}".format(calendar))
|
2018-05-30 15:59:07 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
cal_type = calendar.pop('type')
|
|
|
|
module_name = '.'.join(cal_type.split('.')[:-1])
|
|
|
|
class_name = cal_type.split('.')[-1]
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
self.calendars.append(getattr(module, class_name)(**calendar))
|
|
|
|
|
|
|
|
|
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):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of upcoming events merging all the available calendars.
|
|
|
|
|
|
|
|
:param max_results: Maximum number of results to be returned (default: 10)
|
|
|
|
:type max_results: int
|
|
|
|
|
|
|
|
:returns: platypush.message.Response -- Response object with the list of events in the Google calendar API format.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
output = [
|
|
|
|
{
|
|
|
|
"id": "123456abcdef",
|
|
|
|
"kind": "calendar#event",
|
|
|
|
"status": "confirmed",
|
|
|
|
"htmlLink": "...",
|
|
|
|
"created": "2018-06-01T01:23:45.000Z",
|
|
|
|
"updated": "2018-06-01T01:23:45.000Z",
|
|
|
|
"creator": {
|
|
|
|
"email": "...",
|
|
|
|
"displayName": "...",
|
|
|
|
"self": true
|
|
|
|
},
|
|
|
|
"organizer" {
|
|
|
|
"email": "...",
|
|
|
|
"displayName": "...",
|
|
|
|
"self": true
|
|
|
|
},
|
|
|
|
"start": {
|
|
|
|
"dateTime": "2018-06-02T10:00:00.000Z",
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"dateTime": "2018-06-02T12:00:00.000Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
events = []
|
|
|
|
|
|
|
|
for calendar in self.calendars:
|
2018-12-20 16:40:14 +01:00
|
|
|
cal_events = calendar.get_upcoming_events().output or []
|
|
|
|
events.extend(cal_events)
|
2018-05-30 15:59:07 +02:00
|
|
|
|
|
|
|
events = sorted(events, key=lambda event:
|
2018-06-13 00:54:59 +02:00
|
|
|
dateutil.parser.parse(
|
|
|
|
event['start']['dateTime']
|
|
|
|
if 'dateTime' in event['start']
|
|
|
|
else event['start']['date'] + 'T00:00:00+00:00'
|
|
|
|
))[:max_results]
|
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:
|
|
|
|
|