javascript - 获取 playlistItems 的 viewCount,组合 HTTP 请求 youtube v3 + JQuery

标签 javascript jquery youtube-api xmlhttprequest youtube-data-api

我在这里慢慢变得疯狂:(我无法得到一个看似简单的事情来工作。我使用 yt api v3 从 channel 获取特定播放列表的视频。现在我使用两个 HTTP 请求( channel 和播放列表项目):

  1. 获取给定用户名( channel /内容详细信息)的所有上传的播放列表 ID
  2. 从 html-tag 中的数据属性获取特定的播放列表 ID
  3. 使用 ids 获取每个播放列表的视频信息(playlistItems/snippet)
  4. 输出视频信息,附加到列表

此代码到目前为止有效:

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);
                });
            });
        });
    }

现在我还需要每个视频的 viewCount。 这意味着我必须将每个播放列表的 videoId 存储在数组中,然后为 stats.viewCount 发出新请求 ( https://www.googleapis.com/youtube/v3/videos )。但我无法用我已有的代码来管理它。这是 jsfiddle .

这是对 viewCount 的请求,其中包含手动输入的 id 数组:

$.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...           
    });
});

有人可以帮我将这两个请求拼接在一起吗?我总是会得到一些 undefined variable ,因为我超出了范围。 :(

最佳答案

我可能没有正确理解您的要求。

但是,这段代码怎么样?

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);
                    });
                });
            });
        });
    });
}

关于javascript - 获取 playlistItems 的 viewCount,组合 HTTP 请求 youtube v3 + JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341477/

相关文章:

javascript - 样式表 - 不加载网页

javascript - 使用 require.js 加载非 amd 模块

javascript - 保留字 JavaScript "this"

javascript - 在同一函数 jQuery UI 中禁用和刷新

javascript - 如何将我的文本框自动格式化为许可证格式?

php - 从播放列表ID检索视频ID-YouTube API V3

ios - YouTube Objective-C API 视频 maxResults

java - 上传带字幕的视频

javascript - 加载和显示 svg

javascript - 发布到同一页面但清除文本区域