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.
Showing
2 changed files
with
28 additions
and
9 deletions
... | @@ -9,6 +9,7 @@ const {google} = require("googleapis"); | ... | @@ -9,6 +9,7 @@ const {google} = require("googleapis"); |
9 | const service = google.youtube('v3'); | 9 | const service = google.youtube('v3'); |
10 | const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키 | 10 | const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키 |
11 | let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) | 11 | let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) |
12 | +var videoLang = 'all'; //설정 언어 | ||
12 | const serverIP = "http://localhost:3000" //서버의 주소 | 13 | const serverIP = "http://localhost:3000" //서버의 주소 |
13 | var commentList = new Array(); | 14 | var commentList = new Array(); |
14 | var commentNum = 0; | 15 | var commentNum = 0; |
... | @@ -16,14 +17,30 @@ var savednpt = '' | ... | @@ -16,14 +17,30 @@ var savednpt = '' |
16 | const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식 | 17 | const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식 |
17 | const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식 | 18 | const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식 |
18 | 19 | ||
19 | -function showcomment(response, VideoNum){ | 20 | +async function showcomment(response, VideoNum){ |
20 | - let body = '' | 21 | + let sepComment = new Promise((resolve, reject)=>{ |
21 | - let commentDisplay = ""; | 22 | + async function inSepCom(){ |
23 | + | ||
24 | + let tempcommentDisplay = ""; | ||
22 | console.log(commentNum); | 25 | console.log(commentNum); |
26 | + | ||
23 | for(let iterArr = 0; iterArr < commentNum; iterArr++){ | 27 | for(let iterArr = 0; iterArr < commentNum; iterArr++){ |
24 | - commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>` | 28 | + await commentList[iterArr].lang.then((languageData) =>{ |
29 | + //console.log(languageData); | ||
30 | + if(videoLang == 'all'){ | ||
31 | + tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`; | ||
25 | } | 32 | } |
26 | - body = ` | 33 | + else if(videoLang == languageData){ |
34 | + tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`; | ||
35 | + } | ||
36 | + }); | ||
37 | + } | ||
38 | + return tempcommentDisplay | ||
39 | + } | ||
40 | + resolve(inSepCom()); | ||
41 | + }); | ||
42 | + let commentDisplay = await sepComment; | ||
43 | + let body = ` | ||
27 | <!doctype html> | 44 | <!doctype html> |
28 | <html> | 45 | <html> |
29 | <head> | 46 | <head> |
... | @@ -39,7 +56,7 @@ function showcomment(response, VideoNum){ | ... | @@ -39,7 +56,7 @@ function showcomment(response, VideoNum){ |
39 | <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> | 56 | <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> |
40 | <form action="${serverIP}/search" method="get"> | 57 | <form action="${serverIP}/search" method="get"> |
41 | <div class="form-group" style="font-family: 'Ubuntu', sans-serif;"> | 58 | <div class="form-group" style="font-family: 'Ubuntu', sans-serif;"> |
42 | - <input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;"> | 59 | + <input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;" value=${VideoNum}> |
43 | <label for="language-select">Choose a language:</label> | 60 | <label for="language-select">Choose a language:</label> |
44 | <select class="form-control" name="languages" id="language-select" style="display:inline-flex;"> | 61 | <select class="form-control" name="languages" id="language-select" style="display:inline-flex;"> |
45 | <option value="all" selected>--Please choose an option--</option> | 62 | <option value="all" selected>--Please choose an option--</option> |
... | @@ -62,7 +79,7 @@ function showcomment(response, VideoNum){ | ... | @@ -62,7 +79,7 @@ function showcomment(response, VideoNum){ |
62 | <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> | 79 | <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> |
63 | </div> | 80 | </div> |
64 | <br> | 81 | <br> |
65 | - <a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}" role="button">Show more comments</a> | 82 | + <a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}&languages=${videoLang}" role="button">Show more comments</a> |
66 | <br> | 83 | <br> |
67 | ${commentDisplay} | 84 | ${commentDisplay} |
68 | </body> | 85 | </body> |
... | @@ -70,6 +87,7 @@ function showcomment(response, VideoNum){ | ... | @@ -70,6 +87,7 @@ function showcomment(response, VideoNum){ |
70 | `; | 87 | `; |
71 | response.writeHead(200); | 88 | response.writeHead(200); |
72 | response.end(body); | 89 | response.end(body); |
90 | + | ||
73 | } | 91 | } |
74 | 92 | ||
75 | function loadcomment(ApiKey, VideoNum, npt, n, res){ | 93 | function loadcomment(ApiKey, VideoNum, npt, n, res){ |
... | @@ -98,7 +116,7 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ | ... | @@ -98,7 +116,7 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ |
98 | //console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); | 116 | //console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); |
99 | npt = response.data.nextPageToken; | 117 | npt = response.data.nextPageToken; |
100 | 118 | ||
101 | - if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){ | 119 | + if(npt != null){ |
102 | if( n > 1 ){ | 120 | if( n > 1 ){ |
103 | loadcomment(ApiKey, VideoNum, npt, n - 1, res); | 121 | loadcomment(ApiKey, VideoNum, npt, n - 1, res); |
104 | }else{ | 122 | }else{ |
... | @@ -164,6 +182,7 @@ var app = http.createServer(function(request,response){ // request는 브라우 | ... | @@ -164,6 +182,7 @@ var app = http.createServer(function(request,response){ // request는 브라우 |
164 | commentNum = 0; //코멘트 리스트 초기화 | 182 | commentNum = 0; //코멘트 리스트 초기화 |
165 | 183 | ||
166 | videoNum = queryData.videourl; | 184 | videoNum = queryData.videourl; |
185 | + videoLang = queryData.languages; | ||
167 | //유튜브 주소를 그대로 넣었을 때 videoNum 추출 | 186 | //유튜브 주소를 그대로 넣었을 때 videoNum 추출 |
168 | if(URLreg2.test(videoNum)){ | 187 | if(URLreg2.test(videoNum)){ |
169 | let tempvideoNum = url.parse(videoNum,true).query; | 188 | let tempvideoNum = url.parse(videoNum,true).query; | ... | ... |
... | @@ -40,7 +40,7 @@ async function findLanguage(sourceText) { | ... | @@ -40,7 +40,7 @@ async function findLanguage(sourceText) { |
40 | request.post(options, function (error, response, body) { | 40 | request.post(options, function (error, response, body) { |
41 | if (!error && response.statusCode === 200) { | 41 | if (!error && response.statusCode === 200) { |
42 | resultLang = JSON.parse(body).langCode | 42 | resultLang = JSON.parse(body).langCode |
43 | - console.log(resultLang); | 43 | + //console.log(resultLang); |
44 | resolve(resultLang); | 44 | resolve(resultLang); |
45 | } | 45 | } |
46 | else { | 46 | else { | ... | ... |
-
Please register or login to post a comment