Search code examples
python-3.xgoogle-apigoogle-calendar-api

Filter google calendar event list with the query parameter "q"


I'm trying to filter a list of recently updated events from my Google Calendar with the parameter q= "status ='confirmed'" but it returns nothing.

I first had my block of code like this :

def main():
    watchEvents(monday) # monday is in isoformat : "2021-09-13T00:00:00Z"

def watchEvents(day):
    page_token = None
    while True:
        events = service.events().list(calendarId='primary', pageToken=page_token, updatedMin=day).execute()
        for event in events['items']:
            print (event['summary'])
        page_token = events.get('nextPageToken')
        if not page_token:
            break

But the parameter updatedMin=day outputs an error Traceback (most recent call last): print (event['summary']) KeyError: 'summary', because cancelled events don't have those properties. Here is an example of a cancelled event when I try the API on the google site:

"items": [
  {
   "kind": "calendar#event",
   "etag": "\"xxxxxxx\"",
   "id": "xxxxx",
   "status": "cancelled"
  }

I don't want those type of events where the status are "cancelled" so I tried the parameter q= "status ='confirmed'" in my code :

events = service.events().list(calendarId='primary', pageToken=page_token, updatedMin=day, q= "status ='confirmed'").execute()

But now it returns an empty list, even when I try the API on the google site :

{
 "kind": "calendar#events",
 "etag": "\"xxxxx\"",
 "summary": "FirstName Name private",
 "updated": "2021-09-16T10:04:02.008Z",
 "timeZone": "Europe/Brussels",
 "accessRole": "owner",
 "defaultReminders": [
  {
   "method": "popup",
   "minutes": 30
  }
 ],
 "nextSyncToken": "XXXXXXXXX=",
 "items": []
}

So my questions are :

  • What is the syntax to filter the status of the events ?
  • Or is there another way to get a list of the last updated items?

Solution

  • Answer:

    The Google Calendar API always returns cancelled events for events: list when either updateMin or syncToken are specified in the request. If you wish to remove updated items that have been deleted, this will need to be done locally.

    More Information:

    As per the documentation on the event resource: (emphasis my own):

    Property name Value Description Notes
    status string Status of the event. Optional. Possible values are:
    • "confirmed" - The event is confirmed. This is the default status.
    • "tentative" - The event is tentatively confirmed.
    • "cancelled" - The event is cancelled (deleted). The list method returns cancelled events only on incremental sync (when syncToken or updatedMin are specified) or if the showDeleted flag is set to true. The get method always returns them.
    writable

    and from events: list:

    Parameter name Value Description
    updatedMin datetime Lower bound for an event's last modification time (as a RFC3339 timestamp) to filter by. When specified, entries deleted since this time will always be included regardless of showDeleted. Optional. The default is not to filter by last modification time.

    You can use a syncToken to retrieve updates from the last time you made a sync request, but even then cancelled events will be returned.

    Additionally, it appears that the event status can't be specified using the q parameter to have results filtered in the response.