selfdebrid/extractors/uptobox.js

114 lines
4.1 KiB
JavaScript

const axios = require("axios");
const cheerio = require("cheerio");
const lib = require("../lib");
module.exports = {
get: get,
hostnames: ["uptobox.com"],
tests: []
}
async function get(url, options) {
if (!options) options = lib.getOptions();
let cookies;
if (options.cookieFile) {
lib.log(`uptobox`, `Parsing cookies...`);
cookies = await lib.getCookies(options.cookieFile, "uptobox.com");
lib.log(`uptobox`, `Got cookies:`, [cookies]);
}
lib.log(`uptobox`, `Fetching page...`, [], true, options);
let stringCookie = lib.cookieToString(cookies);
let headers = lib.defaultHeaders;
if (options.cookieFile) headers["Cookie"] = stringCookie;
let resp = await axios({
url: url,
headers: headers
});
lib.log(`uptobox`, `Parsing response...`);
let $ = cheerio.load(resp.data);
let dlElement = $(".comparison-table > thead > tr > td > a:not([href='/payments'])")?.[0];
if (dlElement !== null) {
if (dlElement?.attribs?.href) {
lib.log(`uptbox`, `Found direct link, parsing...`);
if (options.cookieFile) {
let hostname = new URL(dlElement?.attribs?.href).hostname;
cookies = await lib.getCookies(options.cookieFile, hostname);
headers["Cookie"] = lib.cookieToString(cookies);
}
headers["Referer"] = url;
let dlLink = dlElement?.attribs?.href;
let dlName = dlLink.split(`/`)[dlLink.split(`/`).length - 1];
return {
axiosData: {
headers: headers,
url: dlLink
},
metadata: {
fileName: decodeURIComponent(dlName)
}
}
} else {
if ($("form > span[class='red']")?.[0]) {
lib.log(`uptbox`, `There is an error, sending...`);
throw new Error($("form > span[class='red']").text().split(`\n`).join(``).split(`\t`).join(``).split(`.`).join(`, `));
} else if ($(".comparison-table > thead > tr > td > div > .time-remaining")[0]) {
lib.log(`uptbox`, `Found timer, parsing...`);
let seconds = $(".comparison-table > thead > tr > td > div > .time-remaining")[0].attribs["data-remaining-time"];
seconds = ((parseInt(seconds) + 5) * 1000);
let wtk = $("input[name='waitingToken']").val();
lib.log(`uptobox`, `Got waiting token`, [wtk])
lib.log(`uptobox`, `Waiting the designated ${(seconds / 1000)} seconds to download (yes, this is required).`, [], true, options);
await lib.countdown(seconds, options);
lib.log(`uptobox`, `Sending second download request...`, [], true, options);
headers["Content-Type"] = "application/x-www-form-urlencoded";
try {
let resp = await axios({
method: `POST`,
data: `waitingToken=${encodeURIComponent(wtk)}`,
url: url,
headers: headers
});
lib.log(`uptobox`, `Parsing second download request...`);
let $ = cheerio.load(resp.data);
let dlElement = $(".comparison-table > thead > tr > td > a:not([href='/payments'])")?.[0];
if (dlElement?.attribs?.href) {
let hostname = new URL(dlElement?.attribs?.href).hostname;
cookies = await lib.getCookies((options.cookieFile || `${__dirname}/sample-cookies/uptobox.txt`), hostname);
headers["Cookie"] = lib.cookieToString(cookies);
headers["Referer"] = url;
let dlLink = dlElement?.attribs?.href;
let dlName = dlLink.split(`/`)[dlLink.split(`/`).length - 1];
headers["Referer"] = url;
return {
axiosData: {
headers: headers,
url: dlLink
},
metadata: {
fileName: decodeURIComponent(dlName)
}
};
} else throw new Error("Second download request failed.");
} catch(err) {
throw err;
}
} else if ($("title").text().includes("not allowed")) throw new Error("Uptobox is region locked, please set a proxy or turn your VPN to a non-US country.");
else throw new Error("Could not parse page.");
}
} else throw new Error("Could not find download element.");
}