我有点抓狂了:一件看似简单的事情我都做不好。我使用yt api v3从频道获取特定播放列表的视频。现在我使用两个HTTP请求(channels和playlistItems):
- 获取给定用户名(channels/contentDetails)的所有上传的播放列表id
- 从html-tag 中的data属性获取特定的播放列表id
- 使用id获取每个播放列表(playlisttitems/snippet)的视频信息
- 输出视频信息,附加到列表
下面的代码可以运行:
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存储在一个数组中,然后为statistics.viewCount发出新的请求(https://www.googleapis.com/youtube/v3/videos)。但我现有的代码没法处理。
下面是对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...
});
});
谁能帮我把这两个请求连在一起?我总是得到一些未定义的变量,因为我超出了作用域。( 我可能没有正确理解你的要求。
但是,这段代码呢?
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);
});
});
});
});
});
}