-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguardavideos.js
More file actions
109 lines (95 loc) · 3.47 KB
/
Copy pathguardavideos.js
File metadata and controls
109 lines (95 loc) · 3.47 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var fs = require('fs');
var request = require('request');
var async = require("async");
var youtubedl = require('youtube-dl');
//eval(fs.readFileSync('main.js')+'');
var privateData = require('./private');
var url = privateData.url;
var tortureArray=[];
fs.exists('Tortura Musical/00error.txt', function(exists) {
if(exists) {
console.log('Error list deleted');
fs.unlinkSync('Tortura Musical/00error.txt');
} else {
}
});
function VideoData(index, title, url) {
this.index = index
this.title = title;
this.url = url;
}
function downloadVideo(currentVideo, callback) {
var video = youtubedl(currentVideo.url);
// Optional arguments passed to youtube-dl.
//['--format=mp4'],
// Additional options can be given for calling `child_process.execFile()`.
//{ cwd: __dirname });
var exten="";
video.on('info', function(info) {
var fn=info._filename;
console.log(fn);
//Clean video title for invalid filename characters
var cleanString = fn.replace(/[|&;$%@"<>()\\\/+,]/g, "");
video.pipe(fs.createWriteStream('Tortura Musical/'+currentVideo.index+"_"+cleanString));
//console.log('size: ' + info.size);
});
video.on('error', function error(err) {
console.log('VIDEO FALLADO: '+currentVideo.title, err);
fs.appendFile('Tortura Musical/00error.txt', currentVideo.index+'_'+currentVideo.title+'\n', function (err) {});
});
video.on('end', function() {
callback();
});
}
var queue = async.queue(downloadVideo, 10);
queue.drain = function() {
console.log("All files downloaded");
};
function backupTortura(callback){
//TO DO: Optimize this. Call ¿hourly? for list refreshes instead of on each call.
//In the meanwhile, length=0 is the most efficient approach to this.
tortureArray.length=0;
requestTortura(url,"",function(resultado){callback(resultado);}
);
}
function requestTortura(url, extraParams, callback){
request({
url: url+extraParams,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if(body.items){
for(var i=0; i<body.items.length; i++){
var title=body.items[i].snippet.title;
var videourl=generateURLFromID(body.items[i].snippet.resourceId.videoId);
//console.log(title+','+videourl);
//console.log('');
tortureArray.push(new VideoData(tortureArray.length+1,title,videourl));
}
//console.log(body.nextPageToken);
if(body.nextPageToken){
requestTortura(url,"&pageToken="+body.nextPageToken, callback);
}else{
//console.log(tortureArray);
callback && callback(tortureArray);
}
//console.log(body) // Print the json response
}
}
})
}
function generateURLFromID(videoID){
var baseurl='http://www.youtube.com/watch?v=';
return baseurl+videoID;
}
//Main
backupTortura(function(resultado){
var stream = fs.createWriteStream("Tortura Musical/00SongList.txt");
stream.once('open', function(fd) {
for (var i=0;i<resultado.length;i++){
stream.write(resultado[i].index+'::'+resultado[i].title+'::'+resultado[i].url+'\n');
queue.push(resultado[i]);
}
stream.end();
});
});