Retrieving individual videos view count - Youtube API V3.0 - JavaScript
Asked Answered
A

2

20

I've been trying to get the view count on videos that I query through the following method:

      function search() {
        var request = gapi.client.youtube.search.list({
             part: 'snippet',
             channelId: 'IRRELEVANT',
             order: 'date',
             maxResults: '25'

         });

            request.execute(function(response){
               YoutubeResponse(response);
            });

While the documentation tells me that there's a statistics portion to every video, after the snippet I have __proto__ which I guess means there was an error somewhere? or did the API change? Essentially I need the view count of those 25 most recent videos...

I tried changing part: 'snippet' to part: 'statistics' but got back a code: -32602...

Thanks for the help,

Cheers!

EDIT: Apparently the search.list doesn't have the "statistics" but rather I need to search every video individually... The thing is, when using googles "Try It" feature (https://developers.google.com/youtube/v3/docs/videos/list#try-it) when you ask for the statistics in the "Fields" part at the bottom, it doesn't do anything... So I am VERY confused as to how the heck can I get the view counts & length of all 25 videos (if individually or all at once - preferably-)

Alleviative answered 22/6, 2014 at 0:26 Comment(0)
S
36

The link you gave https://developers.google.com/youtube/v3/docs/videos/list#try-it is working for me. To get duration and viewCount: Fill in for part: contentDetails,statistics and for id: a comma-separated-list of video-id's like: TruIq5IxuiU,-VoFbH8jTzE,RPNDXrAvAMg,gmQmYc9-zcg

This will create a request as:

GET https://www.googleapis.com/youtube/v3/videos?part=contentDetails,statistics&id=TruIq5IxuiU,-VoFbH8jTzE,RPNDXrAvAMg,gmQmYc9-zcg&key={YOUR_API_KEY}

Steward answered 22/6, 2014 at 6:23 Comment(3)
So if I understand it correctly, I need to do 2 requests... 1) to get the list of videos from the channel and 2) to get the contentDetails of every item?Alleviative
@GalAppelbaum Yes, the list of videos from a channel is a playlist. Playlist-items or search-results do not contain full video information.Steward
Here is a tip - if you are doing it in Python, I found that a "list" does not mean a Python list but a long string with commas between the values.Favor
D
0

Agree with the answer provided by @Als.

But I found a code snippet which might be more convenient for some of you:

function youtube_view_count_shortcode($params)
{
 $videoID = $params['id']; // view id here 
 $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos? 
 part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");
 $jsonData = json_decode($json);
 $views = $jsonData->items[0]->statistics->viewCount;
 return number_format($views);
}

Replace the key value with the google api key for youtube data API and the video id with the youtube video id and Voila you get the total number of views for the youtube video.

Source: https://www.codementor.io/rajharajesuwari/how-to-get-youtube-views-count-aftojpxhj

Disclosure answered 30/11, 2018 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.