您好,在下面的代码中定义了三个函数。它将下载三个csv文件。我正在尝试从downloadcsvsearch函数访问视频ID,我需要选择该视频ID,我需要将其传递给下一个downloadcsvcomments的功能。我可以这样做吗?
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<a href='#' onclick='downloadCSV({ filename: "a.csv" }); downloadCSVV({ filename1:"b.csv"}); downloadCSVVV({ filename2:"c.csv"});'>Download CSV</a>
<script type="text/javascript">
$(document).ready(function() {});
function convertArrayOfObjectsToCSV(args) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = args.data || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
console.log("columnDelimiter " + result);
});
result += lineDelimiter;
});
return result;
}
function downloadCSV(args) {
var youtubeData = [];
var data, filename, link;
$.getJSON('https://www.googleapis.com/youtube/v3/search?part=snippet&q=laptops&maxResults=50&key=""', function(data) {
console.log(data);
for(var i = 0; i < data.items.length; i++) {
youtubeData.push({
"videoid": data.items[i].id.videoId,
"Title": data.items[i].snippet.title
});
}
var csv = convertArrayOfObjectsToCSV({
data: youtubeData
});
if (csv == null) return;
filename = args.filename || 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
link.click();
});
}
function downloadCSVV(args) {
var youtubeData1 = [];
var data1, filename1, link;
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id=kmJM7Z6_DEw,zsvLai5jxCs,N9uMFDotJPg,OlTH_XPlFrw&key=""&part=snippet,statistics', function(data1) {
console.log(data1);
for(var i = 0; i < data1.items.length; i++) {
youtubeData1.push({
"videoid": data1.items[i].id,
"Id": data1.items[i].snippet.categoryId,
"ChannelId": data1.items[i].snippet.channelId,
"Title": data1.items[i].snippet.title,
// "Description": data.items[i].snippet.description.replace("\n", ""),
"Commentcount": data1.items[i].statistics.commentCount,
"ViewCount": data1.items[i].statistics.viewCount,
"LikeCount": data1.items[i].statistics.likeCount,
"DislikeCount": data1.items[i].statistics.dislikeCount,
"FavoriteCount": data1.items[i].statistics.favoriteCount
});
}
var csv = convertArrayOfObjectsToCSV({
data: youtubeData1
});
if (csv == null) return;
filename1 = args.filename1 || 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data1 = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data1);
link.setAttribute('download', filename1);
link.click();
});
}
function downloadCSVVV(args) {
var items = [];
var youtubeData2 = [];
var data2, filename2, link;
$.getJSON('https://www.googleapis.com/youtube/v3/commentThreads?key=""&textFormat=plainText&part=snippet,replies&videoId=OlTH_XPlFrw&maxResults=60', function(data2) {
for(var i = 0; i < data2.items.length; i++) {
youtubeData2.push({
"videoid": data2.items[i].snippet.topLevelComment.snippet.videoId,
"Authordisplayname": data2.items[i].snippet.topLevelComment.snippet.authorDisplayName,
"Textdisplay": data2.items[i].snippet.topLevelComment.snippet.textDisplay,
});
}
console.log(data2);
var csv = convertArrayOfObjectsToCSV({
data: youtubeData2
});
if (csv == null) return;
filename2 = args.filename2 || 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data2 = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data2);
link.setAttribute('download', filename2);
link.click();
});
}
</script>
</body>
</html>
答案 0 :(得分:0)
你做不到。 commentThreads reference表示videoId
只能包含一个字符串:
videoId参数指示API返回注释线程 与指定的视频ID相关联。
如果您需要有关多个视频的评论,则必须创建多个请求,每个视频ID一个请求。