Merge branch 'develop' of http://khuhub.khu.ac.kr/2019102147/youtube-comment-seperator into develop
Showing
4 changed files
with
36 additions
and
33 deletions
APIs.json
0 → 100644
... | @@ -7,7 +7,8 @@ var qs = require('querystring'); | ... | @@ -7,7 +7,8 @@ var qs = require('querystring'); |
7 | var path = require('path'); | 7 | var path = require('path'); |
8 | const {google} = require("googleapis"); | 8 | const {google} = require("googleapis"); |
9 | const service = google.youtube('v3'); | 9 | const service = google.youtube('v3'); |
10 | -const apiKey = 'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'; //api키 | 10 | +const apis = require('./APIs.json'); |
11 | +const apiKey = apis.youtubeAPI; //api키 | ||
11 | let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) | 12 | let videoNum = "TpPwI_Lo0YY"; //비디오 주소(예시) |
12 | var videoLang = 'all'; //설정 언어 | 13 | var videoLang = 'all'; //설정 언어 |
13 | const serverIP = "http://localhost:3000" //서버의 주소 | 14 | const serverIP = "http://localhost:3000" //서버의 주소 |
... | @@ -17,29 +18,11 @@ var savednpt = '' | ... | @@ -17,29 +18,11 @@ var savednpt = '' |
17 | const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식 | 18 | const URLreg1 = new RegExp(/https?:\/\/youtu.be\//);//유튜브에서 제공하는 동영상 공유 기능을 사용하여 얻은 URL 형식 |
18 | const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식 | 19 | const URLreg2 = new RegExp(/https?:\/\/www.youtube.com\//);//유튜브 주소를 그대로 넣었을 때 URL형식 |
19 | 20 | ||
20 | -async function showcomment(response, VideoNum){ | 21 | +function showcomment(response, VideoNum){ |
21 | - let sepComment = new Promise((resolve, reject)=>{ | 22 | + let commentDisplay = ''; |
22 | - async function inSepCom(){ | 23 | + for(let iterArr = 0; iterArr < commentNum; iterArr++){ |
23 | - | 24 | + commentDisplay += `<br>${commentList[iterArr].name}<br>${commentList[iterArr].text}<br><br>`; |
24 | - let tempcommentDisplay = ""; | 25 | + } |
25 | - console.log(commentNum); | ||
26 | - | ||
27 | - for(let iterArr = 0; iterArr < commentNum; iterArr++){ | ||
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>`; | ||
32 | - } | ||
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 = ` | 26 | let body = ` |
44 | <!doctype html> | 27 | <!doctype html> |
45 | <html> | 28 | <html> |
... | @@ -100,7 +83,10 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ | ... | @@ -100,7 +83,10 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ |
100 | "maxResults" : 100, | 83 | "maxResults" : 100, |
101 | "pageToken" : npt | 84 | "pageToken" : npt |
102 | }).then(function(response) { | 85 | }).then(function(response) { |
86 | + async function loadAndParse(){ | ||
103 | //console.log("Response", response); | 87 | //console.log("Response", response); |
88 | + tempcommentList = new Array(); | ||
89 | + tempListLen = 0; | ||
104 | for(let iter = 0; iter < response.data.pageInfo.totalResults; iter++){ | 90 | for(let iter = 0; iter < response.data.pageInfo.totalResults; iter++){ |
105 | let templang = papago.getLang(response.data.items[iter].snippet.topLevelComment.snippet.textDisplay) | 91 | let templang = papago.getLang(response.data.items[iter].snippet.topLevelComment.snippet.textDisplay) |
106 | let tempComment = { | 92 | let tempComment = { |
... | @@ -109,15 +95,26 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ | ... | @@ -109,15 +95,26 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ |
109 | 'text' : response.data.items[iter].snippet.topLevelComment.snippet.textDisplay, | 95 | 'text' : response.data.items[iter].snippet.topLevelComment.snippet.textDisplay, |
110 | 'lang' : templang | 96 | 'lang' : templang |
111 | } | 97 | } |
112 | - commentList.push(tempComment); | 98 | + tempcommentList.push(tempComment); |
113 | - commentNum += 1; | 99 | + tempListLen += 1; |
114 | - } | 100 | + } |
101 | + for(let iter = 0; iter < tempListLen; iter++){ | ||
102 | + let langPromise = tempcommentList[iter].lang.then(langData=>{ | ||
103 | + return langData; | ||
104 | + }); | ||
105 | + templang = await langPromise; | ||
106 | + if(videoLang == 'all' || videoLang == templang){ | ||
107 | + commentList.push(tempcommentList[iter]); | ||
108 | + commentNum++; | ||
109 | + } | ||
110 | + } | ||
111 | + | ||
115 | //console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay); | 112 | //console.log(response.data.items[0].snippet.topLevelComment.snippet.textDisplay); |
116 | //console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); | 113 | //console.log(response.data.items[1].snippet.topLevelComment.snippet.textDisplay); |
117 | npt = response.data.nextPageToken; | 114 | npt = response.data.nextPageToken; |
118 | 115 | ||
119 | if(npt != null){ | 116 | if(npt != null){ |
120 | - if( n > 1 ){ | 117 | + if( n > 1 && commentNum < 100){ |
121 | loadcomment(ApiKey, VideoNum, npt, n - 1, res); | 118 | loadcomment(ApiKey, VideoNum, npt, n - 1, res); |
122 | }else{ | 119 | }else{ |
123 | savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신 | 120 | savednpt = npt; //만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신 |
... | @@ -128,6 +125,8 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ | ... | @@ -128,6 +125,8 @@ function loadcomment(ApiKey, VideoNum, npt, n, res){ |
128 | console.log('end page'); // 댓글의 마지막 페이지 | 125 | console.log('end page'); // 댓글의 마지막 페이지 |
129 | showcomment(res, VideoNum); | 126 | showcomment(res, VideoNum); |
130 | } | 127 | } |
128 | + } | ||
129 | + loadAndParse(); | ||
131 | }, | 130 | }, |
132 | function(err) { console.error("Execute error", err); }); | 131 | function(err) { console.error("Execute error", err); }); |
133 | } | 132 | } |
... | @@ -193,7 +192,7 @@ var app = http.createServer(function(request,response){ // request는 브라우 | ... | @@ -193,7 +192,7 @@ var app = http.createServer(function(request,response){ // request는 브라우 |
193 | console.log(videoNum); | 192 | console.log(videoNum); |
194 | let npt = "" | 193 | let npt = "" |
195 | if(queryData.nextpage != null) npt = queryData.nextpage | 194 | if(queryData.nextpage != null) npt = queryData.nextpage |
196 | - loadcomment(apiKey,videoNum,npt,2, response); | 195 | + loadcomment(apiKey,videoNum,npt,5, response); |
197 | } | 196 | } |
198 | }); | 197 | }); |
199 | app.listen(3000); | 198 | app.listen(3000); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
api키.txt
deleted
100644 → 0
1 | -AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8 | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | - | 1 | +const apis = require('./APIs.json'); |
2 | -var client_id = '6hpmInU0TCrQIiiJwgJX'; | 2 | +var client_id = apis.papagoClientID; |
3 | -var client_secret = 'jpYZbkj0_g'; | 3 | +var client_secret = apis.papagoClientSecret; |
4 | // var query = "언어를 감지할 문장을 입력하세요."; | 4 | // var query = "언어를 감지할 문장을 입력하세요."; |
5 | // app.get('/detectLangs', function (req, res) { | 5 | // app.get('/detectLangs', function (req, res) { |
6 | // var api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | 6 | // var api_url = 'https://openapi.naver.com/v1/papago/detectLangs'; | ... | ... |
-
Please register or login to post a comment