Alani
Alani

Reputation: 151

Get viewCount for playlistItems, combined HTTP Requests youtube v3 + JQuery

I'm slowly gettin' crazy here :( I can't get a seemingly easy thing to work. I use the yt api v3 to get videos for specific playlists from a channel. Right now I use two HTTP requests (channels and playlistItems):

  1. get playlist id for all uploads for given username (channels/contentDetails)
  2. get specific playlist-id from data attribute in html-tag
  3. use ids to fetch video information for each playlist (playlistItems/snippet)
  4. output video information, append to lists

This code works so far:

HTML

<h2><span>List 1</h2>
<ul data-plist="PLquImyRfMt6dWNzlyTHW9m353VadxKKg_"></ul>

<h2>List 2</h2>
<ul data-plist="PLquImyRfMt6d4gk6zX8FPEZeyYgE4oBdR"></ul>

<h2>List 3</h2>
<ul data-plist="PLquImyRfMt6fnpIb9VR9c6VLZwYs_OyWL"></ul>

JS / JQUERY:

var channelName = 'goetheinstitut';
var vidResults = 3;


// Get all Videos from the channel via Playlist "uploads"
$.get(
    "https://www.googleapis.com/youtube/v3/channels", {
    part: 'contentDetails',
    forUsername: channelName,
    key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
},

function (data) {
    $.each(data.items, function (i, item) {
        pid = item.contentDetails.relatedPlaylists.uploads;
        getVids(pid);
    });
});

// Get videos for specific Playlist, id from data attr

function getVids(pid) {

    $("[data-plist]").each(function () {

        var vidPlaylist = $(this).data('plist');
        var mylist = $(this);

        // Get and output Playlist Items
        $.get(
            "https://www.googleapis.com/youtube/v3/playlistItems", {
            part: 'snippet',
            maxResults: vidResults,
            playlistId: vidPlaylist,
            key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
        },

        function (data) {
            var output;

            $.each(data.items, function (i, item) {
                videoTitle = item.snippet.title.substring(8);
                videoId = item.snippet.resourceId.videoId;

                output = '<li><a href=\"//www.youtube.com/embed/' + videoId + '?autoplay=1&showinfo=0&controls=0\"><h3>' + videoTitle + '</h3><small>viewCount</small></a></li>';

                //Append to results list                            
                $(mylist).append(output);
                });
            });
        });
    }

Now I also need the viewCount for each video. Which means I have to store the videoIds for each playlist in an array and then make a new request ( https://www.googleapis.com/youtube/v3/videos) for statistics.viewCount. But I can't manage it with the code I already have. Here's a jsfiddle.

Here's the request for the viewCount, with an array of ids put in manually:

$.get(
    "https://www.googleapis.com/youtube/v3/videos", {
    part: 'statistics',
    // 3 ids from List 1 - these would have to come as variable
    id: '8kFevX-bp8g, L6thExvypGg, D_RBv6Bsm6E',
    key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
},

function (data) {
    var listviews;

    $.each(data.items, function (i, item) {
        views = item.statistics.viewCount;

        listviews = '<li>'+views+'</li>';

        $('#results').append(listviews);
        // should be part of the output from the previous function...           
    });
});

Can someone please help me to stitch these two requests together? I always end up with some undefined variable because I'm off scope. :(

Upvotes: 0

Views: 771

Answers (1)

t_yamo
t_yamo

Reputation: 779

I might not have understood your requirement correctly.

But, how about this code?

var channelName = 'goetheinstitut';
var vidResults = 3;

$.get(
    "https://www.googleapis.com/youtube/v3/channels", {
    part: 'contentDetails',
    forUsername: channelName,
    key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
},
function (data) {
    $.each(data.items, function (i, item) {
        var pid = item.contentDetails.relatedPlaylists.uploads;
        getVids(pid);
    });
});

function getVids(pid) {
    $("[data-plist]").each(function () {
        var vidPlaylist = $(this).data('plist');
        var mylist = $(this);
        $.get(
            "https://www.googleapis.com/youtube/v3/playlistItems", {
            part: 'snippet',
            maxResults: vidResults,
            playlistId: vidPlaylist,
            key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
        },
        function (data) {
            $.each(data.items, function (i, item) {
                var videoTitle = item.snippet.title.substring(8);
                var videoId = item.snippet.resourceId.videoId;
                $.get(
                    "https://www.googleapis.com/youtube/v3/videos", {
                    part: 'statistics',
                    id: videoId,
                    key: 'AIzaSyCu8N1RGBaExofEQtBZHMMpAYNxvirhIBM'
                },
                function (data) {
                    $.each(data.items, function (i, item) {
                        var views = item.statistics.viewCount;
                        var output = '<li><a href=\"//www.youtube.com/embed/' + videoId + '?autoplay=1&showinfo=0&controls=0\"><h3>' + videoTitle + '</h3><small>' + views + '</small></a></li>';
                        $(mylist).append(output);
                    });
                });
            });
        });
    });
}

Upvotes: 1

Related Questions