cujo30227
cujo30227

Reputation: 770

Retrieve number of items in playlist with YouTube API v3

I'm transferring my iOS code from YouTube Data API v2 to v3.

I would like to retrieve the number of items in a playlist with YouTube API v3.

With the data API v2, I could call a search query, resulting in a list of playlist items with the property "size", which represented the size of the playlist, see:

https://gdata.youtube.com/feeds/api/playlists/snippets?q=GoogleDevelopers&max-results=10&v=2&alt=jsonc

With API v3, I haven't found a way to achieve this. I tried queries, like below, not resulting in the desired output.

https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=RD0KSOMA3QBU0&key={YOUR_API_KEY}

The only way to get the number of items is running a query for each playlist, retrieving the full playlist items. This is an overkill, however, and results in a too high API usage. https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=RD0KSOMA3QBU0&key={YOUR_API_KEY}

Any help appreciated!

Thanks

Upvotes: 2

Views: 4234

Answers (5)

barracuda317
barracuda317

Reputation: 638

I would not recommend to rely on neither itemsCount in Playlist-Query nor pageInfo.totalResults in Playlistitems-Query, because both values may differ from the size of the retrieved list of playlist-items

see also: https://stackoverflow.com/questions/37899490/totalresults-in-playlistitems-do-not-match-items-size

Upvotes: 0

hung phan
hung phan

Reputation: 119

This javascript will retrieve the number 91. That is the total number of clips (total = data.pageInfo.totalResults) from the youtube playlist

<script>
  function numberOfClips(pid){ 
    $.get(
        "https://www.googleapis.com/youtube/v3/playlistItems",{
        part : 'snippet', 
        maxResults : 2,
        playlistId : pid,
        key: 'YOUR API v.3 KEY',
        fields: 'pageInfo(totalResults)'        
        },
        function(data) {
             total = data.pageInfo.totalResults;
             $('#total').html('Total number of clips: ' + total);
             st = JSON.stringify(data,'',2);
             $('#area1').val(st);
        });
  }
  </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<body onload="numberOfClips('PLE9F62B21C75C054C')">
  <p id="total"></p>
  <textarea id='area1' style='width:400px;height:100px;'></textarea>

PLE9F62B21C75C054C

Upvotes: 0

Vishal Khatri
Vishal Khatri

Reputation: 952

I have use the following V3 api to get the playlist videos, and its works for me.

https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId=PLFPg_IUxqnZM3uua-YwStHJ1qmlQKBnh0&key={YOUR_API_KEY_HERE}

**

playlistId

**

There is no details available for "playlistId" parameter in Youtube Documentation. but is works for getting playlist videos.

Upvotes: 1

cujo30227
cujo30227

Reputation: 770

thanks for that. It helped me to find a solution, which is now to call

https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id={Comma Separated ID's}&key={YOUR_API_KEY}

an use the option to make a comma separate lists of the playlists to evaluate. In the return string, I grab the "itemCount" parameter for each playlist.

Thanks

Upvotes: 4

MuhammadBassio
MuhammadBassio

Reputation: 1570

Unfortunately, there is no way to get number of items in a playlist without requesting contentDetails in the parts (which isn't supported when listing playlists).

Upvotes: 0

Related Questions