0
votes

Problem

I need to pull exactly 12 public events from a public calendar via Javascript on client-side, or as many public events there are. Examples:

  1. calendar has 14 total events. 3 are private, so I expect 11.
  2. calendar has 18 total events. First 3 are private. I expect 12 events, not 9.

I have been using the Google Calendar API Explorer to little avail (link will take you to API explorer with the fields I've been using filled out). There doesn't seem to be anyway to tell the API to not include private events.
Note: I am executing this API call without OAuth. Events are posted using the Google Calendar web interface, so extended properties are out of the solution I believe. My public Google Calendar ID: [email protected]

A Bad Solution

If you look at the JSON response, you'll see that events that are private have the following property and value:

"visibility": "private"

What I can do is call the API with maxResults=12 and simply filter out any private events by checking for the JSON property above.

  • If nextPageToken == null, calendar no longer has any events and the number of results retrieved is irrelevant.
  • If nextPageToken != null and after filtering out private events I am left with fewer than 12 events, I can request another call to the Google Calendar API for the next 24 events (by passing in the non-null nextPageToken). If this response's nextPageToken != null, and the sum of the number of events from the last two calls does not equal 12, I can request the next 48 events, and so on until I have 12 public events.

The problem is I may have to call the Google Calendar API several times before I get the desired number of events. And I have to do this procedure for multiple calendars (e.g., if I have 4 public google calendars and each takes an average of 3 API calls to get 12 events, I'd be executing 12 different AJAX calls just to get the data ...). It is paramount that I get 12 events or until the calendar is exhausted.

A Good Solution

I can't find a good solution to this problem. I have scrutinized all the fields in the Google APIs Explorer to absolutely zero luck. Any help would be appreciated.

1
As far as I know there is no way to filter out based on the visibility property.luc

1 Answers

0
votes

I couldn't find an answer to the 'Good Solution' I was yearning for, but I did find an acceptable compromise.

An Okay Solution

In my question, I had stated that I have to do this 'get only public events' from multiple calendars, and if each calendar took an average of 3 API calls to get X public events, I would need to call Google Calendar API 3*C, where C = number of calendars.

The Google API client library provides something called Batch Requests, which allows me to call multiple API calls in one go! Ergo, I can use batch requests to get all events for C calendars in one request, filter out any private events, make another batch request with C >= calendars, and so on.

In conclusion, I can get X public events for C calendars in just an average of 3 API requests, in lieu of 3*C requests.