Showing
2 changed files
with
145 additions
and
13 deletions
CommentLoadingTest.js
0 → 100644
1 | +var http = require('http'); | ||
2 | +var fs = require('fs'); //모듈이라 부름 | ||
3 | +var url = require('url'); | ||
4 | +var testFolder = './data'; | ||
5 | +var qs = require('querystring'); | ||
6 | +var path = require('path'); | ||
7 | +const {google} = require("googleapis"); | ||
8 | +const service = google.youtube('v3'); | ||
9 | +const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키 | ||
10 | +let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) | ||
11 | +var commentList = new Array(); | ||
12 | +var commentNum = 0; | ||
13 | +var savednpt = '' | ||
14 | + | ||
15 | +function loadcomment(ApiKey, VideoNum, npt, n){ | ||
16 | + return service.commentThreads.list({ | ||
17 | + "key":ApiKey, | ||
18 | + "part":[ | ||
19 | + "snippet, replies" | ||
20 | + ], | ||
21 | + "videoId":VideoNum,//비디오 주소 | ||
22 | + "maxResults" : 50, | ||
23 | + "pageToken" : npt | ||
24 | + }).then(function(response) { | ||
25 | + //console.log("Response", response); | ||
26 | + for(let iter = 0; iter < response.data.pageInfo.totalResults; iter++){ | ||
27 | + let tempComment = { | ||
28 | + 'name' : response.data.items[iter].snippet.topLevelComment.snippet.authorDisplayName, | ||
29 | + 'image' : response.data.items[iter].snippet.topLevelComment.snippet.authorProfileImageUrl, | ||
30 | + 'text' : response.data.items[iter].snippet.topLevelComment.snippet.textDisplay | ||
31 | + } | ||
32 | + commentList.push(tempComment); | ||
33 | + commentNum += 1; | ||
34 | + } | ||
35 | + //console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay); | ||
36 | + //console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); | ||
37 | + npt = response.data.nextPageToken; | ||
38 | + | ||
39 | + if(response.data.pageInfo.totalResults == response.data.pageInfo.resultsPerPage){ | ||
40 | + if( n > 1 ){ | ||
41 | + loadcomment(ApiKey, VideoNum, npt, n - 1); | ||
42 | + }else{ | ||
43 | + savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신 | ||
44 | + } | ||
45 | + }else{ | ||
46 | + console.log('end page'); // 댓글의 마지막 페이지 | ||
47 | + } | ||
48 | + }, | ||
49 | + function(err) { console.error("Execute error", err); }); | ||
50 | +} | ||
51 | + | ||
52 | +var app = http.createServer(function(request,response){ // request는 브라우저가 주는 정보, response는 우리가 브라우저에게 줄 정보 | ||
53 | + var _url = request.url; // query string이 담김 ex) /?id=HTML | ||
54 | + var queryData = url.parse(_url, true).query; // query string을 추출하였음 나중에 불러올때는 queryData.(값의 이름) | ||
55 | + var pathname = url.parse(_url,true).pathname; | ||
56 | + var body; | ||
57 | + if(pathname === '/'){ | ||
58 | + body = ` | ||
59 | + <!doctype html> | ||
60 | + <html> | ||
61 | + <head> | ||
62 | + <title>Youtube Comment</title> | ||
63 | + <meta charset="utf-8"> | ||
64 | + </head> | ||
65 | + <body> | ||
66 | + <form action="http://localhost:3000/search" method="get"> | ||
67 | + <p> | ||
68 | + <textarea name="videourl" placeholder="Write your video Url"></textarea> | ||
69 | + </p> | ||
70 | + <p> | ||
71 | + <input type="submit"> | ||
72 | + </p> | ||
73 | + </form> | ||
74 | + </body> | ||
75 | + </html> | ||
76 | + `; | ||
77 | + response.writeHead(200); | ||
78 | + response.end(body); | ||
79 | + } | ||
80 | + else if(pathname === '/search'){ | ||
81 | + videoNum = queryData.videourl; | ||
82 | + console.log(videoNum); | ||
83 | + let npt = "" | ||
84 | + loadcomment(apiKey,videoNum,npt,2).then(()=>{ | ||
85 | + setTimeout(()=>{ //딜레이를 주어 강제로 댓글을 여러번 불러오도록 구현, async화 필요. | ||
86 | + let commentDisplay = ""; | ||
87 | + console.log(commentNum); | ||
88 | + for(let iterArr = 0; iterArr < commentNum; iterArr++){ | ||
89 | + commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>` | ||
90 | + } | ||
91 | + body = ` | ||
92 | + <!doctype html> | ||
93 | + <html> | ||
94 | + <head> | ||
95 | + <title>Youtube Comment</title> | ||
96 | + <meta charset="utf-8"> | ||
97 | + </head> | ||
98 | + <body> | ||
99 | + <form action="http://localhost:3000/search" method="get"> | ||
100 | + <p> | ||
101 | + <textarea name="videourl" placeholder="Write your video Url" value="${videoNum}"></textarea> | ||
102 | + </p> | ||
103 | + <p> | ||
104 | + <input type="submit"> | ||
105 | + </p> | ||
106 | + </form> | ||
107 | + <br> | ||
108 | + <br> | ||
109 | + <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> | ||
110 | + | ||
111 | + <br> | ||
112 | + <br> | ||
113 | + ${commentDisplay} | ||
114 | + </body> | ||
115 | + </html> | ||
116 | + `; | ||
117 | + response.writeHead(200); | ||
118 | + response.end(body); | ||
119 | + },1000); | ||
120 | + | ||
121 | + }) | ||
122 | + } | ||
123 | +}); | ||
124 | +app.listen(3000); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | -//https://www.npmjs.com/package/youtube-v3-api | 1 | +//https://developers.google.com/youtube/v3/docs/commentThreads/list |
2 | //http://khuhub.khu.ac.kr/2019102147/youtube-comment-seperator.git | 2 | //http://khuhub.khu.ac.kr/2019102147/youtube-comment-seperator.git |
3 | 3 | ||
4 | const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; | 4 | const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; |
5 | +let videoNum = "m1gHR4dJhKU"; | ||
5 | const {google} = require("googleapis"); | 6 | const {google} = require("googleapis"); |
6 | const service = google.youtube('v3'); | 7 | const service = google.youtube('v3'); |
8 | +var npt = "" | ||
7 | 9 | ||
8 | -service.commentThreads.list({ | 10 | +for(let i = 0; i < 3; i++){ |
9 | - "key":apiKey, | 11 | + console.log(npt); |
10 | - "part":[ | 12 | + service.commentThreads.list({ |
11 | - "snippet, replies" | 13 | + "key":apiKey, |
12 | - ], | 14 | + "part":[ |
13 | - "videoId":"m1gHR4dJhKU"//비디오 주소 | 15 | + "snippet, replies" |
14 | -}).then(function(response) { | 16 | + ], |
15 | - console.log("Response", response); | 17 | + "videoId":videoNum,//비디오 주소 |
16 | - console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay) | 18 | + "maxResults" : 10, |
17 | - console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay) | 19 | + "pageToken" : npt |
18 | - }, | 20 | + }).then(function(response) { |
19 | - function(err) { console.error("Execute error", err); }); | 21 | + console.log("Response", response); |
22 | + console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay); | ||
23 | + console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); | ||
24 | + npt = JSON.parse(response.data.nextPageToken); | ||
25 | + }, | ||
26 | + function(err) { console.error("Execute error", err); }); | ||
27 | +} | ||
20 | 28 | ||
21 | 29 | ||
22 | /* | 30 | /* | ... | ... |
-
Please register or login to post a comment