-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt.js
102 lines (93 loc) · 2.69 KB
/
yt.js
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
const fs = require('fs');
const ytdl = require('ytdl-core');
const colors = require('colors');
const stringSimilarity = require("string-similarity");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let dwnloadDir = './downloaded/';
let fetchFolder = './otherdownloads/';
let mode;
if (!fs.existsSync(dwnloadDir)){
fs.mkdirSync(dwnloadDir);
}
let f = 0;
let videosArray = [];
let files = [];
fs.readdirSync(fetchFolder).forEach(file => {
files.push(file.replace('.mp3', ''));
console.log(("(===) " + file.replace('.mp3', '')).green);
});
async function download(videos) {
for (i=0; videos.length > i; i++) {
let url = videos[i];
let probsError = false;
let info = await ytdl.getInfo(url).catch((err) => {
console.log("Fatal fetching error occured. Skipped: " + url);
console.log(err);
f++
probsError = true;
});
if (probsError) {
continue
}
let name = info.videoDetails.title;
name = name.replace(/\\|\/|\:|\*|\?|\"|\<|\>|\|/g, '');
let percent = 0;
if (files.length > 0) {
let matched = stringSimilarity.findBestMatch(name, files);
percent = matched.bestMatch.rating*100;
let prcntStr = "(" + percent + "%) " + name + "\n\u200b[" + files[matched.bestMatchIndex] + "]";
if (percent >= 80) {
console.log((prcntStr).blue);
}
else if (percent >= 50) {
console.log((prcntStr).yellow);
}
else {
console.log((prcntStr).magenta);
}
}
if (files.length === 0 || percent < 50) {
let wrtStrm = await fs.createWriteStream(dwnloadDir + name + ".mp3");
console.log("Downloading url " + url + " under filename " + name + ".mp3");
await ytdl.downloadFromInfo(info, { filter: 'audioonly', quality: 'highestaudio' })
.on('error', (err) => {
console.log(("Couldn't get stream from " + url + " (" + name + ") with preferred quality.").red);
console.log(err);
f++
})
.pipe(wrtStrm);
wrtStrm.on('error', (err) => {
console.log(("(ERR) " + name).red);
console.log(err);
f++
});
wrtStrm.on('finish', () => {
console.log(("(+) " + name).green);
f++
})
}
else {
f++
}
}
}
console.log("Please input your URLs. Hit enter twice when you're done.");
rl.on('line', (input) => {
if (input === '') {
rl.emit('close');
return
}
videosArray.push(input);
})
rl.on('close', async () =>
setInterval(() => {
if (f === videosArray.length) {
console.log("All pending files have been successfully downloaded.");
process.exit()
}
}, 1000);
});