-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvMP3.V2.js
153 lines (144 loc) · 7.64 KB
/
ConvMP3.V2.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
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
class Dl {
download(name){
const fs = require('fs');
const puppeteer = require('puppeteer');
const colors = require('colors');
const exec = require('child_process').exec;
(async () => {
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function openSubl() {
const child = exec('subl C:\\Users\\Corentin\\Documents\\dev\\ConvMP3 V2\\back.txt',
(error, stdout, stderr) => {
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
}
function WriteError(errorback) {
var data = fs.readFileSync('C:\\Users\\Corentin\\Documents\\dev\\ConvMP3\\back.txt', 'utf8');
var today = new Date();
var date = today.getFullYear() + '/' + (today.getMonth() + 1) + '/'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' | ' + time;
var message = data + "\n" + errorback + " _____" + dateTime;
fs.writeFile('C:\\Users\\Corentin\\Documents\\dev\\ConvMP3\\back.txt', message, function(erreur) {
if (erreur) {
console.log(erreur.toString().red);
}
})
openSubl();
}
function goodEndDownload(songName) {
console.log(" {".cyan.bgMagenta + songName.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[==========100%]".blue + "<--_____--Download complete--_____-->".yellow);
console.log("====================================================================================".green);
console.log("\\i/".green.bold + " {".cyan.bgMagenta + songName.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download finished " + "[_SUCCESS_]".green.bold);
console.log("====================================================================================".green);
}
const browser = await puppeteer.launch();//Open browser with puppeteer, to see the window, add {headless: false} in parameters
console.log("\\i/".green.bold + " Starting download process for : " + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta);
console.log("====================================================================================".green);
//Open a page with Youtube
const page = await browser.newPage();
page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
console.log(" {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[10%---------]".blue + "<--_____--Start Youtube--_____-->".yellow);
var errorback = await page.goto('https://www.youtube.com/')
.catch(function (e) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " Youtube n'est pas accessible pour le moment /|\\ ".red + " " + e.toString().red);
openSubl();
return(false);
});
if (errorback == false) {
return(false);
};
//Type the name of the song in the youtube searchbar
await page.waitFor(1000);
var errorback = await page.evaluate(() => {
const obj = document.querySelector('input[id="search"]');
obj.select();
})
.catch(function (e) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " La barre de recherche Youtube n'est pas disponible /|\\ ".red + " " + e.toString().red);
openSubl();
return(false);
});
if (errorback == false) {
return(false);
};
await page.keyboard.type(name);
console.log(" {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[==30%-------]".blue + "<--_____--Try to find the song URL--_____-->".yellow);
//Submit the search
await page.waitFor(500);
var errorback = await page.evaluate(() => {
const obj = document.querySelector('button[id="search-icon-legacy"]');
obj.click();
})
.catch(function (e) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " Le bouton de recherche Youtube n'est pas accessible /|\\ ".red + " " + e.toString().red);
openSubl();
return(false);
});
if (errorback == false) {
return(false);
};
//Clic on the video and get the url
console.log(" {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[====50%-----]".blue + "<--_____--Youtube video find--_____-->".yellow);
await page.waitFor(1000);
await page.screenshot({path: 'C:\\Users\\Corentin\\Documents\\dev\\ConvMP4buddy-screenshot.png'});
var errorback = await page.evaluate(() => {
const obj = document.querySelector('a[id="video-title"]');
obj.click();
})
.catch(function (e) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " La vidéo Youtube n'est pas accessible /|\\ ".red + " " + e.toString().red);
openSubl();
return(false);
});
if (errorback == false) {
return(false);
};
const url = page.url();
await page.waitFor(1000);
console.log(" {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[======70%---]".blue + "<--__________--Song URL find--___________-->".yellow);
//Close browser
await browser.close();
//Start downloading of the audio file with youtube-dl
/*
File download option
-a, --batch-file FILE File containing URLs to download ('-' for
stdin), one URL per line. Lines starting
with '#', ';' or ']' are considered as
comments and ignored.
*/
console.log(" {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : " + "[========90%-]".blue + "<--__________--Starting download--___________-->".yellow);
const child = exec('youtube-dl -o "C:\\Users\\Corentin\\Documents\\ConvMP3\\%(title)s.%(ext)s" --extract-audio --audio-format mp3 ' + url,
(error, stdout, stderr) => {
//console.log(`stdout: ${stdout}`);
//console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " Erreur lors du téléchargement /|\\ ".red);
console.log(`exec error: ${error}`.red);
openSubl();
} else {
goodEndDownload(name);
}
});
})()
.catch(function (e) {
console.log("\\i/".green.bold + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " : Download failed " + "[_FAILED_]".red.bold);
console.log("/!\\ ".red + " {".cyan.bgMagenta + name.cyan.bgMagenta.bold + "} ".cyan.bgMagenta + " " + "ERROR :".red.underline + " Une erreur interne est survenue /|\\ ".red + " " + e.toString().red);
});
return(true);
}
}
module.exports.Dl = Dl;