2018-05-04 19:20:23 +02:00
|
|
|
import datetime
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import action
|
2018-05-04 19:20:23 +02:00
|
|
|
from platypush.plugins.google import GooglePlugin
|
2018-05-30 15:59:07 +02:00
|
|
|
from platypush.plugins.calendar import CalendarInterface
|
2018-05-04 19:20:23 +02:00
|
|
|
|
|
|
|
|
2018-05-30 15:59:07 +02:00
|
|
|
class GoogleCalendarPlugin(GooglePlugin, CalendarInterface):
|
2023-10-01 15:37:20 +02:00
|
|
|
r"""
|
2023-09-24 16:54:43 +02:00
|
|
|
Google Calendar plugin.
|
2023-10-01 15:37:20 +02:00
|
|
|
|
|
|
|
In order to use this plugin:
|
|
|
|
|
|
|
|
1. Create your Google application, if you don't have one already, on
|
|
|
|
the `developers console <https://console.developers.google.com>`_.
|
|
|
|
|
|
|
|
2. You may have to explicitly enable your user to use the app if the app
|
|
|
|
is created in test mode. Go to "OAuth consent screen" and add your user's
|
|
|
|
email address to the list of authorized users.
|
|
|
|
|
|
|
|
3. Select the scopes that you want to enable for your application, depending
|
|
|
|
on the integrations that you want to use.
|
|
|
|
See https://developers.google.com/identity/protocols/oauth2/scopes
|
|
|
|
for a list of the available scopes.
|
|
|
|
|
|
|
|
4. Click on "Credentials", then "Create credentials" -> "OAuth client ID".
|
|
|
|
|
|
|
|
5 Select "Desktop app", enter whichever name you like, and click "Create".
|
|
|
|
|
|
|
|
6. Click on the "Download JSON" icon next to your newly created client ID.
|
2024-01-18 23:46:30 +01:00
|
|
|
Save the JSON file under
|
|
|
|
``<WORKDIR>/credentials/google/client_secret.json``.
|
|
|
|
|
|
|
|
7.1. If you're running the service on a desktop environment, then you
|
|
|
|
can just start the application. A browser window will open and
|
|
|
|
you'll be asked to authorize the application - you may be prompted
|
|
|
|
with a warning because you are running a personal and potentially
|
|
|
|
unverified application. After authorizing the application, the
|
|
|
|
process will save the credentials under
|
|
|
|
``<WORKDIR>/credentials/google/<list,of,scopes>.json`` and proceed
|
|
|
|
with the plugin initialization.
|
|
|
|
|
|
|
|
7.2. If you're running the service on a headless environment, or you
|
|
|
|
prefer to manually generate the credentials file before copying to
|
|
|
|
another machine, you can run the following command:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
mkdir -p <WORKDIR>/credentials/google
|
|
|
|
python -m platypush.plugins.google.credentials \
|
|
|
|
'calendar.readonly' \
|
|
|
|
[--noauth_local_webserver] \
|
|
|
|
<WORKDIR>/credentials/google/client_secret.json
|
|
|
|
|
|
|
|
When launched with ``--noauth_local_webserver``, the script will
|
|
|
|
start a local webserver and print a URL that should be opened in
|
|
|
|
your browser. After authorizing the application, you may be
|
|
|
|
prompted with a code that you should copy and paste back to the
|
|
|
|
script. Otherwise, if you're running the script on a desktop, a
|
|
|
|
browser window will be opened automatically.
|
2023-10-01 15:37:20 +02:00
|
|
|
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
scopes = ['calendar.readonly']
|
2018-05-04 19:20:23 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(scopes=self.scopes, *args, **kwargs)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-04 19:20:23 +02:00
|
|
|
def get_upcoming_events(self, max_results=10):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
Get the upcoming events. See
|
2023-10-09 01:26:50 +02:00
|
|
|
:meth:`platypush.plugins.calendar.CalendarPlugin.get_upcoming_events`.
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-04 19:20:23 +02:00
|
|
|
now = datetime.datetime.utcnow().isoformat() + 'Z'
|
2019-03-14 01:12:39 +01:00
|
|
|
service = self.get_service('calendar', 'v3')
|
2023-07-22 23:02:44 +02:00
|
|
|
result = (
|
|
|
|
service.events()
|
|
|
|
.list(
|
|
|
|
calendarId='primary',
|
|
|
|
timeMin=now,
|
|
|
|
maxResults=max_results,
|
|
|
|
singleEvents=True,
|
|
|
|
orderBy='startTime',
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
)
|
2018-05-04 19:20:23 +02:00
|
|
|
|
|
|
|
events = result.get('items', [])
|
2018-07-06 02:55:18 +02:00
|
|
|
return events
|
2018-05-04 19:20:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|