From 2487e6efcd9aa89628cfdaacb600ec05f5d122c6 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Thu, 4 Feb 2021 17:17:38 +0100 Subject: [PATCH] Use Hapi when possible Signed-off-by: loicbersier --- commands/utility/download.js | 94 +++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/commands/utility/download.js b/commands/utility/download.js index c8da57e3..02ea193c 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -52,7 +52,99 @@ class DownloadCommand extends Command { } async exec(message, args) { - console.log(args.proxy); + 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 '); + + + if (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 => { + 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`] + }); + }); + } 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); + return message.channel.send('Hapi server returned an error.'); + }); + return; + } + if (args.listproxy) { let proxys = [];