John Brush
John Brush

Reputation: 177

youtube data api retrieve view counts

i implemented in my .net project api youtube.

this is my code

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            ApiKey = "MY_API_KEY",
            ApplicationName = "MY_APPLICATION_NAME"
        });
        var searchListRequest = youtubeService.Search.List("snippet");
        searchListRequest.Q = SearchText;
        searchListRequest.MaxResults = 50;
        searchListRequest.Order = SearchResource.ListRequest.OrderEnum.ViewCount;

        var searchListResponse = await searchListRequest.ExecuteAsync();


        foreach (var searchResult in searchListResponse.Items)
        {
            if (searchResult.Id.Kind == "youtube#video")
            {
            }
        }

in searchResult not have a STATISTICS (for example view counts). How to?

Upvotes: 0

Views: 715

Answers (2)

mpgn
mpgn

Reputation: 7251

Because search.list don't have the part statitics you need to call two time the API.

  • One time with the request search.list
  • You get the id of the channel
  • And a second call with channel.list, with the id of the channel and parameter: statistics

Then you have viewCount

Doc can help : https://developers.google.com/youtube/v3/docs/search/list https://developers.google.com/youtube/v3/docs/channels/list

Upvotes: 1

Shivam
Shivam

Reputation: 2258

I came across the same problem using their Javascript based API search functionality.

Looks like they do not have a build in "views" option for their search based API. https://developers.google.com/youtube/v3/docs/search#snippet

How ever, you can use their JSON based API and create a AJAX based search box, which returns a JSON based response with a option for view count! https://developers.google.com/youtube/2.0/developers_guide_jsonc

Created this myself, check it out:

$(document).ready(function() {

var q = $('#query');

$('#search-button').click(function(e) {
    var url = "https://gdata.youtube.com/feeds/api/videos?q=" + q.val() + "&v=2&alt=jsonc";
    $.getJSON( url, function( response ) {
        for(var i = 0; i < response.data.items.length; i++) {

            var tr = "<tr>",
                title = "<td>" + response.data.items[i].title + "</td>",
                views = "<td>" + response.data.items[i].viewCount + "</td>",
                likes = "<td>" + response.data.items[i].likeCount + "</td>",
                dislikes = "<td>" + (response.data.items[i].ratingCount - response.data.items[i].likeCount) + "</td>",
                endtr = "</tr>";

            $('#search-container').append(tr + title + views + likes + dislikes + endtr);
        }
    });

    e.preventDefault();
});

});

http://jsfiddle.net/19m9tLo3/1/

Upvotes: 0

Related Questions