ci: testing pipeline

This commit is contained in:
Hydra 2024-04-29 11:01:34 +01:00
parent 05177d5e9c
commit 25fb4342d6
27 changed files with 1349 additions and 1399 deletions

View file

@ -1,9 +1,9 @@
import path from "node:path";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { registerEvent } from "../register-event";
import { getProcesses } from "@main/helpers";
const closeGame = async (
_event: Electron.IpcMainInvokeEvent,
@ -12,13 +12,17 @@ const closeGame = async (
const processes = await getProcesses();
const game = await gameRepository.findOne({ where: { id: gameId } });
const gameProcess = processes.find((runningProcess) => {
const basename = path.win32.basename(game.executablePath);
const basenameWithoutExtension = path.win32.basename(
game.executablePath,
path.extname(game.executablePath)
);
if (!game) return false;
const executablePath = game.executablePath!;
const basename = path.win32.basename(executablePath);
const basenameWithoutExtension = path.win32.basename(
executablePath,
path.extname(executablePath)
);
const gameProcess = processes.find((runningProcess) => {
if (process.platform === "win32") {
return runningProcess.name === basename;
}

View file

@ -4,7 +4,7 @@ import {
getNewGOGGames,
getNewRepacksFromCPG,
getNewRepacksFromUser,
// getNewRepacksFromXatab,
getNewRepacksFromXatab,
getNewRepacksFromOnlineFix,
readPipe,
startProcessWatcher,
@ -22,13 +22,6 @@ import { Repack } from "./entity";
import { Notification } from "electron";
import { t } from "i18next";
import { In } from "typeorm";
import creatWorker from "./workers/test?nodeWorker";
creatWorker({ workerData: "worker" })
.on("message", (message) => {
console.log(`\nMessage from worker: ${message}`);
})
.postMessage("");
startProcessWatcher();
@ -80,9 +73,9 @@ const checkForNewRepacks = async () => {
getNewGOGGames(
existingRepacks.filter((repack) => repack.repacker === "GOG")
),
// getNewRepacksFromXatab(
// existingRepacks.filter((repack) => repack.repacker === "Xatab")
// ),
getNewRepacksFromXatab(
existingRepacks.filter((repack) => repack.repacker === "Xatab")
),
getNewRepacksFromCPG(
existingRepacks.filter((repack) => repack.repacker === "CPG")
),

View file

@ -4,7 +4,6 @@ import { formatUploadDate } from "@main/helpers";
import { Repack } from "@main/entity";
import { requestWebPage, savePage } from "./helpers";
import type { GameRepackInput } from "./helpers";
export const request1337x = async (path: string) =>
requestWebPage(`https://1337xx.to${path}`);
@ -68,7 +67,7 @@ export const extractTorrentsFromDocument = async (
user: string,
document: Document,
existingRepacks: Repack[] = []
): Promise<GameRepackInput[]> => {
) => {
const $trs = Array.from(document.querySelectorAll("tbody tr"));
return Promise.all(
@ -108,7 +107,7 @@ export const getNewRepacksFromUser = async (
user: string,
existingRepacks: Repack[],
page = 1
): Promise<Repack[]> => {
) => {
const response = await request1337x(`/user/${user}/${page}`);
const { window } = new JSDOM(response);

View file

@ -3,7 +3,6 @@ import { JSDOM } from "jsdom";
import { Repack } from "@main/entity";
import { requestWebPage, savePage } from "./helpers";
import type { GameRepackInput } from "./helpers";
import { logger } from "../logger";
export const getNewRepacksFromCPG = async (
@ -14,7 +13,7 @@ export const getNewRepacksFromCPG = async (
const { window } = new JSDOM(data);
const repacks: GameRepackInput[] = [];
const repacks = [];
try {
Array.from(window.document.querySelectorAll(".post")).forEach(($post) => {

View file

@ -1,7 +1,8 @@
import { JSDOM, VirtualConsole } from "jsdom";
import { GameRepackInput, requestWebPage, savePage } from "./helpers";
import { requestWebPage, savePage } from "./helpers";
import { Repack } from "@main/entity";
import { logger } from "../logger";
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
const virtualConsole = new VirtualConsole();
@ -36,43 +37,35 @@ const getGOGGame = async (url: string) => {
};
export const getNewGOGGames = async (existingRepacks: Repack[] = []) => {
try {
const data = await requestWebPage(
"https://freegogpcgames.com/a-z-games-list/"
);
const data = await requestWebPage(
"https://freegogpcgames.com/a-z-games-list/"
);
const { window } = new JSDOM(data, { virtualConsole });
const { window } = new JSDOM(data, { virtualConsole });
const $uls = Array.from(window.document.querySelectorAll(".az-columns"));
const $uls = Array.from(window.document.querySelectorAll(".az-columns"));
for (const $ul of $uls) {
const repacks: GameRepackInput[] = [];
const $lis = Array.from($ul.querySelectorAll("li"));
for (const $ul of $uls) {
const repacks: QueryDeepPartialEntity<Repack>[] = [];
const $lis = Array.from($ul.querySelectorAll("li"));
for (const $li of $lis) {
const $a = $li.querySelector("a");
const href = $a.href;
for (const $li of $lis) {
const $a = $li.querySelector("a");
const href = $a.href;
const title = $a.textContent.trim();
const title = $a.textContent.trim();
const gameExists = existingRepacks.some(
(existingRepack) => existingRepack.title === title
);
const gameExists = existingRepacks.some(
(existingRepack) => existingRepack.title === title
);
if (!gameExists) {
try {
const game = await getGOGGame(href);
if (!gameExists) {
const game = await getGOGGame(href);
repacks.push({ ...game, title });
} catch (err) {
logger.error(err.message, { method: "getGOGGame", url: href });
}
}
repacks.push({ ...game, title });
}
if (repacks.length) await savePage(repacks);
}
} catch (err) {
logger.error(err.message, { method: "getNewGOGGames" });
if (repacks.length) await savePage(repacks);
}
};

View file

@ -1,6 +1,5 @@
import { Repack } from "@main/entity";
import { savePage } from "./helpers";
import type { GameRepackInput } from "./helpers";
import { logger } from "../logger";
import parseTorrent, {
toMagnetURI,
@ -85,7 +84,7 @@ export const getNewRepacksFromOnlineFix = async (
});
const document = new JSDOM(home.body).window.document;
const repacks: GameRepackInput[] = [];
const repacks = [];
const articles = Array.from(document.querySelectorAll(".news"));
const totalPages = Number(
document.querySelector("nav > a:nth-child(13)")?.textContent

View file

@ -1,16 +1,14 @@
import { JSDOM } from "jsdom";
import parseTorrent, { toMagnetURI } from "parse-torrent";
import { Repack } from "@main/entity";
import { logger } from "../logger";
import { requestWebPage, savePage } from "./helpers";
import type { GameRepackInput } from "./helpers";
const getTorrentBuffer = (url: string) =>
fetch(url, { method: "GET" }).then((response) =>
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
);
import createWorker from "@main/workers/torrent-parser.worker?nodeWorker";
import { toMagnetURI } from "parse-torrent";
import type { Instance } from "parse-torrent";
const worker = createWorker({});
const formatXatabDate = (str: string) => {
const date = new Date();
@ -28,29 +26,36 @@ const formatXatabDate = (str: string) => {
const formatXatabDownloadSize = (str: string) =>
str.replace(",", ".").replace(/Гб/g, "GB").replace(/Мб/g, "MB");
const getXatabRepack = async (url: string) => {
const data = await requestWebPage(url);
const { window } = new JSDOM(data);
const { document } = window;
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");
const $uploadDate = document.querySelector(".entry__date");
const $size = document.querySelector(".entry__info-size");
const $downloadButton = document.querySelector(
".download-torrent"
) as HTMLAnchorElement;
const $downloadButton = document.querySelector(
".download-torrent"
) as HTMLAnchorElement;
if (!$downloadButton) throw new Error("Download button not found");
if (!$downloadButton) throw new Error("Download button not found");
const torrentBuffer = await getTorrentBuffer($downloadButton.href);
const onMessage = (torrent: Instance) => {
resolve({
fileSize: formatXatabDownloadSize($size.textContent).toUpperCase(),
magnet: toMagnetURI(torrent),
uploadDate: formatXatabDate($uploadDate.textContent),
});
return {
fileSize: formatXatabDownloadSize($size.textContent).toUpperCase(),
magnet: toMagnetURI({
infoHash: parseTorrent(torrentBuffer).infoHash,
}),
uploadDate: formatXatabDate($uploadDate.textContent),
};
worker.removeListener("message", onMessage);
};
worker.on("message", onMessage);
worker.postMessage($downloadButton.href);
})();
});
};
export const getNewRepacksFromXatab = async (
@ -61,7 +66,7 @@ export const getNewRepacksFromXatab = async (
const { window } = new JSDOM(data);
const repacks: GameRepackInput[] = [];
const repacks = [];
for (const $a of Array.from(
window.document.querySelectorAll(".entry__title a")
@ -84,7 +89,6 @@ export const getNewRepacksFromXatab = async (
const newRepacks = repacks.filter(
(repack) =>
repack.uploadDate &&
!existingRepacks.some(
(existingRepack) => existingRepack.title === repack.title
)

View file

@ -2,8 +2,8 @@ import { BrowserWindow, Menu, Tray, app } from "electron";
import { is } from "@electron-toolkit/utils";
import { t } from "i18next";
import path from "node:path";
import icon from "../../../resources/icon.png?asset";
import trayIcon from "../../../resources/tray-icon.png?asset";
import icon from "@resources/icon.png?asset";
import trayIcon from "@resources/tray-icon.png?asset";
export class WindowManager {
public static mainWindow: Electron.BrowserWindow | null = null;

View file

@ -0,0 +1,17 @@
import { parentPort } from "worker_threads";
import parseTorrent from "parse-torrent";
const port = parentPort;
if (!port) throw new Error("IllegalState");
const getTorrentBuffer = (url: string) =>
fetch(url, { method: "GET" }).then((response) =>
response.arrayBuffer().then((buffer) => Buffer.from(buffer))
);
port.on("message", async (url: string) => {
const buffer = await getTorrentBuffer(url);
const torrent = await parseTorrent(buffer);
port.postMessage(torrent);
});