fix: using random user agent for how long to beat

This commit is contained in:
Hydra 2024-05-03 10:13:07 +01:00
parent 5ae7815e03
commit 590ead9643
No known key found for this signature in database
6 changed files with 509 additions and 359 deletions

View File

@ -58,6 +58,7 @@
"react-router-dom": "^6.22.3", "react-router-dom": "^6.22.3",
"tough-cookie": "^4.1.3", "tough-cookie": "^4.1.3",
"typeorm": "^0.3.20", "typeorm": "^0.3.20",
"user-agents": "^1.1.193",
"windows-1251": "^3.0.4", "windows-1251": "^3.0.4",
"winston": "^3.13.0", "winston": "^3.13.0",
"yaml": "^2.4.1" "yaml": "^2.4.1"

View File

@ -34,6 +34,21 @@ export const searchHowLongToBeat = async (gameName: string) => {
return response.data as HowLongToBeatSearchResponse; return response.data as HowLongToBeatSearchResponse;
}; };
const parseListItems = ($lis: Element[]) => {
return $lis.map(($li) => {
const title = $li.querySelector("h4")?.textContent;
const [, accuracyClassName] = Array.from(($li as HTMLElement).classList);
const accuracy = accuracyClassName.split("time_").at(1);
return {
title: title ?? "",
duration: $li.querySelector("h5")?.textContent ?? "",
accuracy: accuracy ?? "",
};
});
};
export const getHowLongToBeatGame = async ( export const getHowLongToBeatGame = async (
id: string id: string
): Promise<HowLongToBeatCategory[]> => { ): Promise<HowLongToBeatCategory[]> => {
@ -43,18 +58,16 @@ export const getHowLongToBeatGame = async (
const { document } = window; const { document } = window;
const $ul = document.querySelector(".shadow_shadow ul"); const $ul = document.querySelector(".shadow_shadow ul");
if (!$ul) return [];
const $lis = Array.from($ul.children); const $lis = Array.from($ul.children);
return $lis.map(($li) => { const [$firstLi] = $lis;
const title = $li.querySelector("h4").textContent;
const [, accuracyClassName] = Array.from(($li as HTMLElement).classList);
const accuracy = accuracyClassName.split("time_").at(1); if ($firstLi.tagName === "DIV") {
const $pcData = $lis.find(($li) => $li.textContent?.includes("PC"));
return parseListItems(Array.from($pcData?.querySelectorAll("li") ?? []));
}
return { return parseListItems($lis);
title,
duration: $li.querySelector("h5").textContent,
accuracy,
};
});
}; };

View File

@ -1,3 +1,5 @@
import UserAgent from "user-agents";
import type { Repack } from "@main/entity"; import type { Repack } from "@main/entity";
import { repackRepository } from "@main/repository"; import { repackRepository } from "@main/repository";
@ -8,7 +10,13 @@ export const savePage = async (repacks: QueryDeepPartialEntity<Repack>[]) =>
repacks.map((repack) => repackRepository.insert(repack).catch(() => {})) repacks.map((repack) => repackRepository.insert(repack).catch(() => {}))
); );
export const requestWebPage = async (url: string) => export const requestWebPage = async (url: string) => {
fetch(url, { const userAgent = new UserAgent();
return fetch(url, {
method: "GET", method: "GET",
headers: {
"User-Agent": userAgent.toString(),
},
}).then((response) => response.text()); }).then((response) => response.text());
};

View File

@ -26,8 +26,9 @@ export function DODIInstallationGuide({
const handleClose = () => { const handleClose = () => {
if (dontShowAgain) { if (dontShowAgain) {
window.localStorage.setItem(DONT_SHOW_DODI_INSTRUCTIONS_KEY, "1"); window.localStorage.setItem(DONT_SHOW_DODI_INSTRUCTIONS_KEY, "1");
onClose();
} }
onClose();
}; };
return ( return (

View File

@ -43,8 +43,9 @@ export function OnlineFixInstallationGuide({
const handleClose = () => { const handleClose = () => {
if (dontShowAgain) { if (dontShowAgain) {
window.localStorage.setItem(DONT_SHOW_ONLINE_FIX_INSTRUCTIONS_KEY, "1"); window.localStorage.setItem(DONT_SHOW_ONLINE_FIX_INSTRUCTIONS_KEY, "1");
onClose();
} }
onClose();
}; };
return ( return (

816
yarn.lock

File diff suppressed because it is too large Load Diff