Sanico
Sanico

Reputation: 39

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 :

Upvotes: 0

Views: 2544

Answers (2)

tnetennba31
tnetennba31

Reputation: 11

The q parameter works somewhat strangely, in that you set it to a string that serves as a keyword which gets searched for in the following fields of the events: summary, description, location, attendee's displayName, attendee's email.

So, let's say you want to get events that take place at the location Times Square. You would use the following sort of statement:

events = service.events().list(calendarId='primary', pageToken=page_token, q='Times Square').execute()

Now hopefully you don't have an event taking place in Paris that has an attendee with the display name Times Square, or else that event will also be included in the events result set -- since, sadly, you can't specify which particular field q's search should apply to.

Upvotes: 1

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

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.

Upvotes: 1

Related Questions