@xct007/frieren-scraper
Version:
56 lines (55 loc) • 1.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.download = void 0;
const https_1 = __importDefault(require("https"));
const Utils_1 = require("../Utils");
function isValidUrl(url) {
return /(https?:\/\/(.+?\.)?zippyshare\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)/gm.test(url);
}
async function download(url) {
if (!isValidUrl(url)) {
return {
error: true,
message: "Invalid URL!",
};
}
try {
const { data } = await Utils_1.Axios.get(url, {
httpsAgent: new https_1.default.Agent({ keepAlive: true }),
}).catch((e) => e?.response);
const $ = (0, Utils_1.Cheerio)(data);
const splitUrl = url.split("/");
const Regex = new RegExp(`(?<=dlbutton)(.*)(?=;)`, "gm");
const _match = data.match(Regex)[0].replace("').href = ", "");
const finalUrl = `${splitUrl[0]}//${splitUrl[2]}${eval(_match)}`;
const _temp = [];
$("div#lrbox > div.center > div > font").each((i, e) => {
const _rawText = $(e).text();
if (_rawText !== "You have requested the file:") {
_temp.push(_rawText.replace(/:/, ""));
}
});
let metadata = {};
_temp.forEach((e, i) => {
if (i % 2 === 0) {
Object.assign(metadata, {
[e.toLowerCase().trim()]: _temp[i + 1],
});
}
});
return {
...metadata,
["url"]: finalUrl,
};
}
catch (e) {
return {
error: true,
message: String(e),
};
}
}
exports.download = download;