곽교린

Update CommentLoadingTest.js

correct error : load page asynchronous
......@@ -12,14 +12,51 @@ var commentList = new Array();
var commentNum = 0;
var savednpt = ''
function loadcomment(ApiKey, VideoNum, npt, n){
function showcomment(response, VideoNum){
let body = ''
let commentDisplay = "";
console.log(commentNum);
for(let iterArr = 0; iterArr < commentNum; iterArr++){
commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`
}
body = `
<!doctype html>
<html>
<head>
<title>Youtube Comment</title>
<meta charset="utf-8">
</head>
<body>
<form action="http://localhost:3000/search" method="get">
<p>
<textarea name="videourl" placeholder="Write your video Url" >${VideoNum}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>
<br>
<br>
<iframe width="560" height="315" src="https://www.youtube.com/embed/${VideoNum}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<br>
<br>
${commentDisplay}
</body>
</html>
`;
response.writeHead(200);
response.end(body);
}
function loadcomment(ApiKey, VideoNum, npt, n, res){
return service.commentThreads.list({
"key":ApiKey,
"part":[
"snippet, replies"
],
"videoId":VideoNum,//비디오 주소
"maxResults" : 50,
"maxResults" : 100,
"pageToken" : npt
}).then(function(response) {
//console.log("Response", response);
......@@ -38,12 +75,15 @@ function loadcomment(ApiKey, VideoNum, npt, n){
if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){
if( n > 1 ){
loadcomment(ApiKey, VideoNum, npt, n - 1);
loadcomment(ApiKey, VideoNum, npt, n - 1, res);
}else{
savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신
showcomment(res, VideoNum);
}
}else{
savednpt = '';
console.log('end page'); // 댓글의 마지막 페이지
showcomment(res, VideoNum);
}
},
function(err) { console.error("Execute error", err); });
......@@ -84,44 +124,7 @@ var app = http.createServer(function(request,response){ // request는 브라우
videoNum = queryData.videourl;
console.log(videoNum);
let npt = ""
loadcomment(apiKey,videoNum,npt,2).then(()=>{
setTimeout(()=>{ //딜레이를 주어 강제로 댓글을 여러번 불러오도록 구현, async화 필요.
let commentDisplay = "";
console.log(commentNum);
for(let iterArr = 0; iterArr < commentNum; iterArr++){
commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`
}
body = `
<!doctype html>
<html>
<head>
<title>Youtube Comment</title>
<meta charset="utf-8">
</head>
<body>
<form action="http://localhost:3000/search" method="get">
<p>
<textarea name="videourl" placeholder="Write your video Url" >${videoNum}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>
<br>
<br>
<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoNum}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<br>
<br>
${commentDisplay}
</body>
</html>
`;
response.writeHead(200);
response.end(body);
},1000);
})
loadcomment(apiKey,videoNum,npt,2, response);
}
});
app.listen(3000);
\ No newline at end of file
......