Skip to main content

A python library for syncing Google Calendar to local storage

Project description

An asyncio python library for the Google Calendar API. This library provides a simplified Google Calendar API that is lighter weight and more streamlined compared to using aiogoogle, and increased reliability by supporting efficient sync and reading from local storage. See the API Documentation.

The focus of this API is on making it simple to access the most relevant parts of Google Calendar, for doing useful things. It may not support everything in the API however it should be easy to extend to do more as needed.

Quickstart

In order to use the library, you'll need to do some work yourself to get authentication credentials. This depends a lot on the context (e.g. redirecting to use OAuth via web) but should be easy to incorporate using Google's python authentication libraries. See Google's Authentication and authorization overview for details.

You will implement gcal_sync.AbstractAuth to provide an access token. Your implementation will handle any necessary refreshes. You can invoke the service with your auth implementation to access the API.

from gcal_sync.auth import AbstractAuth


class MyAuthImpl(gcal_sync.AbstractAuth):

    def __init__(self, websession: aiohttp.ClientSession) -> None:
        """Initialize MyAuthimpl."""
        super().__init__(websession)

    async def async_get_access_token(self) -> str:
        """Return a valid access token."""
        return ...


service = GoogleCalendarService(MyAuthImpl(...))
calendar = await service.async_get_calendar("primary")

See gcal_sync.api.GoogleCalendarService for more details on API calls and see the overall documentation

Fetching Events

Events can be fetched using the gcal_sync.api.ListEventsRequest which can filter events based on time or search criteria. The GoogleCalendarService supports paging through events using an aync generator like in this example below:

from gcal_sync.api import ListEventsRequest

request = ListEventsRequest(
    calendar_id=calendar.id,
    search="Holiday",
)
result = await service.async_list_events(request)
async for result_page in result:
    for event in result_page.items:
        print(event.summary)

Using the async generator avoids the need to manually handle paging and page tokens, but that is also available if needed. Recurring events are expanded on the server by default, so you don't have to worry about handling them yourself.

Synchronization

If you require a high read rate to the events, then it may be more efficient to first sync down the calendar then query local events. Any recurring events are expanded at query time by the local library by interpreting the recurrence rules on the synced event.

from gcal_sync.sync import CalendarListSyncManager

sync = CalendarEventSyncManager(service)
# Run when you want to sync down the latest set of events
await sync.run()

# Iterate over events in the local store
timeline = await sync.store_service.async_get_timeline()
for event in timeline:
    print(event.summary)

See gcal_sync.sync for more details.

Development Environment

$ python3 -m venv venv
$ source venv/bin/activate
$ pip3 install -r requirements_dev.txt

# Run tests
$ py.test

# Run tests with code coverage
$ py.test --cov-report=term-missing --cov=gcal_sync

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gcal_sync-6.2.0.tar.gz (40.9 kB view details)

Uploaded Source

Built Distribution

gcal_sync-6.2.0-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

Details for the file gcal_sync-6.2.0.tar.gz.

File metadata

  • Download URL: gcal_sync-6.2.0.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for gcal_sync-6.2.0.tar.gz
Algorithm Hash digest
SHA256 33ab2d39127ebcb4ac1c3672b5c4bd2fd6d3eb0560ac0f4b4a71bfa9a920d34c
MD5 978de4dfee48e02b72c6034c3f91537f
BLAKE2b-256 fcb7c7c4d9a4b3e87e9a8a9479684e32b39c032ad1c0f2d40ed2d5ed64c52f1f

See more details on using hashes here.

File details

Details for the file gcal_sync-6.2.0-py3-none-any.whl.

File metadata

  • Download URL: gcal_sync-6.2.0-py3-none-any.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for gcal_sync-6.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3feb4c2eec5472f8321908377e8d3c482fd0411c650b094c2c1aa5fc177db75
MD5 77261e4a32c2fb1f54fbe3c3d3837977
BLAKE2b-256 3619c5d1b79e0888fb33cd7e95de986ce6e001ac42287a0f840bb4e1e091ea5b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page