Compare commits
2 commits
b4f3592c76
...
db4f058fb4
Author | SHA1 | Date | |
---|---|---|---|
db4f058fb4 | |||
3527ded36f |
2 changed files with 192 additions and 172 deletions
|
@ -1,6 +1,7 @@
|
||||||
const { Command } = require('discord-akairo');
|
const { Command } = require('discord-akairo');
|
||||||
const downloader = require('../../utils/download');
|
const downloader = require('../../utils/download');
|
||||||
const compress = require('../../utils/compress');
|
const compress = require('../../utils/compress');
|
||||||
|
const checkHapi = require('../../utils/checkHapi');
|
||||||
const { proxy, Hapi } = require('../../config.json');
|
const { proxy, Hapi } = require('../../config.json');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
@ -52,108 +53,7 @@ class DownloadCommand extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
async exec(message, args) {
|
async exec(message, args) {
|
||||||
let Embed = this.client.util.embed()
|
if (!args.link) return message.channel.send('Please try again with a valid URL.');
|
||||||
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
|
||||||
.setAuthor(`Downloaded by ${message.author.username}`, message.author.displayAvatarURL(), args.link)
|
|
||||||
.setDescription(args.caption ? args.caption : '')
|
|
||||||
.setFooter(`You can get the original video by clicking on the "downloaded by ${message.author.username}" message!`);
|
|
||||||
|
|
||||||
let compressEmbed = this.client.util.embed()
|
|
||||||
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
|
||||||
.setTitle('This one will need compression!')
|
|
||||||
.setDescription('Starting compression now!')
|
|
||||||
.setFooter('Want it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!');
|
|
||||||
|
|
||||||
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
|
|
||||||
|
|
||||||
|
|
||||||
if (Hapi) {
|
|
||||||
let error = false;
|
|
||||||
|
|
||||||
const params = new URLSearchParams();
|
|
||||||
params.append('url', args.link.href);
|
|
||||||
fetch(`${Hapi}/download`, {method: 'POST', body: params})
|
|
||||||
.then(async res => {
|
|
||||||
Embed.setFooter(`Using Hapi | ${Embed.footer.text}`);
|
|
||||||
compressEmbed.setFooter(`Using Hapi | ${compressEmbed.footer.text}`);
|
|
||||||
|
|
||||||
if (res.headers.get('content-type') == 'application/json; charset=utf-8') {
|
|
||||||
let json = await res.json();
|
|
||||||
let compressmsg = await message.channel.send(compressEmbed);
|
|
||||||
|
|
||||||
console.log(json);
|
|
||||||
|
|
||||||
let editmsg = setInterval(() => {
|
|
||||||
console.log('a');
|
|
||||||
fetch(json.status)
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(json => {
|
|
||||||
console.log(json);
|
|
||||||
compressEmbed.setDescription(`Ready in ${json.eta === '' ? 'soon enough' : json.eta}. ${json.percent}% complete.`);
|
|
||||||
compressmsg.edit(compressEmbed);
|
|
||||||
})
|
|
||||||
.catch (e=> {
|
|
||||||
console.error(e);
|
|
||||||
clearInterval(editmsg);
|
|
||||||
return message.channel.send('Hapi server returned an error.');
|
|
||||||
});
|
|
||||||
}, 5000);
|
|
||||||
|
|
||||||
|
|
||||||
let retry = 0;
|
|
||||||
let interval = setInterval(() => {
|
|
||||||
fetch(`${json.final}`)
|
|
||||||
.then(res => {
|
|
||||||
if (res.status == 200) {
|
|
||||||
clearInterval(editmsg);
|
|
||||||
clearInterval(interval);
|
|
||||||
const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}compressed.mp4`);
|
|
||||||
res.body.pipe(dest);
|
|
||||||
dest.on('finish', () => {
|
|
||||||
compressmsg.delete();
|
|
||||||
message.channel.send({
|
|
||||||
embed: Embed,
|
|
||||||
files: [`${os.tmpdir()}/${message.id}compressed.mp4`]
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err);
|
|
||||||
return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
retry++;
|
|
||||||
if (retry >= 5)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.log(`try #${retry} status ${res.status}`);
|
|
||||||
})
|
|
||||||
.catch (e=> {
|
|
||||||
console.error(e);
|
|
||||||
clearInterval(interval);
|
|
||||||
return message.channel.send('Hapi server returned an error.');
|
|
||||||
});
|
|
||||||
}, 5000);
|
|
||||||
} else {
|
|
||||||
const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}.mp4`);
|
|
||||||
res.body.pipe(dest);
|
|
||||||
dest.on('finish', () => {
|
|
||||||
message.channel.send({embed: Embed, files: [`${os.tmpdir()}/${message.id}.mp4`]});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
message.delete();
|
|
||||||
loadingmsg.delete();
|
|
||||||
return;
|
|
||||||
})
|
|
||||||
.catch(e => {
|
|
||||||
console.error(e);
|
|
||||||
error = true;
|
|
||||||
message.channel.send('Hapi server returned an error or is unreachable. Trying standalone download.');
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!error)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.listproxy) {
|
if (args.listproxy) {
|
||||||
let proxys = [];
|
let proxys = [];
|
||||||
|
@ -173,19 +73,126 @@ class DownloadCommand extends Command {
|
||||||
return message.channel.send(Embed);
|
return message.channel.send(Embed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!args.link) return message.channel.send('Please try again with a valid URL.');
|
|
||||||
let filename = `${message.id}_video`;
|
|
||||||
|
|
||||||
|
let Embed = this.client.util.embed()
|
||||||
|
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
||||||
|
.setAuthor(`Downloaded by ${message.author.username}`, message.author.displayAvatarURL(), args.link)
|
||||||
|
.setDescription(args.caption ? args.caption : '')
|
||||||
|
.setFooter(`You can get the original video by clicking on the "downloaded by ${message.author.username}" message!`);
|
||||||
|
|
||||||
|
let compressEmbed = this.client.util.embed()
|
||||||
|
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
||||||
|
.setTitle('This one will need compression!')
|
||||||
|
.setDescription('Starting compression now!')
|
||||||
|
.setFooter('Want it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!');
|
||||||
|
|
||||||
|
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
|
||||||
|
|
||||||
|
|
||||||
|
const spoiler = args.spoiler;
|
||||||
|
let filePath = `${os.tmpdir()}/${message.id}.mp4`;
|
||||||
|
|
||||||
|
if (spoiler)
|
||||||
|
filePath = `SPOILER_${filePath}`;
|
||||||
|
|
||||||
|
const isHapiOnline = await checkHapi();
|
||||||
|
|
||||||
|
if (isHapiOnline) { // I should move that into the download utility file
|
||||||
|
console.log(`[Download] Hapi server is online at: ${Hapi}`);
|
||||||
|
Embed.setFooter(`Using Hapi | ${Embed.footer.text}`);
|
||||||
|
compressEmbed.setFooter(`Using Hapi | ${compressEmbed.footer.text}`);
|
||||||
|
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('url', args.link.href);
|
||||||
|
|
||||||
|
fetch(`${Hapi}/download`, {method: 'POST', body: params})
|
||||||
|
.then(async res => {
|
||||||
|
console.log(`status is ${res.status}`);
|
||||||
|
if (res.status !== 200) {
|
||||||
|
console.log('Status is not 200. Abort');
|
||||||
|
return message.channel.send('An error has occurred.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.headers.get('content-type') === 'application/json; charset=utf-8') { // If we receive JSON it mean it started compressing the video
|
||||||
|
console.log('Compressing');
|
||||||
|
let json = await res.json();
|
||||||
|
let compressmsg = await message.channel.send(compressEmbed);
|
||||||
|
|
||||||
|
console.log(`Got json!\n${json}`);
|
||||||
|
|
||||||
|
// Edit compression embed progress
|
||||||
|
let editmsg = setInterval(() => {
|
||||||
|
fetch(json.status)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => {
|
||||||
|
compressEmbed.setDescription(`Ready in ${json.eta === '' ? 'soon enough' : json.eta}. ${json.percent}% complete.`);
|
||||||
|
compressmsg.edit(compressEmbed);
|
||||||
|
})
|
||||||
|
.catch (e=> {
|
||||||
|
console.error(e);
|
||||||
|
clearInterval(editmsg);
|
||||||
|
return message.channel.send('Hapi server returned an error.');
|
||||||
|
});
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
|
// Check if the file is at the final location ( end result once it get compressed ) give up after 100 try
|
||||||
|
let retry = 0;
|
||||||
|
let interval = setInterval(() => {
|
||||||
|
fetch(`${json.final}`)
|
||||||
|
.then(res => {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
console.log(`try #${retry} status ${res.status}`);
|
||||||
|
retry++;
|
||||||
|
if (retry >= 100) {
|
||||||
|
clearInterval(editmsg);
|
||||||
|
clearInterval(interval);
|
||||||
|
return message.channel.send('Timed out, your video was probably too big to get compressed.');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
clearInterval(editmsg);
|
||||||
|
clearInterval(interval);
|
||||||
|
|
||||||
|
const dest = fs.createWriteStream(filePath);
|
||||||
|
res.body.pipe(dest);
|
||||||
|
|
||||||
|
dest.on('finish', () => {
|
||||||
|
compressmsg.delete();
|
||||||
|
loadingmsg.delete();
|
||||||
|
message.delete();
|
||||||
|
message.channel.send({
|
||||||
|
embed: Embed,
|
||||||
|
files: [filePath]
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err);
|
||||||
|
return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch (e=> {
|
||||||
|
console.error(e);
|
||||||
|
clearInterval(interval);
|
||||||
|
return message.channel.send('Hapi server returned an error.');
|
||||||
|
});
|
||||||
|
}, 5000);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
console.log('finished download');
|
||||||
|
const dest = fs.createWriteStream(filePath);
|
||||||
|
res.body.pipe(dest);
|
||||||
|
dest.on('finish', () => {
|
||||||
|
message.channel.send({embed: Embed, files: [filePath]});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else { // No Hapi server, let the bot download it
|
||||||
if (args.proxy && !args.proxyAuto) { // args.proxyAuto is only provided when the command is run after a error 429
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.spoiler) {
|
downloader(args.link.href, args.proxy != null ? ['--proxy', proxy[args.proxy].ip] : null, filePath)
|
||||||
filename = `SPOILER_${message.id}_video`;
|
|
||||||
}
|
|
||||||
|
|
||||||
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')) {
|
if (err.includes('HTTP Error 429: Too Many Requests')) {
|
||||||
if (args.proxy != null) {
|
if (args.proxy != null) {
|
||||||
|
@ -209,12 +216,13 @@ class DownloadCommand extends Command {
|
||||||
let file = fs.statSync(output);
|
let file = fs.statSync(output);
|
||||||
let fileSize = file.size / 1000000.0;
|
let fileSize = file.size / 1000000.0;
|
||||||
|
|
||||||
if (fileSize > 8) {
|
if (fileSize > 8) { // File bigger than 8 mb, it need compressing
|
||||||
|
filePath = `${filePath.substring(0, filePath.length - 4)}compressed.mp4`;
|
||||||
loadingmsg.delete();
|
loadingmsg.delete();
|
||||||
|
|
||||||
let compressmsg = await message.channel.send(compressEmbed);
|
let compressmsg = await message.channel.send(compressEmbed);
|
||||||
|
|
||||||
let handbrake = compress(output, `${os.tmpdir()}/${filename}compressed.mp4`);
|
let handbrake = compress(output, filePath);
|
||||||
|
|
||||||
let percentComplete;
|
let percentComplete;
|
||||||
let eta;
|
let eta;
|
||||||
|
@ -272,6 +280,8 @@ class DownloadCommand extends Command {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = DownloadCommand;
|
module.exports = DownloadCommand;
|
10
utils/checkHapi.js
Normal file
10
utils/checkHapi.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const { Hapi } = require('../config.json');
|
||||||
|
|
||||||
|
module.exports = async function () {
|
||||||
|
if (Hapi) {
|
||||||
|
var res = await fetch(Hapi);
|
||||||
|
return res.status === 200;
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
};
|
Loading…
Reference in a new issue