gpt4 book ai didi

javascript - 来自 Youtube API v3 的内容详细信息、持续时间和统计信息的问题

转载 作者:行者123 更新时间:2023-11-30 12:04:59 25 4
gpt4 key购买 nike

我正在尝试检索每个视频的 durationviewCount 的值,两者都在 videos:list api 下。我创建了一个单独的函数来返回每个视频的持续时间和统计信息。唯一有效的是检索到的视频播放列表和 getPlaylist(...) 函数中的 videoId。我登录到控制台,这就是我得到的:

console.log("id: " + videoId + " duration: " + vidDuration + " viewCount: " + viewCount); // return value in console

id: xxx duration: undefined viewCount: undefined

我从来没有得到持续时间来工作,但我注意到带有 part: statistics 的 api 将从被注释掉的 channel 中检索观看次数总和的值。但这不是我要找的。我希望它与每个视频一起使用:

获取 https://www.googleapis.com/youtube/v3/videos

但由于某种原因,我无法让它工作。我有 videoId 属性和参数,因此 playlistId 可以读取它并检索视频持续时间和观看次数的所述值,但它仍然未定义,即使 videoId已被定义。哦,警报对话框也没有出现。

我在这里错过了什么?

脚本:

var channelName = 'ExampleChannel';
var vidWidth = 500;
var vidHeight = 400;
var vidResults = 15; /* # of videos to show at once - max 50 */
var vidDuration = "";
var viewCount = 0;
var videoId = "";

$(document).ready(function() {
$.get( // get channel name and load data
"https://www.googleapis.com/youtube/v3/channels",
{
part: 'contentDetails',
forUsername: channelName,
key: 'XXXXXXXXXX'
},

function(data)
{
$.each(data.items,
function(i, item) {
console.log(item); // log all items to console
var playlistId = item.contentDetails.relatedPlaylists.uploads;
//var viewCount = console.log(item.statistics.viewCount);
getPlaylists(playlistId);

})
}
);

// function that gets the playlists
function getPlaylists(playlistId)
{
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",
{
part: 'snippet',
maxResults: vidResults,
playlistId: playlistId,
key: 'XXXXXXXXXX'
},

// print the results
function(data)
{
var output;
$.each(data.items,
function(i, item) {
console.log(item);
var vidTitle = item.snippet.title; // video title
var vidDesc = item.snippet.description; // video description
var videoId = item.snippet.resourceId.videoId; // video id

// check if description is empty
if(vidDesc == null || vidDesc == "")
{
vidDesc = "No description was written."; // FIX: test msg to see where it still shows up
$('#desc').remove(); // remove video description
}
else vidDesc = item.snippet.description;

vidDuration = getVideoDuration(videoId);
viewCount = getViewCount(videoId);
console.log("id: " + videoId + " duration: " + vidDuration + " viewCount: " + viewCount); // return value in console

output = '<li><iframe height="' + vidHeight + '" width="' + vidWidth + '" src=\"//www.youtube.com/embed/' + videoId + '\"></iframe></li><div id="title">' + vidTitle + '</div><div id="desc">' + vidDesc + '</div><div id="duration">Length: ' + vidDuration + '</div><div id="stats">View Count: ' + viewCount + '</div>';

// Append results to list tag
$('#results').append(output);
})
}
);
}

// return video duration
function getVideoDuration(videoId)
{
$.get(
"https://www.googleapis.com/youtube/v3/videos",
{
part: 'contentDetails',
id: videoId,
key: 'XXXXXXXXXX',
},

function(data)
{
$.each(data.items,
function(i, item) {
//videoId = item.snippet.resourceId.videoId;

alert(item.contentDetails.duration); // video duration
//alert(videoId);
})
}
);
}

// return video view count
function getViewCount(videoId)
{
$.get(
"https://www.googleapis.com/youtube/v3/videos",
{
part: 'contentDetails, statistics',
id: videoId,
key: 'XXXXXXXXXX',
},

function(data)
{
$.each(data.items,
function(i, item) {
//videoId = item.snippet.resourceId.videoId;

alert(item.statistics.viewCount); // view count
//alert(videoId);
})
}
);
}
});

截图:(更新)

enter image description here

最佳答案

因为当您调用这些函数时,您不会等待函数完成。您需要使用 Promise在 Javascript 中。

我稍微更改了您的代码(使用我的 api key 测试)

var channelName = 'example';
var vidWidth = 500;
var vidHeight = 400;
var vidResults = 15; /* # of videos to show at once - max 50 */
var vidDuration = "";
var viewCount = 0;
var videoId = "";

$(document).ready(function() {
$.get( // get channel name and load data
"https://www.googleapis.com/youtube/v3/channels",
{
part: 'contentDetails',
forUsername: channelName,
key: 'xxx'
},

function(data)
{
$.each(data.items,
function(i, item) {
//console.log(item); // log all items to console
var playlistId = item.contentDetails.relatedPlaylists.uploads;
//var viewCount = console.log(item.statistics.viewCount);
getPlaylists(playlistId);

});
}
);

// function that gets the playlists
function getPlaylists(playlistId)
{
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",
{
part: 'snippet',
maxResults: vidResults,
playlistId: playlistId,
key: 'xxx'
},

// print the results
function(data)
{
var output;
$.each(data.items,
function(i, item) {
console.log(item);
var vidTitle = item.snippet.title; // video title
var vidDesc = item.snippet.description; // video description
var videoId = item.snippet.resourceId.videoId; // video id

// check if description is empty
if(vidDesc == null || vidDesc == "")
{
vidDesc = "No description was written."; // FIX: test msg to see where it still shows up
$('#desc').remove(); // remove video description
}
else vidDesc = item.snippet.description;

getVideoDuration(videoId).done(function(d, v){
vidDuration = d;
//console.log(r);


viewCount = v;

document.write("id: " + videoId + " duration: " + vidDuration + " viewCount: " + viewCount); // return value in console

document.write("<br>");

output = '<li><iframe height="' + vidHeight + '" width="' + vidWidth + '" src=\"//www.youtube.com/embed/' + videoId + '\"></iframe></li><div id="title">' + vidTitle + '</div><div id="desc">' + vidDesc + '</div><div id="duration">Length: ' + vidDuration + '</div><div id="stats">View Count: ' + viewCount + '</div>';

// Append results to list tag
$('#results').append(output);
});
});
}
);
}

// return video duration
function getVideoDuration(videoId)
{
var dfrd1 = $.Deferred();
var r = '';
$.get(
"https://www.googleapis.com/youtube/v3/videos",
{
part: 'contentDetails',
id: videoId,
key: 'xxx',
},

function(data)
{
$.each(data.items,
function(i, item) {
//videoId = item.snippet.resourceId.videoId;
var view = 0;
r = item.contentDetails.duration; // video duration
getViewCount(videoId).done(function(t){
view = t;
dfrd1.resolve(r, view);
});

//alert(videoId);
});
}
);
return dfrd1.promise();
}

// return video view count
function getViewCount(videoId)
{
var dfrd2 = $.Deferred();
var r = '';
$.get(
"https://www.googleapis.com/youtube/v3/videos",
{
part: 'contentDetails, statistics',
id: videoId,
key: 'xxx',
},

function(data)
{

$.each(data.items,
function(i, item) {
//videoId = item.snippet.resourceId.videoId;

r = item.statistics.viewCount; // view count
//alert(videoId);
dfrd2.resolve(r);

// console.log("in", r);
});
}
);
return dfrd2.promise();
}
});

编辑

换句话说,这是一个异步方法调用。

getVideoDuration(videoId).done(function(r)

解释:

调用函数 getVideoDuration.done 告诉函数 getVideoDuration 将在我们解决 promise 时返回结果(dfrd1 .resolve(r);).与此同时,函数 return dfrd1.promise(); 意味着结果将被推迟。当 promise 得到解决时,我们输入 done,然后我们就可以完成剩下的事情了:)

您在屏幕截图上看到的是合乎逻辑的,因为有两个 Promise ! 我的错误是我没有看到重复的输入行。我更改代码来解决这个问题。

因此第一个函数 getVideoDuration 将被解析,然后是第二个。 当第二个函数被解决时,我们解决第一个 promise ,将结果发送到函数getPlaylists(playlistId)

编辑 25/02

我更改了有关 promise 错误的代码。

关于javascript - 来自 Youtube API v3 的内容详细信息、持续时间和统计信息的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35511630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com