selfdebrid/cli.js
2023-03-13 01:30:52 -04:00

72 lines
2.4 KiB
JavaScript
Executable file

#!/bin/node
const lib = require("./lib");
const filecrypt = require("./filecrypt");
start();
async function start() {
let options = lib.getOptions();
lib.log(`main`, `Got options`, [options]);
for (let i in options.urls) {
let url = options.urls[i];
let hostname = new URL(url).hostname;
if (hostname == "filecrypt.co" || hostname == "filecrypt.cc") {
lib.log(`main`, `Decrypting link "${url}"...`, [], true, options);
let newLinks = await filecrypt.get(url, options);
for (let x in newLinks) {
options.urls.push(newLinks[x]);
}
delete options.urls[i];
}
}
for (let i in options.urls) {
let url = options.urls[i];
lib.log(`main`, `Fetching extractor for "${url}"...`, [], true);
try {
let extractor = await lib.getExtractor(url);
lib.log(`main`, `Got extractor. Getting metadata for "${url}"...`, [], true)
let hostname = new URL(url).hostname;
if (hostname == "multiup.org") {
lib.log(`main`, `Parsing "${url}"...`, [], true, options);
let links = await extractor.get(url, options);
if (options.preferredHost) {
url = links.mirror.find(x => x.host == options.preferredHost).link;
extractor = await lib.getExtractor(url);
} else {
url = links.mirror[0];
extractor = await lib.getExtractor(url);
}
lib.log(`main`, `Got "${url}"`, [], true, options);
}
let direct = await extractor.get(url, options);
if (options.dry == true) {
if (direct?.axiosData) lib.log(`main`, `Got data:`, [direct], true);
else if (direct?.stream) {
lib.log(`main`, `Recieved stream, cannot give stream via dry run`, [], true);
process.exit(1);
} else lib.log(`main`, `Invalid response from backend :(`, [], true);
} else {
lib.log(`main`, `Got data:`, [direct], false);
let dl = await lib.download(direct);
if (dl?.success == true) continue;
else if (dl?.success == false && dl?.error) {
let err = (dl.error.stack || dl.error.message || dl.error.code || dl.error);
lib.log(`main`, `Got error downloading "${url}": ${err}`, [], true);
lib.log(`main`, `Full error:`, [dl.error])
continue;
}
}
} catch(err) {
console.log(`[main] Got error for "${url}":`, (err.stack || err.message || err.code));
continue;
}
}
process.exit();
}