-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding how long to beat integration
- Loading branch information
1 parent
034ea6d
commit 5580a9d
Showing
14 changed files
with
282 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { GameShop } from "@types"; | ||
import { getHowLongToBeatGame, searchHowLongToBeat } from "@main/services"; | ||
|
||
import { registerEvent } from "../register-event"; | ||
|
||
const getHowLongToBeat = async ( | ||
_event: Electron.IpcMainInvokeEvent, | ||
objectID: string, | ||
_shop: GameShop, | ||
title: string | ||
): Promise<Record<string, { time: string; color: string }> | null> => { | ||
const response = await searchHowLongToBeat(title); | ||
const game = response.data.find( | ||
(game) => game.profile_steam === Number(objectID) | ||
); | ||
|
||
if (!game) return null; | ||
const howLongToBeat = await getHowLongToBeatGame(String(game.game_id)); | ||
return howLongToBeat; | ||
}; | ||
|
||
registerEvent(getHowLongToBeat, { | ||
name: "getHowLongToBeat", | ||
memoize: true, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { formatName } from "@main/helpers"; | ||
import axios from "axios"; | ||
import { JSDOM } from "jsdom"; | ||
import { requestWebPage } from "./repack-tracker/helpers"; | ||
|
||
export interface HowLongToBeatResult { | ||
game_id: number; | ||
profile_steam: number; | ||
} | ||
|
||
export interface HowLongToBeatSearchResponse { | ||
data: HowLongToBeatResult[]; | ||
} | ||
|
||
export const searchHowLongToBeat = async (gameName: string) => { | ||
const response = await axios.post( | ||
"https://howlongtobeat.com/api/search", | ||
{ | ||
searchType: "games", | ||
searchTerms: formatName(gameName).split(" "), | ||
searchPage: 1, | ||
size: 100, | ||
}, | ||
{ | ||
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", | ||
Referer: "https://howlongtobeat.com/", | ||
}, | ||
} | ||
); | ||
|
||
return response.data as HowLongToBeatSearchResponse; | ||
}; | ||
|
||
export const classNameColor = { | ||
time_40: "#ff3a3a", | ||
time_50: "#cc3b51", | ||
time_60: "#824985", | ||
time_70: "#5650a1", | ||
time_80: "#485cab", | ||
time_90: "#3a6db5", | ||
time_100: "#287fc2", | ||
}; | ||
|
||
export const getHowLongToBeatGame = async (id: string) => { | ||
const response = await requestWebPage(`https://howlongtobeat.com/game/${id}`); | ||
|
||
const { window } = new JSDOM(response); | ||
const { document } = window; | ||
|
||
const $ul = document.querySelector(".shadow_shadow ul"); | ||
const $lis = Array.from($ul.children); | ||
|
||
return $lis.reduce((prev, next) => { | ||
const name = next.querySelector("h4").textContent; | ||
const [, time] = Array.from((next as HTMLElement).classList); | ||
|
||
return { | ||
...prev, | ||
[name]: { | ||
time: next.querySelector("h5").textContent, | ||
color: classNameColor[time as keyof typeof classNameColor], | ||
}, | ||
}; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.