Youtube v3 API "id" query parameter length - youtube-api

I am using the Youtube v3 api (video list function) to retrieve the statistics for some videos uploaded by a client. I filter the list by the video ids.
Does anyone have any idea how long the "id" parameter could be for such request? The reference only states this:
id - string - The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video's ID.

In theory 50 IDs... you technically can only query 50 videos at a time, so unless you use next page tokens and keep the IDs, you're limited to 50... Personally I wouldn't go above 50 at a time...

Related

Reduce quota from YouTube API when calling channel videos?

I've been using the YouTube Data API and I want to just call the API and for it to return a channel's latest video. Currently I have been doing this:
https://www.googleapis.com/youtube/v3/search?key=[key]&channelId=[channelId]&part=id&order=date&maxResults=1,
but that still uses many units.
Is there any way to get the data I need without using so many units?
For the first question of yours:
I want to just call the API and for it to return a channel's latest video.
the answer is positive. You'll have to proceed as follows:
Step 1: Obtain the ID of the uploads playlist corresponding to the channel of your interest:
For a given channel -- identified by its ID CHANNEL_ID --, invoke the Channels.list API endpoint, queried with its request parameter id set to CHANNEL_ID:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&fields=items/contentDetails/relatedPlaylists/uploads&id=CHANNEL_ID&key=....
The needed uploads playlist ID is to be found as the value of the property contentDetails.relatedPlaylists.uploads of the Channels resource JSON object returned by the endpoint.
Usually, a channel ID and its corresponding uploads playlist ID are related by s/^UC([0-9a-zA-Z_-]{22})$/UU\1/.
Note that above I used the fields request parameter for to get from the API only the info that's actually needed.
Also note that you'll have to invoke Channels.list as above only once, for to use the obtained uploads playlist ID as many times as you need.
Step 2: Obtain the latest uploaded video of the channel of your interest:
For that you'll need to rely on an undocumented behavior of the PlaylistItems.list API endpoint queried with its playlistId request parameter set to the ID you've obtained at step 1:
According to point 2 of this answer of mine:
For the uploads playlist of a channel, the items returned by PlaylistItems.list API endpoint are (have to be) ordered in reverse chronological order by contentDetails.videoPublishedAt.
Therefore, you'll have to invoke PlaylistItems.list on the following URL repeatedly in a loop (using pagination):
https://www.googleapis.com/youtube/v3/playlistItems?playlistId=UPLOADS_ID&part=snippet&fields=nextPageToken,items/snippet/resourceId&maxResults=50&pageToken=PAGE_TOKEN&key=...
Above, UPLOADS_ID is the ID of the uploads playlist obtained at step 1.
The request parameter pageToken=PAGE_TOKEN is needed if and only if you're not invoking PlaylistItems.list for the first time. When the endpoint is invoked the N-th time, with N >= 2, then PAGE_TOKEN would be the value of the property nextPageToken of the JSON response obtained from the previous endpoint call. If an endpoint call does not provide the property nextPageToken then the pagination loop terminates.
The pagination of the result sets of PlaylistItems.list is needed for the sake of correctness of you implementation: you'll have to ignore the snippet objects of the items array obtained from each endpoint call when the value of the property snippet.resourceId.kind is not equal with youtube#video.
For each snippet object obtained from the endpoint that has snippet.resourceId.kind equal to youtube#video, the property snippet.resourceId.videoId is set to the ID of an uploaded video belonging to the playlist identified by UPLOADS_ID.
Therefore, the first time you encounter a snippet object of this latter kind, you retain the respective video ID and break off the pagination loop (thus invoking PlaylistItems.list no more).
For what concerns your second question:
Is there any way to get the data I need without using so many units?
again, the answer is positive.
Taking into account the quota costs, executing the step 1 taken once and the step 2 taken N times amounts to a cost of 1 + N * 1 = N + 1 units. Moreover, if step 1 is not executed, then the cost would be N units. (You need not execute step 1 once you already know UPLOADS_ID from previous runs of step 1 + 2.)
Also note that your pagination loop will most likely terminate with N being 1; thus your API calls would entail most probably only 2 units of quota cost. When step 1 is not executed, the quota cost implied would most probably be only 1 unit.
the following cost 2 unit
baseurl = https://www.googleapis.com/youtube/v3
Get channel "Upload" ID
{baseurl}/channels?part=contentDetails&id={channel Id}&key={api key}
then use upload id for playlistItems
{baseurl}/v3/playlistItems?playlistId={upload id}&part=snippet&maxResults={max numbers}&key={api key}

YouTube Playlist Item API publishedAt field clarification

I have tested YouTube's PlaylistItems.list API to fetch a channel's default playlist.
In the response payload, a video -- that was published on 2020-10-14T20:22:24Z -- is in the first object, and then the next video -- that was published on 2020-10-21T17:54:05Z -- is in the second object.
In this answer it is mentioned that:
The publishedAt getting returned from a Playlist query is the date the
video was added to the playlist, rather than the date the video was
published on YouTube
What might be the reason for my case?
Is there any specific sorting order maintained for this API?
The reason you see the ordering you've exemplified (though you did not mentioned your playlist ID such that others to verify your claim) is two fold.
1. A video's publication time may well be different than its upload time
First thing is the following: according to the official docs of the Video resource's publishedAt property (the emphasis below is mine):
snippet.publishedAt (datetime)
The date and time that the video was published. Note that this time might be different than the time that the video was uploaded. For example, if a video is uploaded as a private video and then made public at a later time, this property will specify the time that the video was made public.
There are a couple of special cases:
If a video is uploaded as a private video and the video metadata is retrieved by the channel owner, then the property value specifies the date and time that the video was uploaded.
If a video is uploaded as an unlisted video, the property value also specifies the date and time that the video was uploaded. In this case, anyone who knows the video's unique video ID can retrieve the video metadata.
The value is specified in ISO 8601 format.
Therefore, in cases, it may very well be that a given video has different upload time and time of publication.
Furthermore, the official docs of PlaylistItems resource says the following w.r.t. two related properties:
snippet.publishedAt (datetime)
The date and time that the item was added to the playlist. The value is specified in ISO 8601 format.
contentDetails.videoPublishedAt (datetime)
The date and time that the video was published to YouTube. The value is specified in ISO 8601 format.
From the spec of snippet.publishedAt, it follows that, in the case of a playlist being the uploads playlist of a given channel (that is a playlist of which ID is of form UU...; note that, usually, a channel ID and its corresponding uploads playlist ID are related by s/^UC([0-9a-zA-Z_-]{22})$/UU\1/, though not documented officially), the value of snippet.publishedAt is the upload date of that video.
The second spec, indicates that the value of contentDetails.videoPublishedAt is that of the corresponding Video resource's snippet.publishedAt, the time of publication of that video.
2. For the uploads playlist of a channel, the items returned by PlaylistItems.list API endpoint are (have to be) ordered in reverse chronological order by contentDetails.videoPublishedAt
Indeed, the ordering condition seems to be true. This feature is not documented officially, but, in my experience, the statement above holds true for every result set obtained from PlaylistItems.list endpoint.
I could make here the following argument justifying the necessity that the items resulted upon the invocation of PlaylistItems.list endpoint be ordered in reverse chronological order (newest first) by contentDetails.videoPublishedAt:
This argument is of a pragmatic kind: if the result set of PlaylistItems.list is not ordered as mentioned, then this endpoint becomes useless.
This is so, since, in this case, for one to obtain the most recently published video would have to fetch locally all the uploaded items (the number of which is limited by design to 20000), for to then scan that result set for the most recent one. Being compelled to fetch all uploaded items only for to obtain the newest one is pragmatically a nonsense. If the number of uploads of a given channel exceeds the limit of 20000, then the most recent video could possibly fall outside this boundary; thus, for such kind of channels, the most recently published video could not be obtained from the API at all.
Therefore, by way of contradiction, the result set has to be ordered in reverse chronological order by contentDetails.videoPublishedAt.

How to get videos for a playlist in sorted by publish date using youtube api

I need to get daily uploaded videos in a playlist so thought to get videos for a playlist sorted by published date.
So is there any option to pass published date while fetching data for playlist?
I tried 2 options
option 1 )
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=15&playlistId=PLP-nGFpz3fa_boeLhG4m0Ie_8vmCvp5oH&key={YOUTUBE_API_KEY}
does result return from this URL is in sorted order? I found its not in sorted order.
Can I pass some date parameters related to date in this or some sort related parameters?
option2 )
I tried using "Youtube Search API"
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list
here we have date option but only can pass channelId not playlistId.
So can we pass playlistId in search API to get all videos for playlist?
Is there any other way which I can use?
Thanks
The PlaylistItems:list does not have any way to order results - the playlist order is returned in the order specified by the owner. Nor does Search:list allow you to specify a playlist.
Does the playlist owner add new videos to the beginning or end of the playlist? If the beginning, you could try fetching the first X videos and comparing the publish date to the current date.
Looking at the guide using Videos.list, nowhere does it mention that this feature is available, where the list is readily sorted upon return of the response body. Try putting the results in a container first where you will implement your own comparison of dates to sort the items. This SO thread seems to confirm this.

How many ids does the Youtube API filter permits?

If I have a list of around 3000+ ids, will the API allow me to pass all of them through the id filter category when using the videos/list endpoint
YouTube returns a max of 50 videos per page, as stated on the videos/list endpoint, so the id filter will take 50 IDs max.

youtube api v3 - get videos from a playlist with view count data ordered by number of views

I want to get all the videos from a playlist with statistics data like viewCount and the videos should be ordered by number of video views. So far I have managed the following:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUbW18JZRgko_mOGm5er8Yzg&key={YOUR_KEY_HERE}&maxResults=50
In the youtube api v2 we can pass part=statistics to get the view count but this is not working with v3 playlists. How can I get the above the video data in the above mentioned format.
From the results of playlistItems.list call, collect the video ids, make a comma-separated list and set it as id parameter of videos.list call. That call will give you the data you need.

Resources