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
75 additions
and
56 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,60 +17,77 @@ var savednpt = '' | ... | @@ -16,60 +17,77 @@ 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(){ |
22 | - console.log(commentNum); | 23 | + |
23 | - for(let iterArr = 0; iterArr < commentNum; iterArr++){ | 24 | + let tempcommentDisplay = ""; |
24 | - commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>` | 25 | + console.log(commentNum); |
25 | - } | 26 | + |
26 | - body = ` | 27 | + for(let iterArr = 0; iterArr < commentNum; iterArr++){ |
27 | - <!doctype html> | 28 | + await commentList[iterArr].lang.then((languageData) =>{ |
28 | - <html> | 29 | + //console.log(languageData); |
29 | - <head> | 30 | + if(videoLang == 'all'){ |
30 | - <title>Youtube Comment</title> | 31 | + tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`; |
31 | - <meta charset="utf-8"> | 32 | + } |
32 | - <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous"> | 33 | + else if(videoLang == languageData){ |
33 | - <link rel="preconnect" href="https://fonts.googleapis.com"> | 34 | + tempcommentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`; |
34 | - <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | 35 | + } |
35 | - <link href="https://fonts.googleapis.com/css2?family=Permanent+Marker&display=swap" rel="stylesheet"> | 36 | + }); |
36 | - <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> | 37 | + } |
37 | - </head> | 38 | + return tempcommentDisplay |
38 | - <body style="margin:5%"> | 39 | + } |
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> | 40 | + resolve(inSepCom()); |
40 | - <form action="${serverIP}/search" method="get"> | 41 | + }); |
41 | - <div class="form-group" style="font-family: 'Ubuntu', sans-serif;"> | 42 | + let commentDisplay = await sepComment; |
42 | - <input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;"> | 43 | + let body = ` |
43 | - <label for="language-select">Choose a language:</label> | 44 | + <!doctype html> |
44 | - <select class="form-control" name="languages" id="language-select" style="display:inline-flex;"> | 45 | + <html> |
45 | - <option value="all" selected>--Please choose an option--</option> | 46 | + <head> |
46 | - <option value="ko">한국어</option> | 47 | + <title>Youtube Comment</title> |
47 | - <option value="en">English</option> | 48 | + <meta charset="utf-8"> |
48 | - <option value="ja">日本語</option> | 49 | + <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous"> |
49 | - <option value="zh-CN">中國語</option> | 50 | + <link rel="preconnect" href="https://fonts.googleapis.com"> |
50 | - <option value="es">español</option> | 51 | + <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> |
51 | - <option value="fr">Français</option> | 52 | + <link href="https://fonts.googleapis.com/css2?family=Permanent+Marker&display=swap" rel="stylesheet"> |
52 | - <option value="de">Deutsch</option> | 53 | + <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> |
53 | - </select> | 54 | + </head> |
54 | - <p align="right" style="margin-top:1%"> | 55 | + <body style="margin:5%"> |
55 | - <button type="submit" class="btn btn-danger">Display Comments</button> | 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> |
56 | - </p> | 57 | + <form action="${serverIP}/search" method="get"> |
57 | - </div> | 58 | + <div class="form-group" style="font-family: 'Ubuntu', sans-serif;"> |
58 | - </form> | 59 | + <input type="text" name="videourl" placeholder="Type in video Url" class="form-control" style="margin-bottom:10px;" value=${VideoNum}> |
59 | - <br> | 60 | + <label for="language-select">Choose a language:</label> |
60 | - <br> | 61 | + <select class="form-control" name="languages" id="language-select" style="display:inline-flex;"> |
61 | - <div class="embed-responsive embed-responsive-16by9" style="text-align:center;"> | 62 | + <option value="all" selected>--Please choose an option--</option> |
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> | 63 | + <option value="ko">한국어</option> |
63 | - </div> | 64 | + <option value="en">English</option> |
64 | - <br> | 65 | + <option value="ja">日本語</option> |
65 | - <a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}" role="button">Show more comments</a> | 66 | + <option value="zh-CN">中國語</option> |
66 | - <br> | 67 | + <option value="es">español</option> |
67 | - ${commentDisplay} | 68 | + <option value="fr">Français</option> |
68 | - </body> | 69 | + <option value="de">Deutsch</option> |
69 | - </html> | 70 | + </select> |
70 | - `; | 71 | + <p align="right" style="margin-top:1%"> |
71 | - response.writeHead(200); | 72 | + <button type="submit" class="btn btn-danger">Display Comments</button> |
72 | - response.end(body); | 73 | + </p> |
74 | + </div> | ||
75 | + </form> | ||
76 | + <br> | ||
77 | + <br> | ||
78 | + <div class="embed-responsive embed-responsive-16by9" style="text-align:center;"> | ||
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> | ||
80 | + </div> | ||
81 | + <br> | ||
82 | + <a class="btn btn-dark" href="${serverIP}/search?videourl=${VideoNum}&nextpage=${savednpt}&languages=${videoLang}" role="button">Show more comments</a> | ||
83 | + <br> | ||
84 | + ${commentDisplay} | ||
85 | + </body> | ||
86 | + </html> | ||
87 | + `; | ||
88 | + response.writeHead(200); | ||
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