2024-04-21 08:26:29 +03:00
|
|
|
|
import { JSDOM } from "jsdom";
|
|
|
|
|
|
|
|
|
|
import { Repack } from "@main/entity";
|
|
|
|
|
import { logger } from "../logger";
|
|
|
|
|
import { requestWebPage, savePage } from "./helpers";
|
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
import createWorker from "@main/workers/torrent-parser.worker?nodeWorker";
|
|
|
|
|
import { toMagnetURI } from "parse-torrent";
|
|
|
|
|
import type { Instance } from "parse-torrent";
|
|
|
|
|
|
|
|
|
|
const worker = createWorker({});
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
|
|
|
|
const formatXatabDate = (str: string) => {
|
|
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
|
|
const [day, month, year] = str.split(".");
|
|
|
|
|
|
|
|
|
|
date.setDate(Number(day));
|
|
|
|
|
date.setMonth(Number(month) - 1);
|
|
|
|
|
date.setFullYear(Number(year));
|
|
|
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
return date;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formatXatabDownloadSize = (str: string) =>
|
|
|
|
|
str.replace(",", ".").replace(/Гб/g, "GB").replace(/Мб/g, "MB");
|
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
const getXatabRepack = (url: string) => {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
(async () => {
|
|
|
|
|
const data = await requestWebPage(url);
|
|
|
|
|
const { window } = new JSDOM(data);
|
|
|
|
|
const { document } = window;
|
|
|
|
|
|
|
|
|
|
const $uploadDate = document.querySelector(".entry__date");
|
|
|
|
|
const $size = document.querySelector(".entry__info-size");
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
const $downloadButton = document.querySelector(
|
|
|
|
|
".download-torrent"
|
|
|
|
|
) as HTMLAnchorElement;
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
if (!$downloadButton) throw new Error("Download button not found");
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
const onMessage = (torrent: Instance) => {
|
|
|
|
|
resolve({
|
|
|
|
|
fileSize: formatXatabDownloadSize($size.textContent).toUpperCase(),
|
|
|
|
|
magnet: toMagnetURI(torrent),
|
|
|
|
|
uploadDate: formatXatabDate($uploadDate.textContent),
|
|
|
|
|
});
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
worker.removeListener("message", onMessage);
|
|
|
|
|
};
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
2024-05-04 01:00:53 +03:00
|
|
|
|
worker.once("message", onMessage);
|
2024-04-29 13:01:34 +03:00
|
|
|
|
})();
|
|
|
|
|
});
|
2024-04-21 08:26:29 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getNewRepacksFromXatab = async (
|
|
|
|
|
existingRepacks: Repack[] = [],
|
|
|
|
|
page = 1
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const data = await requestWebPage(`https://byxatab.com/page/${page}`);
|
|
|
|
|
|
|
|
|
|
const { window } = new JSDOM(data);
|
|
|
|
|
|
2024-04-29 13:01:34 +03:00
|
|
|
|
const repacks = [];
|
2024-04-21 08:26:29 +03:00
|
|
|
|
|
|
|
|
|
for (const $a of Array.from(
|
|
|
|
|
window.document.querySelectorAll(".entry__title a")
|
|
|
|
|
)) {
|
|
|
|
|
try {
|
|
|
|
|
const repack = await getXatabRepack(($a as HTMLAnchorElement).href);
|
|
|
|
|
|
|
|
|
|
repacks.push({
|
|
|
|
|
title: $a.textContent,
|
|
|
|
|
repacker: "Xatab",
|
|
|
|
|
...repack,
|
|
|
|
|
page,
|
|
|
|
|
});
|
2024-04-28 21:21:14 +03:00
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
logger.error((err as Error).message, {
|
|
|
|
|
method: "getNewRepacksFromXatab",
|
|
|
|
|
});
|
2024-04-21 08:26:29 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newRepacks = repacks.filter(
|
|
|
|
|
(repack) =>
|
|
|
|
|
!existingRepacks.some(
|
|
|
|
|
(existingRepack) => existingRepack.title === repack.title
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!newRepacks.length) return;
|
|
|
|
|
|
|
|
|
|
await savePage(newRepacks);
|
|
|
|
|
|
|
|
|
|
return getNewRepacksFromXatab(existingRepacks, page + 1);
|
|
|
|
|
};
|