곽교린

Update CommentLoadingTest.js

modify code to can seperate comment by selected language. and correct e-
rror that when go to next comment page selected language is not applied.
and modify code to allow videoURL to remain after the page moves.
......@@ -9,6 +9,7 @@ const {google} = require("googleapis");
const service = google.youtube('v3');
const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키
let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시)
var videoLang = 'all'; //설정 언어
const serverIP = "http://localhost:3000" //서버의 주소
var commentList = new Array();
var commentNum = 0;
......@@ -16,14 +17,30 @@ var savednpt = ''
const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식
const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식
function showcomment(response, VideoNum){
let body = ''
let commentDisplay = "";
async function showcomment(response, VideoNum){
let sepComment = new Promise((resolve, reject)=>{
async function inSepCom(){
let tempcommentDisplay = "";
console.log(commentNum);
for(let iterArr = 0; iterArr < commentNum; iterArr++){
commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`
await commentList[iterArr].lang.then((languageData) =>{
//console.log(languageData);
if(videoLang == 'all'){
tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`;
}
body = `
else if(videoLang == languageData){
tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`;
}
});
}
return tempcommentDisplay
}
resolve(inSepCom());
});
let commentDisplay = await sepComment;
let body = `
<!doctype html>
<html>
<head>
......@@ -39,7 +56,7 @@ function showcomment(response, VideoNum){
<h1 style="font-family: 'Permanent Marker', cursive; text-align:center; padding-bottom:1%;"><a href="${serverIP}" style="text-decoration:none; color:inherit;">Youtube Comment Seperator</a></h1>
<form action="${serverIP}/search" method="get">
<div class="form-group" style="font-family: 'Ubuntu', sans-serif;">
<input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;">
<input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;" value=${VideoNum}>
<label for="language-select">Choose a language:</label>
<select class="form-control" name="languages" id="language-select" style="display:inline-flex;">
<option value="all" selected>--Please choose an option--</option>
......@@ -62,7 +79,7 @@ function showcomment(response, VideoNum){
<iframe class="embed-responsive-item" 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>
</div>
<br>
<a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}" role="button">Show more comments</a>
<a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}&languages=${videoLang}" role="button">Show more comments</a>
<br>
${commentDisplay}
</body>
......@@ -70,6 +87,7 @@ function showcomment(response, VideoNum){
`;
response.writeHead(200);
response.end(body);
}
function loadcomment(ApiKey, VideoNum, npt, n, res){
......@@ -98,7 +116,7 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){
//console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay);
npt = response.data.nextPageToken;
if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){
if(npt != null){
if( n > 1 ){
loadcomment(ApiKey, VideoNum, npt, n - 1, res);
}else{
......@@ -164,6 +182,7 @@ var app = http.createServer(function(request,response){ // request는 브라우
commentNum = 0; //코멘트 리스트 초기화
videoNum = queryData.videourl;
videoLang = queryData.languages;
//유튜브 주소를 그대로 넣었을 때 videoNum 추출
if(URLreg2.test(videoNum)){
let tempvideoNum = url.parse(videoNum,true).query;
......
......@@ -40,7 +40,7 @@ async function findLanguage(sourceText) {
request.post(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
resultLang = JSON.parse(body).langCode
console.log(resultLang);
//console.log(resultLang);
resolve(resultLang);
}
else {
......