Automatically switch proxy

Signed-off-by: loicbersier <loic.bersier1@gmail.com>
This commit is contained in:
loicbersier 2020-09-14 15:37:28 +02:00
parent f4ea0b4605
commit 73581ce89f
2 changed files with 37 additions and 6 deletions

View file

@ -164,7 +164,7 @@ class ytpCommand extends Command {
} }
if (args.add) { if (args.add) {
if (args.proxy) { if (args.proxy && !args.proxyAuto) { // args.proxyAuto is only provided when the command is run after a error 429
args.proxy = args.proxy -1; args.proxy = args.proxy -1;
if (!proxy[args.proxy]) args.proxy = 0; if (!proxy[args.proxy]) args.proxy = 0;
} }
@ -187,9 +187,24 @@ class ytpCommand extends Command {
return downloader(url, options, `./asset/ytp/userVid/${message.id}.mp4`) return downloader(url, options, `./asset/ytp/userVid/${message.id}.mp4`)
.on('error', (err) => { .on('error', (err) => {
if (err.includes('HTTP Error 429: Too Many Requests')) {
if (args.proxy != null) {
args.proxy = args.proxy + 1;
} else {
args.proxy = 0;
args.proxyAuto = true;
}
if (!proxy[args.proxy]) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked along with its proxy');
loadingmsg.delete(); loadingmsg.delete();
if (err.includes('HTTP Error 429: Too Many Requests')) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked, you can try again with the `--proxy #` option ( can see the list of proxy with --listproxy ) and hope it work.'); return this.client.commandHandler.runCommand(message, this.client.commandHandler.findCommand('download'), args);
}
if (err.includes('Error: status code 403')) return message.channel.send('`HTTP Error 403: Forbidden.`\nThe video you tried to download is not publicly available therefor the bot can\'t download it.');
return message.channel.send(err, { code: true }); return message.channel.send(err, { code: true });
}) })
.on('end', async output => { .on('end', async output => {
const hash = md5File.sync(output); const hash = md5File.sync(output);

View file

@ -51,6 +51,7 @@ class DownloadCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
console.log(args.proxy);
if (args.listproxy) { if (args.listproxy) {
let proxys = []; let proxys = [];
@ -74,8 +75,8 @@ class DownloadCommand extends Command {
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>'); let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
let filename = `${message.id}_video`; let filename = `${message.id}_video`;
if (args.proxy) { if (args.proxy && !args.proxyAuto) { // args.proxyAuto is only provided when the command is run after a error 429
args.proxy = args.proxy -1; args.proxy = args.proxy - 1;
if (!proxy[args.proxy]) args.proxy = 0; if (!proxy[args.proxy]) args.proxy = 0;
} }
@ -91,7 +92,22 @@ class DownloadCommand extends Command {
downloader(args.link.href, args.proxy != null ? ['--proxy', proxy[args.proxy].ip] : null, `${os.tmpdir()}/${filename}.mp4`) downloader(args.link.href, args.proxy != null ? ['--proxy', proxy[args.proxy].ip] : null, `${os.tmpdir()}/${filename}.mp4`)
.on('error', async err => { .on('error', async err => {
if (err.includes('HTTP Error 429: Too Many Requests')) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked, you can try again with the `--proxy #` option ( can see the list of proxy with --listproxy ) and hope it work.'); if (err.includes('HTTP Error 429: Too Many Requests')) {
if (args.proxy != null) {
args.proxy = args.proxy + 1;
} else {
args.proxy = 0;
args.proxyAuto = true;
}
if (!proxy[args.proxy]) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked along with its proxy');
loadingmsg.delete();
return this.client.commandHandler.runCommand(message, this.client.commandHandler.findCommand('download'), args);
}
if (err.includes('Error: status code 403')) return message.channel.send('`HTTP Error 403: Forbidden.`\nThe video you tried to download is not publicly available therefor the bot can\'t download it.');
return message.channel.send(err, { code: true }); return message.channel.send(err, { code: true });
}) })
.on('end', async output => { .on('end', async output => {