Yuncheol Kwak

fix conflict

1 var http = require('http'); 1 var http = require('http');
2 -var fs = require('fs'); //모듈이라 부름 2 +var fs = require('fs');
3 var url = require('url'); 3 var url = require('url');
4 var testFolder = './data'; 4 var testFolder = './data';
5 var qs = require('querystring'); 5 var qs = require('querystring');
...@@ -11,15 +11,55 @@ let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) ...@@ -11,15 +11,55 @@ let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시)
11 var commentList = new Array(); 11 var commentList = new Array();
12 var commentNum = 0; 12 var commentNum = 0;
13 var savednpt = '' 13 var savednpt = ''
14 +const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식
15 +const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식
14 16
15 -function loadcomment(ApiKey, VideoNum, npt, n){ 17 +function showcomment(response, VideoNum){
18 + let body = ''
19 + let commentDisplay = "";
20 + console.log(commentNum);
21 + for(let iterArr = 0; iterArr < commentNum; iterArr++){
22 + commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`
23 + }
24 + body = `
25 + <!doctype html>
26 + <html>
27 + <head>
28 + <title>Youtube Comment</title>
29 + <meta charset="utf-8">
30 + </head>
31 + <body>
32 + <form action="http://localhost:3000/search" method="get">
33 + <p>
34 + <textarea name="videourl" placeholder="Write your video Url" >${VideoNum}</textarea>
35 + </p>
36 + <p>
37 + <input type="submit">
38 + </p>
39 + </form>
40 + <br>
41 + <br>
42 + <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>
43 +
44 + <br>
45 + <a href="http://localhost:3000/search?videourl=${VideoNum}&nextpage=${savednpt}"><input type="button" value="댓글 더 불러오기"></a>
46 + <br>
47 + ${commentDisplay}
48 + </body>
49 + </html>
50 + `;
51 + response.writeHead(200);
52 + response.end(body);
53 +}
54 +
55 +function loadcomment(ApiKey, VideoNum, npt, n, res){
16 return service.commentThreads.list({ 56 return service.commentThreads.list({
17 "key":ApiKey, 57 "key":ApiKey,
18 "part":[ 58 "part":[
19 "snippet, replies" 59 "snippet, replies"
20 ], 60 ],
21 "videoId":VideoNum,//비디오 주소 61 "videoId":VideoNum,//비디오 주소
22 - "maxResults" : 50, 62 + "maxResults" : 100,
23 "pageToken" : npt 63 "pageToken" : npt
24 }).then(function(response) { 64 }).then(function(response) {
25 //console.log("Response", response); 65 //console.log("Response", response);
...@@ -38,12 +78,15 @@ function loadcomment(ApiKey, VideoNum, npt, n){ ...@@ -38,12 +78,15 @@ function loadcomment(ApiKey, VideoNum, npt, n){
38 78
39 if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){ 79 if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){
40 if( n > 1 ){ 80 if( n > 1 ){
41 - loadcomment(ApiKey, VideoNum, npt, n - 1); 81 + loadcomment(ApiKey, VideoNum, npt, n - 1, res);
42 }else{ 82 }else{
43 savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신 83 savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신
84 + showcomment(res, VideoNum);
44 } 85 }
45 }else{ 86 }else{
87 + savednpt = '';
46 console.log('end page'); // 댓글의 마지막 페이지 88 console.log('end page'); // 댓글의 마지막 페이지
89 + showcomment(res, VideoNum);
47 } 90 }
48 }, 91 },
49 function(err) { console.error("Execute error", err); }); 92 function(err) { console.error("Execute error", err); });
...@@ -82,46 +125,17 @@ var app = http.createServer(function(request,response){ // request는 브라우 ...@@ -82,46 +125,17 @@ var app = http.createServer(function(request,response){ // request는 브라우
82 commentNum = 0; //코멘트 리스트 초기화 125 commentNum = 0; //코멘트 리스트 초기화
83 126
84 videoNum = queryData.videourl; 127 videoNum = queryData.videourl;
128 + //유튜브 주소를 그대로 넣었을 때 videoNum 추출
129 + if(URLreg2.test(videoNum)){
130 + let tempvideoNum = url.parse(videoNum,true).query;
131 + videoNum = tempvideoNum.v;
132 + }
133 + //유튜브 URL공유 기능을 사용하여 얻은 주소를 넣었을 때 videoNum 추출
134 + videoNum = videoNum.replace(URLreg1,"");
85 console.log(videoNum); 135 console.log(videoNum);
86 let npt = "" 136 let npt = ""
87 - loadcomment(apiKey,videoNum,npt,2).then(()=>{ 137 + if(queryData.nextpage != null) npt = queryData.nextpage
88 - setTimeout(()=>{ //딜레이를 주어 강제로 댓글을 여러번 불러오도록 구현, async화 필요. 138 + loadcomment(apiKey,videoNum,npt,2, response);
89 - let commentDisplay = "";
90 - console.log(commentNum);
91 - for(let iterArr = 0; iterArr < commentNum; iterArr++){
92 - commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`
93 - }
94 - body = `
95 - <!doctype html>
96 - <html>
97 - <head>
98 - <title>Youtube Comment</title>
99 - <meta charset="utf-8">
100 - </head>
101 - <body>
102 - <form action="http://localhost:3000/search" method="get">
103 - <p>
104 - <textarea name="videourl" placeholder="Write your video Url" >${videoNum}</textarea>
105 - </p>
106 - <p>
107 - <input type="submit">
108 - </p>
109 - </form>
110 - <br>
111 - <br>
112 - <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>
113 -
114 - <br>
115 - <br>
116 - ${commentDisplay}
117 - </body>
118 - </html>
119 - `;
120 - response.writeHead(200);
121 - response.end(body);
122 - },1000);
123 -
124 - })
125 } 139 }
126 }); 140 });
127 app.listen(3000); 141 app.listen(3000);
...\ No newline at end of file ...\ No newline at end of file
......
1 -{"web":{"client_id":"197854895056-dk8etrtt0otte8ebrra2c9lvt2k0mff4.apps.googleusercontent.com","project_id":"youtubecommentseperator","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","javascript_origins":["http://localhost:3031"]}}
...\ No newline at end of file ...\ No newline at end of file