download.js
2 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
/*var request = require('request');
var optionParams = {
q:"염따",
part:"snippet",
key:"AIzaSyCgGa6aM7taXs4bajtYukbc_EQAKTLVTNc",
type:"video",
maxResult:2,
regionCode:"KR",
order:"viewCount"
};
optionParams.q = encodeURI(optionParams.q);
var url = "https://www.googleapis.com/youtube/v3/search?";
for (var option in optionParams)
{
url+=option+"="+optionParams[option]+"&";
}
url = url.substr(0,url.length-1);
var VideoIds = new Array();
var idx = 0;
request(url, function(err,res,body){
//console.log(body)
var data = JSON.parse(body).items;
for(var content in data)
{
VideoIds[idx]=(data[content].id.videoId);
idx++;
}
console.log(VideoIds.length);
console.log(VideoIds[0]);
console.log(typeof(VideoIds[1]));
//다운로드 부분
var fs = require('fs');
var youtubedl = require('youtube-dl');
var video = youtubedl('http://www.youtube.com/watch?v='.concat(VideoIds[1]));
video.on('info',function(info)
{
console.log('Download started');
console.log('filename : '+ info.filename);
console.log('size : '+info.size);
});
video.pipe(fs.createWriteStream('justlikethat.mp4'));
});
*/
var request=require('request');
var optionParams={
q:"코코몽",
part:"snippet",
key:"AIzaSyCgGa6aM7taXs4bajtYukbc_EQAKTLVTNc",
type:"video",
maxResults:10,
regionCode:"KR",
order:"viewCount"
};
//한글을 검색어로 전달하기 위해선 url encoding 필요!
optionParams.q=encodeURI(optionParams.q);
var url="https://www.googleapis.com/youtube/v3/search?";
for(var option in optionParams){
url+=option+"="+optionParams[option]+"&";
}
//url의마지막에 붙어있는 & 정리
url=url.substr(0, url.length-1);
request(url, function(err, res, body){
// console.log(body)
//json형식을 서버로 부터 받음
var data=JSON.parse(body).items;
for(var content in data){
//youtube downloader에 videoId 넘기면 됨.
console.log(data[content].snippet.title+" : "+data[content].id.videoId);
}
});