-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloader.js
More file actions
224 lines (208 loc) · 10.8 KB
/
Downloader.js
File metadata and controls
224 lines (208 loc) · 10.8 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const ytdl = require('ytdl-core');
const fs = require('fs');
const path = require('path');
const {ffmpex} = require('./FFmpex');
const {lobby} = require('./Side_Functions/lobby');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports ={
Path: function path(){
return (__dirname)
},
Downloader: function Downloader(pathv, link, fileformat, quality, choice, direction, abspath, AACENABLE){
function confirm(link, fileformat, quality, path){
console.log(`CHOOSEN PARAMETERS:\n\n (LINK: ${link}); (FILE FORMAT: ${fileformat});\n\n (QUALITY: ${quality}); (DIRECTORY: ${path})\n\n`)
var videotitle;
sleep(3000);}
function addfolder(pathv){
if(direction === 2){
const dirpath = path.resolve(abspath);
try {
if (!fs.existsSync(dirpath)) {
fs.mkdirSync(dirpath);
}
} catch (err) {
console.error(err);
}
}
}
function verifypath(pathv){
addfolder(pathv)
const directoryPath = path.resolve(pathv);
fs.open(directoryPath, 'w', function(err){if(err) throw err})
return directoryPath
}
function titleressourcer(title){
let n = 0;
let m = 0
for (let i = 0; i < title.length; i++) {
if(m = 0){
console.log('WAITING FOR PHASE 1 OF TITLERESSOURCER.JS \n');
sleep(2000);}
if (title[i] === '/' || title[i] === '?' || title[i] === '\\' || title[i] === '"' || title[i] === '*' || title[i] === '>' || title[i] === '<' || title[i] === '|' || title[i] === ':') {
title = title.substring(0, i) + '_' + title.substring(i + 1);
}
if(n = 0){
console.log('WAITING FOR PHASE 2 OF TITLERESSOURCER.JS \n');
sleep(2000);}
const chars = /[`~!@#$%^&*()<>?;:'"{},.]/g;
title = title.replace(chars, "_");
const emojiRegex = /[^\w\s_\p{Emoji}&&[^U+1F1E6-U+1F1FF]]/g;
title = title.replace(emojiRegex, "_");
}
return title
}
function speedlistener(speed, value){
value = 'Gb/s'
if(speed > 10){
speed /= 10;
value = 'Mb/s';
}
else if(speed < 1){
speed *= 10;
value = 'Kb/s';
if(speed < 1){
speed *= 10;
value = 'Bytes/s';
/*if(speed < 1){
speed *= 10;
value = 'Bytes/s';
}*/
}
}
return [speed, value]
}
function ytdlapi(link, pathv, format, quality, title){
pathv = verifypath(pathv)
confirm(link, format, quality, pathv);
var video;
function saveToFile(url, pathv){
lobby()
return new Promise((resolve) => {
try{video = ytdl(url, {quality: quality, filter: choice});}catch(error){
console.log(`\n \x1b[31m YTDL ERROR: Failed to download ${title} \x1b[0m \n`);
}
const output = fs.createWriteStream(pathv);
// emit progress events
let downloadedBytes = 0;
var timeTaken;
var speed;
var bytesd;
var speedynamic = 0;
var points = '';
var startTime = Date.now();
video.on('progress', (chunkLength, downloadedBytes, totalBytes) => {
points += '.';
if(points === '.'){points = '. '}
else if(points === '. .'){points = '.. '}
else if(points === '.. .'){points = '...'}
var differencetime = (Date.now() - startTime);
const percentComplete = downloadedBytes / totalBytes * 100;
speedynamic = (downloadedBytes / differencetime/1024/100); var value = 'Mb/s';
let speeddata = speedlistener(speedynamic, value)
speedynamic = speeddata[0]; value = speeddata[1];
process.stdout.write(`\rDownloading${points} ${percentComplete.toFixed(2)}% | (${speedynamic.toFixed(2)} ${value})`);
if(points === '...'){points = '';}
bytesd = totalBytes;
});
// emit info event to get video metadata
let videoInfo = null;
video.on('info', (info) => {
videoInfo = info;
});
// pipe the video stream to the output file
video.pipe(output);
var unit = 'seconds'
// wait for the download to finish
output.on('finish', (totalBytes) => {
// calculate average download speed
timeTaken = (Date.now() - startTime) / 1000; // in seconds
if(timeTaken > 100){
timeTaken /= 60;
unit = 'minutes';
if(timeTaken > 100){
timeTaken /= 60;
unit = 'hours';
if(timeTaken > 100){
timeTaken /= 24;
unit = 'days';
}
}
}
var value = 'Mb/s'
speed = bytesd / timeTaken / 1024 / 1024; // in MiB/s
speedout = speedlistener(speed, value); speed = speedout[0]; value = speedout[1];
// clear progress bar
process.stdout.clearLine();
process.stdout.cursorTo(0);
// print download summary
const result = `\n \x1b[32mDownloaded ${title} in ${timeTaken.toFixed(2)} ${unit} (avg speed: ${speed.toFixed(2)} ${value})\x1b[0m \n`
startTime = 0;
console.log(result);
resolve();
});
});
}
startTime = Date.now();
const savePromise = saveToFile(link, pathv);
savePromise.then(() => {
console.log('Done!');
if(direction === 2){
//lobby()
}
}).catch((error) => {
console.error('Error:', error.message);
});
}
function mainressourcer (link){
const timeoutId = setTimeout(() => {
console.log('Timeout expired, skipping search.');
return;
}, 5000);
ytdl.getInfo(link)
.then(vinfor => {
var duration = vinfor.videoDetails.lengthSeconds
vinfor = titleressourcer(vinfor.videoDetails.title);
if(fileformat === '.mkv' || fileformat === '.aac' && quality === 'greatest' || fileformat === '.flac' || fileformat === '.wav'){
if(direction === 2) addfolder(abspath)
console.clear()
lobby()
if(fileformat === '.mkv') AACENABLE = null
else if(fileformat === '.aac') AACENABLE = 'AACACTIVATE'
else if(fileformat === '.flac') AACENABLE = 'FLACACTIVATE'
else if(fileformat === '.wav') AACENABLE = 'WAVACTIVATE'
console.log(fileformat)
ffmpex(link, vinfor, abspath, AACENABLE, duration).then(time => {
time *= 60
//console.log(`\n \x1b[32mDownloaded ${vinfor} in ${time.toFixed(2)} seconds\x1b[0m \n`)
})
}
else{
pathv += vinfor + fileformat;
ytdlapi(link, pathv, fileformat, quality, vinfor);
}})
.catch(e =>{
if(e.syscall === 'getaddrinfo'){
console.log('╭───────────────────────────────────────────────────────────────────────────╮');
console.log('│ |');
console.log('│ 🌐 HOST ERROR: POOR/NON-EXISTANT INTERNET CONNECTIVITY! 🌐 │');
console.log('│ |');
console.log('│ Please make sure you are properly connected │');
console.log('│ to internet or and try again later. |');
console.log('╰───────────────────────────────────────────────────────────────────────────╯');
}
else{
console.log('╭───────────────────────────────────────────────────────────────────────────╮');
console.log('│ |');
console.log('│ ❌ MEDIA ERROR: INVALID/NON-EXISTANT YOUTUBE MEDIA URL! ❌ │');
console.log('│ |');
console.log('│ Please make sure you entered an existant │');
console.log('│ YouTube media URL and try again. |');
console.log('╰───────────────────────────────────────────────────────────────────────────╯');
}
})
clearTimeout(timeoutId);
}
mainressourcer(link);
} }