Showing
9 changed files
with
136 additions
and
48 deletions
1 | -var request = require('request'); | 1 | +var http = require('http'); |
2 | - | 2 | +var fs = require('fs'); |
3 | -var optionParams = { | 3 | +var url = require('url'); |
4 | - q:"저라뎃", | 4 | +var qs = require('querystring'); |
5 | - part:"snippet", | 5 | + |
6 | - key:"AIzaSyCgGa6aM7taXs4bajtYukbc_EQAKTLVTNc", | 6 | +function templateHTML(title, list, body){ |
7 | - type:"video", | 7 | + return ` |
8 | - maxResult2:5, | 8 | + <!doctype html> |
9 | - regionCode:"KR", | 9 | + <html> |
10 | - videoDuration:"short" | 10 | + <head> |
11 | -}; | 11 | + <title>Youtube MPL - ${title}</title> |
12 | -optionParams.q = encodeURI(optionParams.q); | 12 | + <meta charset="utf-8"> |
13 | - | 13 | + </head> |
14 | -var url = "https://www.googleapis.com/youtube/v3/search?"; | 14 | + <body> |
15 | -for (var option in optionParams) | 15 | + <h1><a href="/">Youtube MPL</a></h1> |
16 | -{ | 16 | + ${list} |
17 | - url+=option+"="+optionParams[option]+"&"; | 17 | + <a href="/create">create</a> |
18 | + ${body} | ||
19 | + </body> | ||
20 | + </html> | ||
21 | + `; | ||
18 | } | 22 | } |
19 | - | 23 | +function templateList(filelist){ |
20 | -url = url.substr(0,url.length-1); | 24 | + var list = '<ul>'; |
21 | - | 25 | + var i = 0; |
22 | -var VideoIds = new Array(); | 26 | + while(i < filelist.length){ |
23 | -var idx = 0; | 27 | + list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</a></li>`; |
24 | -request(url, function(err,res,body){ | 28 | + i = i + 1; |
25 | - //console.log(body) | 29 | + } |
26 | - var data = JSON.parse(body).items; | 30 | + list = list+'</ul>'; |
27 | - | 31 | + return list; |
28 | - for(var content in data) | 32 | +} |
29 | - { | 33 | + |
30 | - VideoIds[idx]=(data[content].id.videoId); | 34 | +var app = http.createServer(function(request,response){ |
31 | - idx++; | 35 | + var _url = request.url; |
36 | + var queryData = url.parse(_url, true).query; | ||
37 | + var pathname = url.parse(_url, true).pathname; | ||
38 | + if(pathname === '/'){ | ||
39 | + if(queryData.id === undefined){ | ||
40 | + fs.readdir('./data', function(error, filelist){ | ||
41 | + var title = 'Welcome'; | ||
42 | + var description = 'Youtube MPL!!'; | ||
43 | + var list = templateList(filelist); | ||
44 | + var template = templateHTML(title, list, `<h2>${title}</h2>${description}`); | ||
45 | + response.writeHead(200); | ||
46 | + response.end(template); | ||
47 | + }); | ||
48 | + } else { | ||
49 | + fs.readdir('./data', function(error, filelist){ | ||
50 | + fs.readFile(`data/${queryData.id}`, 'utf8', function(err, description){ | ||
51 | + var title = queryData.id; | ||
52 | + var list = templateList(filelist); | ||
53 | + var template = templateHTML(title, list, `<h2>${title}</h2>${description}`); | ||
54 | + response.writeHead(200); | ||
55 | + response.end(template); | ||
56 | + }); | ||
57 | + }); | ||
58 | + } | ||
59 | + } else if(pathname === '/create'){ | ||
60 | + fs.readdir('./data', function(error, filelist){ | ||
61 | + var title = 'Download files'; | ||
62 | + var list = templateList(filelist); | ||
63 | + var template = templateHTML(title, list, ` | ||
64 | + <form action="http://localhost:3000/create_process" method="post"> | ||
65 | + <p><input type="text" name="title" placeholder="title"></p> | ||
66 | + <p><input type="text" name="count" placeholder="count"></p> | ||
67 | + <p> | ||
68 | + <input type="submit"> | ||
69 | + </p> | ||
70 | + </form> | ||
71 | + `); | ||
72 | + response.writeHead(200); | ||
73 | + response.end(template); | ||
74 | + }); | ||
75 | + } else if(pathname === '/create_process'){ | ||
76 | + var body = ''; | ||
77 | + request.on('data', function(data){ | ||
78 | + body = body + data; | ||
79 | + }); | ||
80 | + request.on('end', function(){ | ||
81 | + var post = qs.parse(body); | ||
82 | + var title = post.title; | ||
83 | + var count = post.count; | ||
84 | + fs.writeFile(`data/${title}`, count, 'utf8', function(err){ | ||
85 | + response.writeHead(302, {Location: `http://localhost:3000/`}); | ||
86 | + response.end(); | ||
87 | + }) | ||
88 | + }); | ||
89 | + } else { | ||
90 | + response.writeHead(404); | ||
91 | + response.end('Not found'); | ||
32 | } | 92 | } |
33 | - console.log(VideoIds.length); | ||
34 | - console.log(VideoIds[0]); | ||
35 | - console.log(typeof(VideoIds[1])); | ||
36 | - | ||
37 | - //다운로드 부분 | ||
38 | - var fs = require('fs'); | ||
39 | - var youtubedl = require('youtube-dl'); | ||
40 | - var video = youtubedl('http://www.youtube.com/watch?v='.concat(VideoIds[1])); | ||
41 | - | ||
42 | - video.on('info',function(info) | ||
43 | - { | ||
44 | - console.log('Download started'); | ||
45 | - console.log('filename : '+ info.filename); | ||
46 | - console.log('size : '+info.size); | ||
47 | - }); | ||
48 | - | ||
49 | - video.pipe(fs.createWriteStream('justlikethat.mp4')); | ||
50 | }); | 93 | }); |
51 | - | 94 | +app.listen(3000); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
download.js
0 → 100644
1 | +var request = require('request'); | ||
2 | +var optionParams = { | ||
3 | + q:title, | ||
4 | + part:"snippet", | ||
5 | + key:"AIzaSyCgGa6aM7taXs4bajtYukbc_EQAKTLVTNc", | ||
6 | + type:"video", | ||
7 | + maxResult2:count, | ||
8 | + regionCode:"KR", | ||
9 | + videoDuration:"short" | ||
10 | +}; | ||
11 | +optionParams.q = encodeURI(optionParams.q); | ||
12 | +var url = "https://www.googleapis.com/youtube/v3/search?"; | ||
13 | +for (var option in optionParams) | ||
14 | +{ | ||
15 | + url+=option+"="+optionParams[option]+"&"; | ||
16 | +} | ||
17 | +url = url.substr(0,url.length-1); | ||
18 | +var VideoIds = new Array(); | ||
19 | +var idx = 0; | ||
20 | +request(url, function(err,res,body){ | ||
21 | + //console.log(body) | ||
22 | + var data = JSON.parse(body).items; | ||
23 | + for(var content in data) | ||
24 | + { | ||
25 | + VideoIds[idx]=(data[content].id.videoId); | ||
26 | + idx++; | ||
27 | + } | ||
28 | + console.log(VideoIds.length); | ||
29 | + console.log(VideoIds[0]); | ||
30 | + console.log(typeof(VideoIds[1])); | ||
31 | + //다운로드 부분 | ||
32 | + var fs = require('fs'); | ||
33 | + var youtubedl = require('youtube-dl'); | ||
34 | + var video = youtubedl('http://www.youtube.com/watch?v='.concat(VideoIds[1])); | ||
35 | + video.on('info',function(info) | ||
36 | + { | ||
37 | + console.log('Download started'); | ||
38 | + console.log('filename : '+ info.filename); | ||
39 | + console.log('size : '+info.size); | ||
40 | + }); | ||
41 | + video.pipe(fs.createWriteStream('justlikethat.mp4')); | ||
42 | +}); |
video/justlikethat.mp4
0 → 100644
This file is too large to display.
video/myvideo.mp4
0 → 100644
This file is too large to display.
video/myvideo1.mp4
0 → 100644
This file is too large to display.
video/myvideo3.mp4
0 → 100644
This file is too large to display.
-
Please register or login to post a comment