feat: adding hltb key extraction

This commit is contained in:
Chubby Granny Chaser
2024-10-07 20:56:53 +01:00
parent 0222121288
commit baafc6c7d1
22 changed files with 791 additions and 205 deletions

View File

@@ -1,32 +1,65 @@
import axios from "axios";
import { requestWebPage } from "@main/helpers";
import { HowLongToBeatCategory } from "@types";
import type {
HowLongToBeatCategory,
HowLongToBeatSearchResponse,
} from "@types";
import { formatName } from "@shared";
import { logger } from "./logger";
import UserAgent from "user-agents";
export interface HowLongToBeatResult {
game_id: number;
profile_steam: number;
}
const state = {
apiKey: null as string | null,
};
export interface HowLongToBeatSearchResponse {
data: HowLongToBeatResult[];
}
const getHowLongToBeatSearchApiKey = async () => {
const userAgent = new UserAgent();
const document = await requestWebPage("https://howlongtobeat.com/");
const scripts = Array.from(document.querySelectorAll("script"));
const appScript = scripts.find((script) =>
script.src.startsWith("/_next/static/chunks/pages/_app")
);
if (!appScript) return null;
const response = await axios.get(
`https://howlongtobeat.com${appScript.src}`,
{
headers: {
"User-Agent": userAgent.toString(),
},
}
);
const results = /fetch\("\/api\/search\/"\.concat\("(.*?)"\)/gm.exec(
response.data
);
if (!results) return null;
return results[1];
};
export const searchHowLongToBeat = async (gameName: string) => {
state.apiKey = state.apiKey ?? (await getHowLongToBeatSearchApiKey());
if (!state.apiKey) return { data: [] };
const userAgent = new UserAgent();
const response = await axios
.post(
"https://howlongtobeat.com/api/search",
"https://howlongtobeat.com/api/search/8fbd64723a8204dd",
{
searchType: "games",
searchTerms: formatName(gameName).split(" "),
searchPage: 1,
size: 100,
size: 20,
},
{
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"User-Agent": userAgent.toString(),
Referer: "https://howlongtobeat.com/",
},
}

View File

@@ -16,6 +16,7 @@ import trayIcon from "@resources/tray-icon.png?asset";
import { gameRepository, userPreferencesRepository } from "@main/repository";
import { IsNull, Not } from "typeorm";
import { HydraApi } from "./hydra-api";
import UserAgent from "user-agents";
export class WindowManager {
public static mainWindow: Electron.BrowserWindow | null = null;
@@ -79,11 +80,12 @@ export class WindowManager {
this.mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
const userAgent = new UserAgent();
callback({
requestHeaders: {
...details.requestHeaders,
"user-agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"user-agent": userAgent.toString(),
},
});
}
@@ -146,30 +148,29 @@ export class WindowManager {
}
public static createNotificationWindow() {
this.notificationWindow = new BrowserWindow({
transparent: true,
maximizable: false,
autoHideMenuBar: true,
minimizable: false,
focusable: false,
skipTaskbar: true,
frame: false,
width: 350,
height: 104,
x: 0,
y: 0,
webPreferences: {
preload: path.join(__dirname, "../preload/index.mjs"),
sandbox: false,
},
});
this.notificationWindow.setIgnoreMouseEvents(true);
this.notificationWindow.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
});
this.notificationWindow.setAlwaysOnTop(true, "screen-saver", 1);
this.loadNotificationWindowURL();
// this.notificationWindow = new BrowserWindow({
// transparent: true,
// maximizable: false,
// autoHideMenuBar: true,
// minimizable: false,
// focusable: false,
// skipTaskbar: true,
// frame: false,
// width: 350,
// height: 104,
// x: 0,
// y: 0,
// webPreferences: {
// preload: path.join(__dirname, "../preload/index.mjs"),
// sandbox: false,
// },
// });
// this.notificationWindow.setIgnoreMouseEvents(true);
// this.notificationWindow.setVisibleOnAllWorkspaces(true, {
// visibleOnFullScreen: true,
// });
// this.notificationWindow.setAlwaysOnTop(true, "screen-saver", 1);
// this.loadNotificationWindowURL();
}
public static openAuthWindow() {