How can I query YouTube API to get videos from auto-generated 'Topic' channels? - youtube

How can I input search parameters using the YouTube API so that I only get video ID's from topic channels? For instance, if I go to youtube.com and manually search "Hello Adele - Topic" I correctly get the song, 'Hello' from the 'Adele - Topic' channel as my first search result, https://www.youtube.com/watch?v=_WS9w10ygpU.
However, if I perform the same query using the YouTube Data API, with the parameters:
part='snippet'
q='Hello Adele - Topic'
I won't get back the ID for the correct video. Is there any way to get this programmatically?

As stated in this thread, auto generated channels have no videos. They have only playlists with videos from other channels. So you have to look for playlists. You may check the sample request in this link.
Here's the URL sample of auto-generated Topic-based Id which grabs its playlist id:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=HC9m3exs6zk1U&fields=items%2Fid&key={YOUR_API_KEY}
//Outputs sample playlist Id: LP9m3exs6zk1U
Now here's the URL sample using that playlist Id to get the videos from the auto-generated Topic-based channel Id:
GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=5&playlistId=LP9m3exs6zk1U&key={YOUR_API_KEY}
//Outputs video data you want.
Hope this helps.

Related

YouTube Api v3: calculate youtube playlist length

How can i get a youtube playlist duration (the sum of all it's videos durations).
I used this endpoint to get the playlist videos :
https://youtube.googleapis.com/youtube/v3/videos?playlistId
and it's return the videos object with all videos ids, in this case i need to make an api call to get each video length with separate api call to :
https://youtube.googleapis.com/youtube/v3/videos
I searched for a solution for this and found that we can make a call to previous api with all ids each id with a query param ex. id=aaaaa&id=bbbbb, but i need to be the owner of these videos to make the request.
Is there any way to get all videos with one call and without being the owner of the videos ?

Youtube RSS feed for live streams

I want to get the RSS feed for all the videos of a channel, including live streams. I know about
https://www.youtube.com/feeds/videos.xml?channel_id=
But this doesn't include "Past live streams" videos from the channel, only "Uploads" videos. How can you get live streams too in the feed?
If you really need the info as RSS you could follow any of these options:
Use "PlaylistItems" endpoint (or the endpoint available at https://yt.lemnoslife.com/) for get the items form the uploads playlist of the given channel - see working example from YouTube Data API - see this answer for more info about the "Uploads" playlist - check also the response from yt.lemnoslife.com1, then, build an RSS feed programatically.
(If you decide to generate the RSS feed with an RSS generator example: FetchRSS - or any other tool), then, you can copy and paste the URL of the "Uploads" playlist of the given channel - following your channel sample, the link is: https://www.youtube.com/playlist?list=UU0nggDVchmmO7ikV4NosYhg and let the RSS generator of your choice builds the RSS.
Use this endpoint - https://www.youtube.com/feeds/videos.xml?playlist_id=<PLAYLIST_ID> - credits to this comment - in your use case, that would be: https://www.youtube.com/feeds/videos.xml?playlist_id=UU0nggDVchmmO7ikV4NosYhg - notice the value of the playlist_id parameter - it's the "Uploads" of the channel "UC0nggDVchmmO7ikV4NosYhg".
1 I found this website on this answer.

How list all videos from a YouTube channel?

https://www.googleapis.com/youtube/v3/channels?id=UC_x5XG1OV2P6uZZ5FSM9Ttw&part=snippet%2CcontentDetails%2Cstatistics&key={YOUR_API_KEY}
Using this API link, I get a list of videos from start but not all videos. I only get 300 to 500 videos.
This is a bit complicated. You have to make a few calls instead.
find the channel ID of the channel you want.
list playlists (youtube.channels.list set id to channelId and set part to contentDetails)
find the ID of the playlist with name uploads
list playlist items (youtube.playlistItems.list set playlistId and set part to snippet optionally set maxResults to 50)
Page through results using nextPageToken
Found this from here https://stackoverflow.com/a/27872244/975887
Hope this helps.

Get all videos for a given user

I'm trying to find a way to retrieve all videos posted by a given user (eg: https://www.youtube.com/user/laliga/videos).
All the examples I've found online or the doc (https://developers.google.com/youtube/v3/docs/) seems to require either a playlist ID, channel ID or video ID.
Is there any way I can directly query the author's videos or am I forced to retrieve all playlists and then iterate over?
Thanks
You just need to map the user name to its channelID first via the YouTube API like so:
https://www.googleapis.com/youtube/v3/channels?forUsername=USER_NAME&part=id&key=API_KEY
Full docs: https://developers.google.com/youtube/v3/docs/channels/list
Then you can make a Videos:List call with that channelID to get the videos.
1)take your google key id and channel id and put inside below url
https://www.googleapis.com/youtube/v3/search?key=APIKEY&channelId=CHANNELID&part=snippet,id&order=date&maxResults=20
ex
https://www.googleapis.com/youtube/v3/search?key=adsjsdakhkjshd&channelId=sdahghsadhgj&part=snippet,id&order=date&maxResults=20

How do I get a list of uploaded videos for a certain channel with the new YouTube Data API (V3)?

I am trying to get a list of video ids for all uploaded videos to a channel. I would also like to use the new version of the YouTube Data API (V3). How do I do this?
You have to get the upload playlist id to get each videos uploaded. To get that, you need to get the channel id. After you have the playlist id from the channel id, it is pretty simple. I have written out the steps for all three below.
Also, we offer PubSubHubBub which allows you to be alerted every time a new video is added to a channel, or you could use SUP (V2) to see which resources have changed before making the calls.
Instructions to get video ids for all uploaded videos for a channel in V3
Get the channel id for the channel you want (you probably only need to do this once, then you can save it)
Use search.list
Set type to channel
Set q to the name of the channel you want
Grab the channel id (something like this: "channelId": "UC0X2VuXXXXXXXXXXXXXXXX")
Get the playlist id for the channel uploads using the channel id from step 1 (you probably only need to do this once, then you can save it)
Use channels.list
Set id to UC0X2VuXXXXXXXXXXXXXXXX from step 1
Grab the uploads key from contentDetails (something like this: "uploads": "UU0XXXXXXXXXXXXXXXXXXXX")
Get the videos via the playlistitems in the playlist using the playlist id from step 2
Use playlistItems.list
Set playlistId to UU0XXXXXXXXXXXXXXXXXXXX from step 2
Go through each PlaylistItem and pull out the video id
In the meantime, there is a much easier way:
Use the channels.list
set forUsername = [CHANNELNAME]
set part = contentDetails
grab $data->items[0]->contentDetails->relatedPlaylists->uploads --> [PLAYLISTID]
Use playlistItems.list
Set playlistId = [PLAYLISTID] from step 1
Go through each PlaylistItem and pull out the video ids

Resources