Why do I get inconsistent results consuming the v3 YouTube API? - youtube

As far as I can tell, I'm paging through API results as I should:
Make a request
Get a result back containing the 'totalResults' and the 'nextPage' token
Make the same request, adding the 'pageToken' parameter
Some issues I'm having:
If I make any request multiple times, I'll often get one of two different 'totalResults' values
If I page through and grab all the results for various queries, I'll get different numbers of items
Here's a set of queries followed by their 'nextPage' and 'totalResults' values:
https://www.googleapis.com/youtube/v3/search?key=~key~&part=id&channelId=UC8hzWaP0JfFJ8a1UjLIl1FA&maxResults=50&publishedAfter=2016-02-23T14%3A32%3A58.928-06%3A00&publishedBefore=2017-02-24T14%3A32%3A58.928-06%3A00&type=video
CDIQAA/239
https://www.googleapis.com/youtube/v3/search?key=~key~&part=id&channelId=UC8hzWaP0JfFJ8a1UjLIl1FA&maxResults=50&pageToken=CDIQAA&publishedAfter=2016-02-23T14%3A32%3A58.928-06%3A00&publishedBefore=2017-02-24T14%3A32%3A58.928-06%3A00&type=video
CGQQAA/188
https://www.googleapis.com/youtube/v3/search?key=~key~&part=id&channelId=UC8hzWaP0JfFJ8a1UjLIl1FA&maxResults=50&pageToken=CGQQAA&publishedAfter=2016-02-23T14%3A32%3A58.928-06%3A00&publishedBefore=2017-02-24T14%3A32%3A58.928-06%3A00&type=video
CJYBEAA/188
https://www.googleapis.com/youtube/v3/search?key=~key~&part=id&channelId=UC8hzWaP0JfFJ8a1UjLIl1FA&maxResults=50&pageToken=CJYBEAA&publishedAfter=2016-02-23T14%3A32%3A58.928-06%3A00&publishedBefore=2017-02-24T14%3A32%3A58.928-06%3A00&type=video
null/239
The first three queries contained 50 items, and the last contained 18, so I got 168 items total. This is really frustrating since I don't know if any of the three counts is the correct count.
Again, if I put any one query in my browser and hit 'refresh' over and over, I'll get either 188 or 239.

Related

Youtube Data API V3 Number of results not equal to maxResults/pageInfo.resultsPerPage

Good day. I'm trying to get the top videos for gaming within the specified publish dates ordered by view count. I'm having a problem with the number of items that it is returning in the JSON response because it's less than 50 items even though I have set the maxResults parameter to 50 and the JSON response returns pageInfo.resultsPerPage equal to 50. The pageInfo.totalResults also returns a lot more than 50 so it should be displaying 50 items in the result. As an example, I'm using the followng URL
https://www.googleapis.com/youtube/v3/search?key={API_Key}&part=snippet&maxResults=50&order=viewCount&publishedAfter=2017-02-01T00%3A00%3A00Z&publishedBefore=2017-02-01T00%3A01%3A59Z&type=video&videoCategoryId=20
The returned result here the last time I ran this query has 20 items even though there are 161,307 total results. I would like to ask a solution that will make sure that I will always get the number of items equal to the maxResults if possible. Hope someone can help me on this. Thank you very much.
EDIT: I know how to use the page token, but I'm not aiming for getting the items that I need for more than one request (as the Data API have limited credits per day). The issue that I'm trying to resolve is to make sure that I always get 50 items everytime I invoke the request.
Although there are 160k+ total results, your publishedAfter/publishedBefore filters cut the number returned down to the videos published in that time range, which is 20.
Well, I got 50 results with the URL request that you provided. Just make sure that you place right value on the parameter that you use, especially the value in publishedAfter. You can also use the parameter pageToken to get the next page or next 50 results.
For more information, check this SO question on how to use pageToken

Why limited number of next page tokens?

Through a script I can collect a sequence of videos that search list returns. The maxresults variable was set to 50. The total number items are big in number but the number of next page tokens are not enough to retrieve all the desired results. Is there any way to take all the returned items or it is YouTube restricted?
Thank you.
No, retrieving the results of a search is limited in size.
The total results that you are allowed to retrieve seems to have been reduced to 500 (in the past it was limited to 1000). The api does not allow you to retrieve more from a query. To try to get more, try using a number of queries with different parameters, like: publishedAfter, publishedBefore, order, type, videoCategoryId, or vary the query tags and keep track of getting different video id's returned.
See for a reference:
https://code.google.com/p/gdata-issues/issues/detail?id=4282
BTW. "totalResults" is an estimation and its value can change on the next page call.
See: YouTube API v3 totalResults field is returning 1 000 000 when it shoudn't

Max-results value is too high.YouTube API

I'm trying to load the first 100 videos of a YouTube channel and I can only load 50 videos. I always get this error message
Max-results value is too high. Only up to 50 results can be returned per query.
and I'm using this URL.
http://gdata.youtube.com/feeds/api/users/shaytards/uploads?&max-results=100
I'm not sure if I need any kind of developer key to load 100 results. Even if you know a way to load videos 75-100 would be great but any help would be appreciated.
The Youtube data API requires you to set up your own pagination if you need to get more than 50 results. In this case, then, you would set max-results to 50 and do the query, then on your page have some sort of "next" button (or however you want to implement your pagination) that would trigger a new call (either through ajax, a new page, or whatever your own workflow is). This new call would be to the same data feed, but you'd append the parameter &start-index=51 so that you'd get 50 more results, numbered 51-100. You can continue on like this up to 1000 results.
See https://developers.google.com/youtube/2.0/reference#Paging_through_Results for more details.
In the YouTube Data API V3 it is a little different. Each search query returns a "nextPageToken" that can be used in a subsequent query for more results. Documentation: https://developers.google.com/youtube/v3/guides/implementation/pagination

Accessing an item beyond start_index=1000 in a YouTube user upload feed

I am currently trying to pull data about videos from a YouTube user upload feed. This feed contains all of the videos uploaded by a certain user, and is accessed from the API by a request to:
http://gdata.youtube.com/feeds/api/users/USERNAME/uploads
Where USERNAME is the name of the YouTube user who owns the feed.
However, I have encountered problems when trying to access feeds which are longer than 1000 videos. Since each request to the API can return 50 items, I am iterating through the feed using max_length and start_index as follows:
http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?start-index=1&max-results=50&orderby=published
http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?start-index=51&max-results=50&orderby=published
And so on, incrementing start_index by 50 on each call. This works perfectly up until:
http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?start-index=1001&max-results=50&orderby=published
At which point I receive a 400 error informing me that 'You cannot request beyond item 1000.' This confused me as I assumed that the query would have only returned 50 videos: 1001-1051 in the order of most recently published. Having looked through the documentation, I discovered this:
Limits on result counts and accessible results
...
For any given query, you will not be able to retrieve more than 1,000
results even if there are more than that. The API will return an error
if you try to retrieve greater than 1,000 results. Thus, the API will
return an error if you set the start-index query parameter to a value
of 1001 or greater. It will also return an error if the sum of the
start-index and max-results parameters is greater than 1,001.
For example, if you set the start-index parameter value to 1000, then
you must set the max-results parameter value to 1, and if you set the
start-index parameter value to 980, then you must set the max-results
parameter value to 21 or less.
I am at a loss about how to access a generic user's 1001st last uploaded video and beyond in a consistent fashion, since they cannot be indexed using only max-results and start-index. Does anyone have any useful suggestions for how to avoid this problem? I hope that I've outlined the difficulty clearly!
Getting all the videos for a given account is supported, but you need to make sure that your request for the uploads feed is going against the backend database and not the search index. Because you're including orderby=published in your request URL, you're going against the search index. Search index feeds are limited to 1000 entries.
Get rid of the orderby=published and you'll get the data you're looking for. The default ordering of the uploads feed is reverse-chronological anyway.
This is a particularly easy mistake to make, and we have a blog post up explaining it in more detail:
http://apiblog.youtube.com/2012/03/keeping-things-fresh.html
The nice thing is that this is something that will no longer be a problem in version 3 of the API.

Delay and Inconsistent results using Twitter search API when using "since_id" parameter

We've noticed what seems to be a delay and/or inconsistent results using the Twitter Search API when specifying a sinceid in the param clause. For example:
http://search.twitter.com/search?ors=%23b4esummit+#b4esummit+b4esummit&q=&result_type=recent&rpp=100&show_user=true&since_id=
Will give the most recent Tweets, but:
http://search.twitter.com/search?ors=%23b4esummit+#b4esummit+b4esummit&q=&result_type=recent&rpp=100&show_user=true&since_id= 12642940173
will often not give tweets that are after that ID for several hours (even though they're visible in the first query)...
anyone have similar problems?
First off, those are not Twitter search API URLs. You should be querying the API like this:
http://search.twitter.com/search.json?q=%23b4esummit%20OR%20#b4esummit%20OR%20b4esummit&result_type=recent&rpp=100&show_user=true
Second, since_id cuts off from the bottom of the list. You can see the behavior illustrated in this documentation: https://dev.twitter.com/docs/working-with-timelines
For an example, at the time of this writing, the above URL returns 31 entries. Picking the ID of a Tweet in the middle of that list, I constructed:
http://search.twitter.com/search.json?q=%23b4esummit%20OR%20#b4esummit%20OR%20b4esummit&result_type=recent&rpp=100&show_user=true&since_id=178065448397574144
Which only returns 12 entries, which match the top 12 entries of the first URL.

Resources