From ab9392e5304fdac759bdf1f18cac2c256393c80e Mon Sep 17 00:00:00 2001 From: masterhc Date: Mon, 29 Jul 2024 10:53:51 +0100 Subject: [PATCH] Feature: Start the download if there is no response after 5 min. --- controller/api.js | 63 +++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/controller/api.js b/controller/api.js index f11eed1..45d153a 100644 --- a/controller/api.js +++ b/controller/api.js @@ -479,35 +479,14 @@ exports.torrent = (bot)=> const filter = i => i.customId === 'Redirect'+random || i.customId === 'Remove'+random; let message = await user.send({embeds: [embed], components: [row] }); - const collector = message.createMessageComponentCollector({ filter, time: 60000 }); + const collector = message.createMessageComponentCollector({ filter, time: 5*60000 }); collector.on('collect', async m => { collector.stop(); m.deferUpdate(); - let cookie = await fetch('https://utorrent.wordfights.com/api/v2/auth/login', - { - "headers": - { - "content-type": "application/x-www-form-urlencoded; charset=UTF-8", - }, - "body":`username=${process.env.qbittorrentusername}&password=${process.env.qbittorrentpassword}`, - "method": "POST" - } - ) - .then(handleResponse) - - async function handleResponse(response) - { - try { - let headers = [...response.headers.entries()]; - headers = headers.filter(i=>i[0]=='set-cookie')[0][1] - return headers - } catch (error) { - console.log(error) - return - } - } + let cookie = await getCookie(); + if(!cookie) return if(m.customId === 'Remove'+random) @@ -538,7 +517,43 @@ exports.torrent = (bot)=> collector.on('end', async ()=> { message.edit({ components: [] }); + await fetch("https://utorrent.wordfights.com/api/v2/torrents/setForceStart", { + "headers": { + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", + "cookie": cookie, + }, + "body": `value=true&hashes=${req.params.id}`, + "method": "POST" + }); }) res.json({ok:'OK'}) } +} + +async function getCookie() +{ + return await fetch('https://utorrent.wordfights.com/api/v2/auth/login', + { + "headers": + { + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", + }, + "body":`username=${process.env.qbittorrentusername}&password=${process.env.qbittorrentpassword}`, + "method": "POST" + } + ) + .then(handleResponse) + + async function handleResponse(response) + { + try { + let headers = [...response.headers.entries()]; + headers = headers.filter(i=>i[0]=='set-cookie')[0][1] + return headers + } catch (error) { + console.log(error) + return + } + + } } \ No newline at end of file