Google calendar request returns no more than 25 entries

I am trying to delete all calendar entries from today onwards. I run the request and call getEntries () as a result of the request. getEntries () always returns 25 entries (or less if the calendar has less than 25 entries). Why aren't all records returned? I expect about 80 entries.

As a test, I tried to execute the query by deleting the 25 returned records, again executing the query, deleting again, etc. It works, but there should be a better way.

The following is Java code that runs a query only once.

CalendarQuery myQuery = new CalendarQuery(feedUrl); DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'"); Date dt = Calendar.getInstance().getTime(); myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt))); // Make the end time far into the future so we delete everything myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59")); // Execute the query and get the response CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class); // !!! This returns 25 (or less if there are fewer than 25 entries on the calendar) !!! int test = resultFeed.getEntries().size(); // Delete all the entries returned by the query for (int j = 0; j < resultFeed.getEntries().size(); j++) { CalendarEventEntry entry = resultFeed.getEntries().get(j); entry.delete(); } 

PS: I looked at the Javadoc Data API and Google Data API Developers Guide . These sites are fine, but not very good. Does anyone know any additional google API documentation?

+4
source share
5 answers

You can increase the number of results with myQuery.setMaxResults() . The maximum maximum will be maximum, so you can make several queries ("paged" results) by changing myQuery.setStartIndex() .

http://code.google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setMaxResults(int) http://code.google.com/apis/gdata/javadoc/com/ google / gdata / client / Query.html # setStartIndex (int)

+9
source

Based on the answers of Jim Blackler and Chris Kaminsky, I improved my code to read the query results on the pages. I also do the deletion as a package, which should be faster than individual deletions.

I provide Java code here if it is useful to someone.

 CalendarQuery myQuery = new CalendarQuery(feedUrl); DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'"); Date dt = Calendar.getInstance().getTime(); myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt))); // Make the end time far into the future so we delete everything myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59")); // Set the maximum number of results to return for the query. // Note: A GData server may choose to provide fewer results, but will never provide // more than the requested maximum. myQuery.setMaxResults(5000); int startIndex = 1; int entriesReturned; List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>(); CalendarEventFeed resultFeed; // Run our query as many times as necessary to get all the // Google calendar entries we want while (true) { myQuery.setStartIndex(startIndex); // Execute the query and get the response resultFeed = service.query(myQuery, CalendarEventFeed.class); entriesReturned = resultFeed.getEntries().size(); if (entriesReturned == 0) // We've hit the end of the list break; // Add the returned entries to our local list allCalEntries.addAll(resultFeed.getEntries()); startIndex = startIndex + entriesReturned; } // Delete all the entries as a batch delete CalendarEventFeed batchRequest = new CalendarEventFeed(); for (int i = 0; i < allCalEntries.size(); i++) { CalendarEventEntry entry = allCalEntries.get(i); BatchUtils.setBatchId(entry, Integer.toString(i)); BatchUtils.setBatchOperationType(entry, BatchOperationType.DELETE); batchRequest.getEntries().add(entry); } // Get the batch link URL and send the batch request Link batchLink = resultFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM); CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest); // Ensure that all the operations were successful boolean isSuccess = true; StringBuffer batchFailureMsg = new StringBuffer("These entries in the batch delete failed:"); for (CalendarEventEntry entry : batchResponse.getEntries()) { String batchId = BatchUtils.getBatchId(entry); if (!BatchUtils.isSuccess(entry)) { isSuccess = false; BatchStatus status = BatchUtils.getBatchStatus(entry); batchFailureMsg.append("\nID: " + batchId + " Reason: " + status.getReason()); } } if (!isSuccess) { throw new Exception(batchFailureMsg.toString()); } 
+3
source

There is a small quote on the API page http://code.google.com/apis/calendar/data/1.0/reference.html#Parameters

Note. The query for maximum results for the Calendar is set to 25 by default, so you wonโ€™t get the entire calendar feed by accident. If you want to get the whole feed you can specify a very large number of max results.

So, to get all the events from the Google calendar feed, we do the following:

google.calendarurl.com/.../basic?max-results=999999

in the API you can also query with setMaxResults = 999999

+2
source

I came here looking for a Python solution; If someone is stuck in exactly the same way, the important line is the fourth:

 query = gdata.calendar.service.CalendarEventQuery(cal, visibility, projection) query.start_min = start_date query.start_max = end_date query.max_results = 1000 
+1
source

Unfortunately, Google is going to limit the maximum number of queries you can receive. This is done in order to keep the request rule in its recommendations (for example, HTTP requests are not allowed, for example, to more than 30 seconds). They built the whole architecture around themselves, so you could build the logic as it is.

0
source

Source: https://habr.com/ru/post/1307047/


All Articles