CommentLoadingTest.js
5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var http = require('http');
var fs = require('fs');
var url = require('url');
var testFolder = './data';
var qs = require('querystring');
var path = require('path');
const {google} = require("googleapis");
const service = google.youtube('v3');
const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키
let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시)
var commentList = new Array();
var commentNum = 0;
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 = "";
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>
<a href="http://localhost:3000/search?videourl=${VideoNum}&nextpage=${savednpt}"><input type="button" value="댓글 더 불러오기"></a>
<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" : 100,
"pageToken" : npt
}).then(function(response) {
//console.log("Response", response);
for(let iter = 0; iter < response.data.pageInfo.totalResults; iter++){
let tempComment = {
'name' : response.data.items[iter].snippet.topLevelComment.snippet.authorDisplayName,
'image' : response.data.items[iter].snippet.topLevelComment.snippet.authorProfileImageUrl,
'text' : response.data.items[iter].snippet.topLevelComment.snippet.textDisplay
}
commentList.push(tempComment);
commentNum += 1;
}
//console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay);
//console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay);
npt = response.data.nextPageToken;
if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){
if( 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); });
}
var app = http.createServer(function(request,response){ // request는 브라우저가 주는 정보, response는 우리가 브라우저에게 줄 정보
var _url = request.url; // query string이 담김 ex) /?id=HTML
var queryData = url.parse(_url, true).query; // query string을 추출하였음 나중에 불러올때는 queryData.(값의 이름)
var pathname = url.parse(_url,true).pathname;
var body;
if(pathname === '/'){
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"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
`;
response.writeHead(200);
response.end(body);
}
else if(pathname === '/search'){
commentList.splice(0);
commentNum = 0; //코멘트 리스트 초기화
videoNum = queryData.videourl;
//유튜브 주소를 그대로 넣었을 때 videoNum 추출
if(URLreg2.test(videoNum)){
let tempvideoNum = url.parse(videoNum,true).query;
videoNum = tempvideoNum.v;
}
//유튜브 URL공유 기능을 사용하여 얻은 주소를 넣었을 때 videoNum 추출
videoNum = videoNum.replace(URLreg1,"");
console.log(videoNum);
let npt = ""
if(queryData.nextpage != null) npt = queryData.nextpage
loadcomment(apiKey,videoNum,npt,2, response);
}
});
app.listen(3000);