hydra/src/main/services/repack-tracker/xatab.ts

120 lines
3.1 KiB
TypeScript
Raw Normal View History

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";
2024-05-12 15:29:12 +03:00
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
import { formatBytes } from "@shared";
2024-05-14 18:54:14 +03:00
import { getFileBuffer } from "@main/helpers";
2024-04-29 13:01:34 +03:00
const worker = createWorker({});
worker.setMaxListeners(11);
let totalPages = 1;
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;
};
2024-05-12 15:29:12 +03:00
const getXatabRepack = (
url: string
2024-05-14 18:54:14 +03:00
): Promise<{ fileSize: string; magnet: string; uploadDate: Date } | null> => {
2024-04-29 13:01:34 +03:00
return new Promise((resolve) => {
(async () => {
const data = await requestWebPage(url);
const { window } = new JSDOM(data);
const { document } = window;
const $uploadDate = document.querySelector(".entry__date");
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-05-14 18:54:14 +03:00
if (!$downloadButton) return resolve(null);
worker.once("message", (torrent: Instance | null) => {
if (!torrent) return resolve(null);
2024-04-21 08:26:29 +03:00
2024-04-29 13:01:34 +03:00
resolve({
2024-05-12 15:29:12 +03:00
fileSize: formatBytes(torrent.length ?? 0),
2024-04-29 13:01:34 +03:00
magnet: toMagnetURI(torrent),
2024-05-12 15:29:12 +03:00
uploadDate: formatXatabDate($uploadDate!.textContent!),
2024-04-29 13:01:34 +03:00
});
2024-05-12 15:29:12 +03:00
});
2024-05-14 18:54:14 +03:00
const buffer = await getFileBuffer($downloadButton.href);
worker.postMessage(buffer);
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-05-12 15:29:12 +03:00
const repacks: QueryDeepPartialEntity<Repack>[] = [];
2024-04-21 08:26:29 +03:00
if (page === 1) {
totalPages = Number(
window.document.querySelector(
"#bottom-nav > div.pagination > a:nth-child(12)"
)?.textContent
);
}
const repacksFromPage = Array.from(
2024-04-21 08:26:29 +03:00
window.document.querySelectorAll(".entry__title a")
).map(($a) => {
return getXatabRepack(($a as HTMLAnchorElement).href)
.then((repack) => {
if (repack) {
repacks.push({
title: $a.textContent!,
repacker: "Xatab",
...repack,
page,
});
}
})
.catch((err: unknown) => {
logger.error((err as Error).message, {
method: "getNewRepacksFromXatab",
2024-05-14 18:54:14 +03:00
});
2024-04-28 21:21:14 +03:00
});
});
await Promise.all(repacksFromPage);
2024-04-21 08:26:29 +03:00
const newRepacks = repacks.filter(
(repack) =>
!existingRepacks.some(
(existingRepack) => existingRepack.title === repack.title
)
);
if (!newRepacks.length) return;
if (page === totalPages) return;
2024-04-21 08:26:29 +03:00
await savePage(newRepacks);
return getNewRepacksFromXatab(existingRepacks, page + 1);
};