Get Video Views for Group of YouTube Videos - youtube-api

I can get a list of videos from a channel I'm querying. I have most of the information I need in an existing api call, but I'd really like to also get the number of times each video has been viewed.
My current query gives me 50 results:
https://www.googleapis.com/youtube/v3/search?part=id,snippet&key=' . $key . '&channelId=' . $channelId . '&order=date&maxResults=50&publishedBefore=' . $publishedBefore
I read I can get viewCounts through the API by providing a video id. But that's one at a time. I'm hoping I can ammend my query above to also output the viewCounts to make 1 query instead of 51 queries.
I have a part attribute that I can set to "a comma-separated list of one or more search resource properties that the API response will include." The examples I've seen have "id,snippet", but can I specify a value that will provide how many times the video has been viewed? I can't find a list of search resource properties.
Also, is there a way to view a channel's "hottest" or "trendiest" videos that they currently have?

You can make one single request which get statistics from youtube API. You need to call API like this
https://content.googleapis.com/youtube/v3/videos?id=Ks-_Mh1QhMc,Cc0KYU2j0TM4,CeIho2S0ZahI&part=statistics&key={APIKEY}
In this request I am getting data from 3 videoId, you can get upto 50 videos at a time.

Related

YouTube API - get tags for all videos with playlist query

I'm querying YouTube channels to retrieve playlist metadata like this:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=5&playlistId={PLAYLIST_ID}&key={API_KEY}
This query returns an array of all of the videos on the channel. Each object in that array includes various fields that provide metadata on each video, but none of these fields include the tags associated with the video. I can get that data using a query like this:
https://www.googleapis.com/youtube/v3/videos?key={API_KEY}&fields=items(snippet(title,description,tags))&part=snippet&id={VIDEO_ID}
The problem with that is that now I need to issue a separate query for every single video that comes back in the first query.
So, my question is, is there a way that I can get these tags included as part of the initial JSON object from the first query? Can I add any parameters that will request this data be included with that response?
Thanks for any help!
Short answer: No.
The PlaylistItems: list documentation does not show a way to get video tags from the playlistItems endpoint. The only parts available are id, snippet, status and contentDetails and neither of those contain tags.
However!
You do not have to make a request for each video in the playlist! The documentation states:
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. (string)
That means you can supply multiple, comma-separated video ids to the videos endpoint.
Example:
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=kOkQ4T5WO9E,a59gmGkq_pw,Io0fBr1XBUA&key={YOUR_API_KEY}
So in total, it will cost you two requests to get the tags of all videos in a playlist.*
*In practice, you might have to make more than two requests. If I remember correctly, YouTube limits the returned items to 50 per request. Thus, if the playlist contains more than 50 videos, you will have to make another request with the pageToken parameter set.

youtube v3 api : get all live streaming playlist ids of a channel

I have two "Live streaming/live broadcast saved playlists" in following youtube channel --> https://www.youtube.com/user/swaminarayanlive.
I am trying to retrieve all the "live streaming / live broadcast playlists" of a channel using new youtube v3 api by using the below link-->
https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCBkNpeyvBO2TdPGVC_PsPUA&key={YOUR_API_KEY}
here i can get the info of playlists which are not live streams or live broadcast. and not able to get the same for the live one.
Please help me how can i get that for live one using youtube v3 api
I was researching another issue with the API when finding this, and when I saw that this was never resolved, I decided to look into it. It turns out that this is related to that other issue.
The YouTube API v3 lacks support for saved playlists. The channel swaminarayanlive did not create the playlists, only saved them from the channel BhujMandir.
The workaround in your case would be to retrieve the playlists of BhujMandir and extract the response snippets with "title" parameters containing the word "Live".
You would need to go through the pages and search each one for this, since there is a limit to how many results an API response can show at once. Currently it's 50.
So, you would use the following to get the first page of playlistItems:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCVItNtUctAknegvmYcMhUQg&maxResults=50&key={YOUR_API_KEY}
This will return an API response containing the properties "kind" (which will have the value "youtube#playlistListResponse") and "etag". If there are more than 50 results, there will also be a "nextPageToken" property. (On a page that is not the first, there would also be a "prevPageToken" property.)
After these properties, there are two blocks. One called "pageInfo", containing info about how many results (playlists) there are and how many are shown per page, and one block called "items", containing the resulting playlistItems.
You would look through the items block for any playlistItem with a title property (which is a string) containing the substring "Live" and get the id properties of those. You would then look on the next page by using the nextPageToken's value in a new HTTP request, like so:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCVItNtUctAknegvmYcMhUQg&maxResults=50&pageToken=[nextPageToken_value_here]&key={YOUR_API_KEY}
As of now, the two playlists you're looking for are on the pages with tokens CJYBEAA and CMgBEAA.
I think that where is the {YOUR_API_KEY} you should change this code and insert the name of your application program interface

Get list of videos including private/unlisted using contentOwnerId

I was using playlistitems to get the authenticated users' video list (including private/unlisted).
Now I tried contentOwnerId. I can get private video info using onBehalfOfContentOwner, but there is no onBehalfOfContentOwner key for playlistitems.
Is there other way to get the full list of an owned channel?
I'm fairly certain that the expected way of doing this is via search.list(part=snippet, forContentOwner=true, onBehalfOfContentOwner=CONTENT_OWNER_ID, type=video, maxResults=50) and optionally including channelId=UC... if you just want to get the videos in a specific channel you manage. You'd have to request multiple pages if there are more than 50 videos. I just tested that out and it does appear to return private as well as public videos.
Yes, it's kind of odd that this is done via search.list() and not playlistItems.list()...

Tags (keywords) are missing from the Youtube API video feed when using category search

When I make an authorized call for Youtube videos, I get all the tags/keywords in the feed.
http://gdata.youtube.com/feeds/api/users/default/uploads?alt=jsonc&v=2
However, if I want to filter this result by a single keyword/category, the tags are not returned by the API. Example, I want to reduce the number of videos in the feed by tag "English"/"French".
http://gdata.youtube.com/feeds/api/users/default/uploads/-/English?alt=jsonc&v=2
Is this expected behavior or a bug?
It's expected behavior; this blog post explains how your second request is going against the search index, which will never return keywords/tags in the response.

YouTube API "published" filter doesn't seem to work

I'm trying to use the YouTube API to return videos that were recently published, but the filter I'm using doesn't seem to work as expected.
This API call only returns two videos whereas there should be tons more that were published after March 1st:
https://gdata.youtube.com/feeds/api/videos?q=&fields=entry[xs:dateTime(published)%20%3E%20xs:dateTime('2013-03-01T12:00:00.000Z')]
However, if I add a query string, then many more results are returned. For example:
https://gdata.youtube.com/feeds/api/videos?q=surfing&fields=entry[xs:dateTime(published)%20%3E%20xs:dateTime('2013-03-01T12:00:00.000Z')]
Anyone know why? Is there another approach I should be using to just get me the latest videos published regardless of query string?
I understand your confusion, but that's not what the fields= parameter is used for. The documentation should hopefully clear things up, but to summarize, using fields= in that manner is equivalent to making a request without the fields= parameter and then filtering the results of that request so that it only includes the entries that match your filter.
So if your request without fields= would normally return 25 specific videos, adding fields= to it will give you a response that includes somewhere between 0 and 25 videos—all the non-matching videos are filtered out.
You can request a feed of recently published videos without any other filters using http://gdata.youtube.com/feeds/api/videos?v=2&orderby=published

Resources