Returning duration while listing playlistItems? - youtube

I'm using this url:
https://www.googleapis.com/youtube/v3/playlistItems?
playlistId=FLFe0SGNFqZ9E2owO5ZDZpeg&
part=snippet,contentDetails,status
to fetch YouTube playlistItems.
I included all the part I'm interested in, however I found no way to return the duration of the item.
https://developers.google.com/youtube/v3/docs/playlistItems/list
Is this possible?
currently I'm getting back only an item like this:
{
"status": {
"privacyStatus": "public"
},
"kind": "youtube#playlistItem",
"contentDetails": {
"videoId": "tL-Ba86UhoE"
},
"snippet": {
"playlistId": "FLFe0SGeFqZ9E2owO5ZDZpwg",
"thumbnails": {
"default": {
"url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/default.jpg",
"width": 120,
"height": 90
},
"high": {
"url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/hqdefault.jpg",
"width": 480,
"height": 360
},
"medium": {
"url": "https://i1.ytimg.com/vi/tL-BA86Uhoh/mqdefault.jpg",
"width": 320,
"height": 180
}
},
"title": "Music Video",
"resourceId": {
"kind": "youtube#video",
"videoId": "tL-BA86Uhoh"
},
"channelId": "UCFe0SGNFqZ9E2owO5ZDZpwg",
"publishedAt": "2013-07-06T18:41:43.000Z",
"channelTitle": "channeltitle",
"position": 0,
"description": "Video for"
},
"etag": "\"ePFRUfYBkeQ2ncpP9OLHvB0fDw4/CJiCG6tvFw4quQlzJq3gTrhvCNo\"",
"id": "FL-fX6VqgtTfCJWKNE4aVRODKSrRgEdGvw"
},
With no time indication at all.
I'd rather avoid flooding YouTube with request to return a single video's duration.

Use link like this for get duration for many videos:
https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=idFirst,idSecond&key=YOUR_API_KEY

Use this Function : [ only used with Youtube Api v3 ]
Browser :
you have to make another http request
using :
https://www.googleapis.com/youtube/v3/videos?id={video id}&part=contentDetails&key={api Key}
and on
contentDetails
you will find the duration
Php :
function getDuration($apiKey,$video_id){
$link='https://www.googleapis.com/youtube/v3/videos?id='.$video_id.'&part=contentDetails&key='.$apiKey;
$data = json_decode(file_get_contents($link),true);
$duration = $data['items'][0]['contentDetails']['duration'];
return $duration;
}

Related

Youtube API Playlists not returning the Playlist

I am using the Playlists endpoint of Youtube's API to get the playlists from a specific channel but it doesn't work for the following channels:
https://www.youtube.com/channel/UCX5fyLRcU05DMa2OVu-qFEQ
https://www.youtube.com/channel/UCwm2mdiUXApmL5bCxHtgJIA
The query I am using:
https://www.googleapis.com/youtube/v3/playlists?channelId=UCwm2mdiUXApmL5bCxHtgJIA&part=snippet&order=date&maxResults=50&key=YOUR_API_KEY
https://www.googleapis.com/youtube/v3/playlists?channelId=UCX5fyLRcU05DMa2OVu-qFEQ&part=snippet&order=date&maxResults=50&key=YOUR_API_KEY
It doesn't return the videos when I try to target the playlists in these channels using the specif Playlist ID as well. I can see that these channels, playlists and videos are public and searchable.
Is there any reason why the API shouldn't return this playlist that I am missing?
Thank you for your help.
Bartu
If you check the playlist page of the channel you provided, you'll see that all playlists posted there are not really from the channel - those were added/generated by YouTube itself.
Example:
https://www.youtube.com/playlist?list=OLAK5uy_nvZSUlZB8qwifPgIVCoEulF4VyErdBX48
A normal playlist (that belongs to the channel) should have its ID as follows:
PL3tRBEVW0hiB5KKP9gs_AkW8J4dOvbdPN
By using YouTube Data API, you can test the results:
URL: https://youtube.googleapis.com/youtube/v3/playlists?part=id%2Csnippet%2Cstatus&id=OLAK5uy_nvZSUlZB8qwifPgIVCoEulF4VyErdBX48&key=[YOUR_API_KEY]
Results:
{
"kind": "youtube#channelListResponse",
"etag": "Yw1yJOOwI7LJ8LeYmvQ8wpyHWbU",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#channel",
"etag": "wDhMSGEntArTZuX56hjNjytDUlU",
"id": "UCFtEEv80fQVKkD4h1PF-Xqw",
"snippet": {
"title": "Microsoft",
"description": "Innovation is a key ingredient in Microsoft’s mission: to empower every person and organization on the planet to achieve more. \n\nSubscribe to Microsoft's official YouTube channel to explore technology’s role in making the world more sustainable, inclusive, and innovative for all.\n\n",
"customUrl": "microsoft",
"publishedAt": "2006-02-13T05:21:44Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AKedOLRZENZBPyEEG2cd0PZXemKKyG1GMGVJXmzHhrs4=s88-c-k-c0x00ffffff-no-rj",
"width": 88,
"height": 88
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AKedOLRZENZBPyEEG2cd0PZXemKKyG1GMGVJXmzHhrs4=s240-c-k-c0x00ffffff-no-rj",
"width": 240,
"height": 240
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AKedOLRZENZBPyEEG2cd0PZXemKKyG1GMGVJXmzHhrs4=s800-c-k-c0x00ffffff-no-rj",
"width": 800,
"height": 800
}
},
"localized": {
"title": "Microsoft",
"description": "Innovation is a key ingredient in Microsoft’s mission: to empower every person and organization on the planet to achieve more. \n\nSubscribe to Microsoft's official YouTube channel to explore technology’s role in making the world more sustainable, inclusive, and innovative for all.\n\n"
},
"country": "US"
},
"contentDetails": {
"relatedPlaylists": {
"likes": "",
"uploads": "UUFtEEv80fQVKkD4h1PF-Xqw"
}
},
"status": {
"privacyStatus": "public",
"isLinked": true,
"longUploadsStatus": "longUploadsUnspecified"
}
}
]
}
When you query a normal playlist (that belongs to the channel), you will get these results:
URL: https://youtube.googleapis.com/youtube/v3/playlists?part=id%2Csnippet%2Cstatus&id=PL3tRBEVW0hiB5KKP9gs_AkW8J4dOvbdPN&key=[YOUR_API_KEY]
Results:
{
"kind": "youtube#playlistListResponse",
"etag": "OV6gnfMDH09mU3YBdHPuf5uz1Jg",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlist",
"etag": "X8YVDKLXZZfHlVTh_ud0LD-IMMQ",
"id": "PL3tRBEVW0hiB5KKP9gs_AkW8J4dOvbdPN",
"snippet": {
"publishedAt": "2016-05-15T15:29:30Z",
"channelId": "UC7_YxT-KID8kRbqZo7MyscQ",
"title": "DOOM",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/pCygvprsgIk/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/pCygvprsgIk/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/pCygvprsgIk/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/pCygvprsgIk/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/pCygvprsgIk/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Markiplier",
"localized": {
"title": "DOOM",
"description": ""
}
},
"status": {
"privacyStatus": "public"
}
}
]
}
Check this answer for more details.

TotalResults count doesn't match with the actual results returned in YouTube v3 search API

We are using youtube v3 search API. We are getting mismatch in the "totalResults" count and the list of items returned in the response.items field. I am requesting 50 videos in the request. Response returned shows totalResults count as 65 but the response payload has only 3 videos.
API : www.googleapis.com/youtube/v3/search/list
Request Payload
{key=API_KEY, maxResults=50, order=date, part=snippet, publishedAfter=2017-03-20T23:59:59.999-04:00, publishedBefore=2017-04-19T23:59:59.001-04:00, q=( ( "Wood Mackenzie" OR "Wood Mac" OR Woodmac OR #woodmackenzie ) AND NOT (fleetwood OR "fleet wood" OR 9WoodMac ) ) , type=video}
Response Payload
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HTW4WQODZeERIv51VrRfOhir8dg\"",
"nextPageToken": "CDIQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 65,
"resultsPerPage": 50
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/lnNNrToZHGTehIEJ6bvEx5RZMo4\"",
"id": {
"kind": "youtube#video",
"videoId": "obzlB21t904"
},
"snippet": {
"publishedAt": "2017-04-14T11:38:08.000Z",
"channelId": "UC60Sa1LQjgy_a-K8ThN4eQw",
"title": "Fleet wood mac Live at Leeds",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/obzlB21t904/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/obzlB21t904/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/obzlB21t904/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "KEVIN MARSHALL",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/uYuuTAL3vtnFI1WOYjth0cso2Xo\"",
"id": {
"kind": "youtube#video",
"videoId": "gHAn0SbwKMg"
},
"snippet": {
"publishedAt": "2017-04-06T22:17:31.000Z",
"channelId": "UC3_MxBSSbmGznoSTgoBQ7_w",
"title": "Beat Club: British Invasion 60's, Pt-2; Animals-Move-Fleet Wood Mac-J.Mayall-Who-Pacemakers-Kinks",
"description": "Eric Burdon & Animals: C.C. Rider 0:01 The Move: Wild Tiger Woman 3:55 Fleet Wood Mac: Dragonfly 6:26 John Mayall: My Pretty Girl 9:47 Thunderclap ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/gHAn0SbwKMg/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/gHAn0SbwKMg/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/gHAn0SbwKMg/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "ROCK&POPS1_koba",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/tapelxOG4Q3gZRJCR2qaVdt-ZO8\"",
"id": {
"kind": "youtube#video",
"videoId": "5GvLbFBdcF4"
},
"snippet": {
"publishedAt": "2017-04-02T04:48:47.000Z",
"channelId": "UC6fn383TI1HVDYW4dSEIogQ",
"title": "\"Dreams\" by fleet wood mac (vocal cover)",
"description": "I do not own this song. I do not make a profit off of this song. It belongs to the person(s) who wrote it. Please subscribe and comment below any suggestions?!",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/5GvLbFBdcF4/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/5GvLbFBdcF4/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/5GvLbFBdcF4/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Musikallitee",
"liveBroadcastContent": "none"
}
}
]
}
As described in the documentation itself, the totalResults is:
integer
The total number of results in the result set.Please note that the value is an approximation and may not represent an exact value. In addition, the maximum value is 1,000,000.
You should not use this value to create pagination links. Instead, use the nextPageToken and prevPageToken property values to determine whether to show pagination links.
The value is only an approximation/estimate, but not the exact value for the total number of items returned. I've tested the API with the same details as yours and the value tends to change from 63, to 64, and 65.
With all that said, there is nothing wrong in your code. It's just the expected behavior. Unfortunately, it is not stated anywhere as to where the value of totalResults is based on or where it is best used. It's best to proceed with an implementation that doesn't rely on this parameter.

How to identify to liveevent and livestream association?

I had created an live event on youtube and i had associated a custom ingestion (live stream) to that live event.
I am able to get the list of live events by calling
https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet&broadcastStatus=all
Response
{
"kind": "youtube#liveBroadcastListResponse",
"etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/6LS8J5LIkMcXRAIUEViaRonO_jk\"",
"items": [
{
"kind": "youtube#liveBroadcast",
"etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/eSW3gnAC5A4iNg4xj-r8vvP4ha8\"",
"id": "M96Pswa174I",
"snippet": {
"publishedAt": "2015-08-27T09:26:52.000Z",
"channelId": "UCx_ZkmygOl4Bc0PHV8zXeag",
"title": "My new event",
"description": "This is test event",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/M96Pswa174I/default_live.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/M96Pswa174I/mqdefault_live.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/M96Pswa174I/hqdefault_live.jpg",
"width": 480,
"height": 360
}
},
"scheduledStartTime": "2015-08-27T09:30:00.000Z",
"isDefaultBroadcast": false
}
}
]
}
then i am able to get the live stream by calling
https://www.googleapis.com/youtube/v3/liveStreams?part=cdn%2Csnippet&mine=true
Response
- SHOW HEADERS -
{
"kind": "youtube#liveStreamListResponse",
"etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/ctbzD5B9MI-wBMxDXCWCto2oFO8\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#liveStream",
"etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/LEFmnjMqvHydtqmxovlaXCai3vM\"",
"id": "x_ZkmygOl4Bc0PHV8zXeag1440668944469033",
"snippet": {
"publishedAt": "2015-08-27T09:49:04.000Z",
"channelId": "UCx_ZkmygOl4Bc0PHV8zXeag",
"title": "My new stream",
"description": "this is demo stream",
"isDefaultStream": false
},
"cdn": {
"format": "720p_hfr",
"ingestionType": "rtmp",
"ingestionInfo": {
"streamName": "rathodm63.6584-jszu-8p4h-43yg",
"ingestionAddress": "rtmp://a.rtmp.youtube.com/live2",
"backupIngestionAddress": "rtmp://b.rtmp.youtube.com/live2?backup=1"
}
}
}
]
}
but how to identify that this steam belongs to above specified live event ?
You must use LiveBroadcasts.bind to bind a YouTube broadcast to a stream. Note that a broadcast can only be bound to one video stream, although a video stream may be bound to more than one broadcast. Then, you can use the LiveBroadcasts.list request and check the contentDetails.boundStreamId, which will be the stream associated with that livebroadcast. You'll have to specify part=contentDetails if you want to do this through an HTTP request.

Retrieve Video IDs contained in a Playlist - YouTube API v3

I've been using the following method to retrieve all video IDs, titles, and default thumbnail images contained within a specific YouTube playlist for use in a dynamic playlist carousel (The IDs are then passed to an array which is used to dynamically create a thumbnail carousel. Note the 'playlist' variable is defined via an externalized XML value):
// Get YouTube Playlist values
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/' + playlist + '?v=2&alt=json&callback=?';
var videoURL = 'http://www.youtube.com/watch?v=';
$.getJSON(playListURL, function(data) {
var list_data = "";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var thumb = ""+ videoID +"/sddefault.jpg";
if (videoID !='videos') {
yt_videos.push(videoID);
}
yt_thumb.push(thumb);
yt_title.push(feedTitle);
});
$(list_data).appendTo(".cont");
populateCarousel();
});
YouTube has depricated the v1-2 APIs and the method is longer working. Does the YouTube API v3 provide an equivalent method? Specifically replacing the call:
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/' + playlist + '?v=2&alt=json&callback=?';
You will need to switch to the new PlaylistItems/list endpoint to get video information from a playlist.
Request:
HTTP GET: GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id&playlistId=PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk&key={YOUR_API_KEY}
Response (for id= PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk):
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/pBQT3k1NWD9D6HOvzNhT4QS1-W0\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 10,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/wu_RMwA6QYinWSMrYXyOugKDbyI\"",
"id": "PLl3eZeHB033DOxfNMOVENodvC_4QtQ-r2bxEjeNprDxQ",
"snippet": {
"publishedAt": "2014-12-10T21:16:39.000Z",
"channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Late Show with David Letterman",
"playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "4bVgilYncao"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/DPNyicQckvtlwgMTintYEwYsE6g\"",
"id": "PLl3eZeHB033DOxfNMOVENoaZ-Ybk0W0zkTL1Sliq5xeA",
"snippet": {
"publishedAt": "2014-12-10T21:16:58.000Z",
"channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
"title": "David Letterman Announces His Retirement from the Late Show",
"description": "David Letterman announces that he will retire from the Late Show in 2015.\n\nLive on Letterman is up for a Webby award! Let your voice be heard and vote now! http://bit.ly/1l4oktE #WebbyAwards",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/H9eYkpgeeI8/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/H9eYkpgeeI8/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/H9eYkpgeeI8/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/H9eYkpgeeI8/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/H9eYkpgeeI8/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Late Show with David Letterman",
"playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "H9eYkpgeeI8"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/aL6XWPV0PoM50wQcyclAxsvtWy4\"",
"id": "PLl3eZeHB033DOxfNMOVENobu4IxNcwxZw1eEEoLqHjhY",
"snippet": {
"publishedAt": "2014-12-10T21:17:14.000Z",
"channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Late Show with David Letterman",
"playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
"position": 2,
"resourceId": {
"kind": "youtube#video",
"videoId": "ONgN2Hgz3XE"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/YF1JYH8bTG20vjuPLeMR-2xkxjo\"",
"id": "PLl3eZeHB033DOxfNMOVENoQncF7sXo86YALoy5U4yqlU",
"snippet": {
"publishedAt": "2014-12-10T21:17:30.000Z",
"channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
"title": "David Letterman - Future Islands: \"Seasons (Waiting On You)\"",
"description": "Making their network TV debut, Future Islands performed \"Seasons (Waiting On You)\" from their album, \"Singles.\"",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/1Ee4bfu_t3c/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Late Show with David Letterman",
"playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
"position": 3,
"resourceId": {
"kind": "youtube#video",
"videoId": "1Ee4bfu_t3c"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/_Ft_7ujRwQbP7-altelmVnzwQx0\"",
"id": "PLl3eZeHB033DOxfNMOVENoYQ9Z55P4r2KhvkV5yx-okc",
"snippet": {
"publishedAt": "2014-12-10T21:17:47.000Z",
"channelId": "UCVUx0VcNxnHx7ZjuZK5Sthw",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Late Show with David Letterman",
"playlistId": "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
"position": 4,
"resourceId": {
"kind": "youtube#video",
"videoId": "Bzr5VtFvSyw"
}
}
}
]
}
The id value for each object in the items array should be the videoId value that you want.
api_key = 'YOUR_API_KEY'
api_service_name = 'youtube'
api_version = 'v3'
playlist_id = 'SOME_PLAYLIST_ID'
# build a connected client
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=api_key)
# make a request (note, only the 'snippet' part has been requested)
request = youtube.playlistItems().list(part=['snippet'], playlistId=playlist_id)
response = request.execute()
# scrape out the actual video ids (probably could be safer)
for (k, v) in response.items():
if(k == 'items'):
video_ids = [pli['snippet']['resourceId']['videoId'] for pli in v if pli['snippet']['resourceId']['kind']=='youtube#video']
print(video_ids)

With the YouTube API v3 how can I get access to content duration, and genres? By search or get details

I tried to use this:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&{playlist_id}&key={key}&maxResults=50
and
https://www.googleapis.com/youtube/v3/search?part=snippet&q=YouTube+Data+API&type=video&videoCaption=closedCaption&key={key}&maxResults=50
It says in the documentation it is contentDetails.duration but this doesn't seem to be present in the data I receive.
In order to get the detailed information about a video in a playlist, you will need to hit the Videos.list method for every video in that playlist with the snippet and contentDetails values for part:
Request:
HTTP GET https://www.googleapis.com/youtube/v3/videos?part=snippet%2C+contentDetails&id=AKiiekaEHhI&key={YOUR_API_KEY}
Response (for id=AKiiekaEHhI):
{
"kind": "youtube#videoListResponse",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/Qz22l3E04nR4kuY2SGE5M_d1BMM\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/8X_byN22HJTh5FJyoMH9bFGR_Og\"",
"id": "AKiiekaEHhI",
"snippet": {
"publishedAt": "2015-05-04T10:01:43.000Z",
"channelId": "UCkvdZX3SVgfDW8ghtP1L2Ug",
"title": "The Legend of Zelda: Majora's Mask With Glitches - Part 17: Going Against the Flow",
"description": "Follow me on Twitter! http://twitter.com/swordlesslink\n\nFollow me on TwitchTV for live video game streaming! http://twitch.tv/swordlesslink",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/AKiiekaEHhI/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/AKiiekaEHhI/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/AKiiekaEHhI/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/AKiiekaEHhI/sddefault.jpg",
"width": 640,
"height": 480
}
},
"channelTitle": "Swordless Link",
"categoryId": "20",
"liveBroadcastContent": "none",
"localized": {
"title": "The Legend of Zelda: Majora's Mask With Glitches - Part 17: Going Against the Flow",
"description": "Follow me on Twitter! http://twitter.com/swordlesslink\n\nFollow me on TwitchTV for live video game streaming! http://twitch.tv/swordlesslink"
}
},
"contentDetails": {
"duration": "PT17M30S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": true
}
}
]
}
The duration of the content can be found as the object for the duration key in the contentDetails dictionary.
The categoryId maps to your video category, which you will have to look up using the VideoCategories.list method.
Request:
HTTP GET https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id={CATEGORY_ID}&key={YOUR_API_KEY}
Response (for id=20:
{
"kind": "youtube#videoCategoryListResponse",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/cVclmqPmb6Xbwij8SctXRUrSVhw\"",
"items": [
{
"kind": "youtube#videoCategory",
"etag": "\"dhbhlDw5j8dK10GxeV_UG6RSReM/WmA0qYEfjWsAoyJFSw2zinhn2wM\"",
"id": "20",
"snippet": {
"channelId": "UCBR8-60-B28hp2BmDPdntcQ",
"title": "Gaming",
"assignable": true
}
}
]
}
The object for the title key in the snippet dictionary returns the String value of the video category that you want.
If you check search resource, you'll see its not in that response.
This was actually a feature request in: https://code.google.com/p/gdata-issues/issues/detail?id=4294
You can follow up that issue.

Resources