Toggle navigation
Toggle navigation
This project
Loading...
Sign in
곽윤철
/
youtube-comment-seperator
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
곽교린
2022-05-07 19:03:23 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
595e942dc7e01f87bc65a404379623f409104706
595e942d
1 parent
ee669977
유튜브 동영상 주소 입력하면 영상하고 코멘트 불러오는 기본적인 구조는 짜놈.
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
13 deletions
CommentLoadingTest.js
test.js
CommentLoadingTest.js
0 → 100644
View file @
595e942
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
=
''
function
loadcomment
(
ApiKey
,
VideoNum
,
npt
,
n
){
return
service
.
commentThreads
.
list
({
"key"
:
ApiKey
,
"part"
:[
"snippet, replies"
],
"videoId"
:
VideoNum
,
//비디오 주소
"maxResults"
:
50
,
"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
);
}
else
{
savednpt
=
npt
;
//만약 댓글을 n번 불러온 후에 댓글이 더 남아있으면 savednpt 갱신
}
}
else
{
console
.
log
(
'end page'
);
// 댓글의 마지막 페이지
}
},
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'
){
videoNum
=
queryData
.
videourl
;
console
.
log
(
videoNum
);
let
npt
=
""
loadcomment
(
apiKey
,
videoNum
,
npt
,
2
).
then
(()
=>
{
setTimeout
(()
=>
{
//딜레이를 주어 강제로 댓글을 여러번 불러오도록 구현, async화 필요.
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" value="
${
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>
<br>
${
commentDisplay
}
</body>
</html>
`
;
response
.
writeHead
(
200
);
response
.
end
(
body
);
},
1000
);
})
}
});
app
.
listen
(
3000
);
\ No newline at end of file
test.js
View file @
595e942
//https://
www.npmjs.com/package/youtube-v3-api
//https://
developers.google.com/youtube/v3/docs/commentThreads/list
//http://khuhub.khu.ac.kr/2019102147/youtube-comment-seperator.git
const
apiKey
=
'AIzaSyCjBrFKnBlGvxsfOD-qJP8nBkdEoqKRHu8'
;
let
videoNum
=
"m1gHR4dJhKU"
;
const
{
google
}
=
require
(
"googleapis"
);
const
service
=
google
.
youtube
(
'v3'
);
var
npt
=
""
service
.
commentThreads
.
list
({
"key"
:
apiKey
,
"part"
:[
"snippet, replies"
],
"videoId"
:
"m1gHR4dJhKU"
//비디오 주소
}).
then
(
function
(
response
)
{
console
.
log
(
"Response"
,
response
);
console
.
log
(
response
.
data
.
items
[
0
].
snippet
.
topLevelComment
.
snippet
.
textDisplay
)
console
.
log
(
response
.
data
.
items
[
1
].
snippet
.
topLevelComment
.
snippet
.
textDisplay
)
},
function
(
err
)
{
console
.
error
(
"Execute error"
,
err
);
});
for
(
let
i
=
0
;
i
<
3
;
i
++
){
console
.
log
(
npt
);
service
.
commentThreads
.
list
({
"key"
:
apiKey
,
"part"
:[
"snippet, replies"
],
"videoId"
:
videoNum
,
//비디오 주소
"maxResults"
:
10
,
"pageToken"
:
npt
}).
then
(
function
(
response
)
{
console
.
log
(
"Response"
,
response
);
console
.
log
(
response
.
data
.
items
[
0
].
snippet
.
topLevelComment
.
snippet
.
textDisplay
);
console
.
log
(
response
.
data
.
items
[
1
].
snippet
.
topLevelComment
.
snippet
.
textDisplay
);
npt
=
JSON
.
parse
(
response
.
data
.
nextPageToken
);
},
function
(
err
)
{
console
.
error
(
"Execute error"
,
err
);
});
}
/*
...
...
Please
register
or
login
to post a comment