Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added dynamic retrieval of HLTB search API #9

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/hooks/HltbApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

// NOTE: Close reproduction of https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/pull/26
const SearchKey = Symbol('Search Key');
let searchApiRoot: string = '';
async function fetchSearchKey() {
try {
const url = 'https://howlongtobeat.com';
Expand All @@ -29,10 +30,15 @@ async function fetchSearchKey() {
if (scriptResponse.status === 200) {
const scriptText = await scriptResponse.text();
const pattern =
/\/api\/lookup\/"\.concat\("([a-zA-Z0-9]+)"\)\.concat\("([a-zA-Z0-9]+)"\)/;
/\/api\/[a-zA-Z]*\/"\.concat\("([a-zA-Z0-9]+)"\)\.concat\("([a-zA-Z0-9]+)"\)/;
const matches = scriptText.match(pattern);

if (matches && matches[1]) {
// dynamically retrieving the root of the HLBT search URL from the script
// Should work if {search-api} changes, assuming the overall pattern stays the same (i.e: "api/{search-api}/".concat(...).concat(...)")
searchApiRoot = matches[0]
.split('.')[0]
.slice(0, -1);
const apiKey = `${matches[1]}${matches[2]}`;
console.log('HLTB API Key:', apiKey);
return apiKey;
Expand Down Expand Up @@ -157,7 +163,7 @@ async function fetchSearchResultsWithKey(gameName: string, apiKey: string) {
},
};

return fetchNoCors(`https://howlongtobeat.com/api/lookup/${apiKey}`, {
return fetchNoCors(`https://howlongtobeat.com/${searchApiRoot}${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down