我目前正在尝试使用Youtube V3 API生成频道中最近上传的视频的Feed。目前我的主要是正在工作,但是我不能在我的生活中获得两个重要的数据位 - 视频观看和视频持续时间。我知道这些数据属于"视频"端点,但我不确定如何将其集成到Google的Youtube API网站上提供的Javascript API示例中。以下是我的代码 - 非常感谢任何帮助:
// Define some variables used to remember state.
var playlistId, nextPageToken, prevPageToken;
// After the API loads, call a function to get the uploads playlist ID.
function handleAPILoaded() {
requestUserUploadsPlaylistId();
}
// Call the Data API to retrieve the playlist ID that uniquely identifies the
// list of videos uploaded to the currently authenticated user's channel.
function requestUserUploadsPlaylistId() {
// See https://developers.google.com/youtube/v3/docs/channels/list
var request = gapi.client.youtube.channels.list({
mine: true,
part: 'contentDetails, statistics'
});
request.execute(function(response) {
playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playlistId);
});
}
// Retrieve the list of videos in the specified playlist.
function requestVideoPlaylist(playlistId, pageToken) {
$('#video-container').html('');
var requestOptions = {
playlistId: playlistId,
part: 'snippet, contentDetails',
maxResults: 10
};
if (pageToken) {
requestOptions.pageToken = pageToken;
}
var request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(function(response) {
// Only show pagination buttons if there is a pagination token for the
// next or previous page of results.
nextPageToken = response.result.nextPageToken;
var nextVis = nextPageToken ? 'visible' : 'hidden';
$('#next-button').css('visibility', nextVis);
prevPageToken = response.result.prevPageToken
var prevVis = prevPageToken ? 'visible' : 'hidden';
$('#prev-button').css('visibility', prevVis);
var playlistItems = response.result.items;
if (playlistItems) {
$.each(playlistItems, function(index, item) {
displayResult(item.snippet);
displayContentDetails(item.contentDetails);
displayStats(item.statistics);
});
} else {
$('#video-container').html('Sorry you have no uploaded videos');
}
});
}
// Create a listing for a video.
function displayResult(videoSnippet) {
var title = videoSnippet.title;
var videoId = videoSnippet.resourceId.videoId;
var publishedAt = videoSnippet.publishedAt;
var videoThumbURL = videoSnippet.thumbnails.high.url;
var publishedUTC = new Date(publishedAt);
var publishedDay = publishedUTC.getUTCDay();
var publishedMonth = publishedUTC.getUTCMonth();
var publishedYear = publishedUTC.getUTCFullYear();
var a = moment(publishedUTC);
var b = moment(Date.now());
var timeFrom = a.from(b);
$('#video-container').append('<a href=\"https://youtu.be/'+ videoId +'\"><li><img src='+videoThumbURL+'><br><p>' + title + ' - ' + timeFrom + '</p></li></a>');
}
// Create a listing for a video.
function displayContentDetails(stuff) {
var videoEndsAt = stuff.duration;
$('#video-container li').append('<span class="endsAt">'+ videoEndsAt +'</span>');
}
function displayStats(vidStats) {
var videoStats = vidStats.viewCount;
$('#video-container li').append('<span class="videoStats">'+ videoStats +'</span>');
}
// Retrieve the next page of videos in the playlist.
function nextPage() {
requestVideoPlaylist(playlistId, nextPageToken);
}
// Retrieve the previous page of videos in the playlist.
function previousPage() {
requestVideoPlaylist(playlistId, prevPageToken);
}
答案 0 :(得分:1)
虽然您说通道代码显示您从播放列表中提取,但没有您想要的数据。
对于播放列表中的每个视频,您需要保存videoID,然后使用'snippet'的'parts'参数拨打Videos: list endpoint(一次最多50个ID)的电话,contentDetails,统计。这些API结果将包含您想要的数据。
答案 1 :(得分:0)
谢谢@ Johnh10,你是对的,这就是我最终做的事情。
var videoStatsEndpoint = "https://www.googleapis.com/youtube/v3/videos?part=statistics";
var videoToQuery = "&id="+videoId+"&key=";
var apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var videoViews = 0;
jQuery.get(videoStatsEndpoint + videoToQuery + apiKey, function(data){
videoViews = data.items[0].statistics.viewCount;
console.log(videoViews);
});