Skip to content

Commit

Permalink
fix: dropping algolia usage
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrannychaseroperation committed Apr 15, 2024
1 parent c57d624 commit df11365
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 103 deletions.
15 changes: 1 addition & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { app, BrowserWindow } from "electron";
import i18n from "i18next";
import path from "node:path";
import {
getSteamDBAlgoliaCredentials,
logger,
resolveDatabaseUpdates,
WindowManager,
} from "@main/services";
import { resolveDatabaseUpdates, WindowManager } from "@main/services";
import { updateElectronApp } from "update-electron-app";
import { dataSource } from "@main/data-source";
import * as resources from "@locales";
import { userPreferencesRepository } from "@main/repository";
import { stateManager } from "@main/state-manager";

const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) app.quit();
Expand Down Expand Up @@ -49,13 +43,6 @@ if (process.defaultApp) {
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
dataSource.initialize().then(async () => {
try {
const algoliaCredentials = await getSteamDBAlgoliaCredentials();
stateManager.setValue("steamDBAlgoliaCredentials", algoliaCredentials);
} catch (err) {
logger.error(err, { method: "getSteamDBAlgoliaCredentials" });
}

await resolveDatabaseUpdates();

await import("./main");
Expand Down
24 changes: 6 additions & 18 deletions src/main/events/helpers/search-games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import orderBy from "lodash/orderBy";
import type { GameRepack, GameShop, CatalogueEntry } from "@types";

import { formatName, getSteamAppAsset, repackerFormatter } from "@main/helpers";
import { searchAlgolia } from "@main/services";
import { searchSteamGame } from "@main/services";
import { stateManager } from "@main/state-manager";

const { Index } = flexSearch;
Expand All @@ -20,8 +20,6 @@ for (let i = 0; i < repacks.length; i++) {
repacksIndex.add(i, formatName(formatter(repack.title)));
}

export const HITS_PER_PAGE = 12;

export const searchRepacks = (title: string): GameRepack[] => {
const repacks = stateManager.getValue("repacks");

Expand All @@ -37,23 +35,13 @@ export const searchRepacks = (title: string): GameRepack[] => {
export const searchGames = async (query: string): Promise<CatalogueEntry[]> => {
const formattedName = formatName(query);

const steamResults = await searchAlgolia<{ objectID: string; name: string }>({
index: "steamdb",
query: formattedName,
params: {
facetFilters: '["appType:Game"]',
hitsPerPage: `${HITS_PER_PAGE}`,
},
headers: {
Referer: "https://steamdb.info/",
},
});
const steamResults = await searchSteamGame(formattedName);

const results = steamResults.hits.map((hit) => ({
objectID: hit.objectID,
title: hit.name,
const results = steamResults.map((result) => ({
objectID: result.objectID,
title: result.name,
shop: "steam" as GameShop,
cover: getSteamAppAsset("library", hit.objectID),
cover: getSteamAppAsset("library", result.objectID),
}));

const gamesIndex = new Index({
Expand Down
61 changes: 0 additions & 61 deletions src/main/services/algolia.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/main/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./algolia";
export * from "./logger";
export * from "./repack-tracker";
export * from "./steam";
Expand Down
47 changes: 46 additions & 1 deletion src/main/services/steam.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { SteamAppDetails } from "@types";
import axios from "axios";
import { JSDOM } from "jsdom";

import type { SteamAppDetails } from "@types";

import { logger } from "./logger";

export interface SteamAppDetailsResponse {
Expand Down Expand Up @@ -31,3 +34,45 @@ export const getSteamAppDetails = async (
throw new Error(err);
});
};

export const searchSteamGame = async (term: string) => {
const searchParams = new URLSearchParams({
start: "0",
count: "50",
sort_by: "_ASC",
/* Games only */
category1: "998",
term: term,
});

const response = await axios.get(
`https://store.steampowered.com/search/results/?${searchParams.toString()}`
);

const { window } = new JSDOM(response.data);
const { document } = window;

const $anchors = Array.from(
document.querySelectorAll("#search_resultsRows a")
);

return $anchors.reduce((prev, $a) => {
const $title = $a.querySelector(".title");
const objectIDs = $a.getAttribute("data-ds-appid");

if (!objectIDs) return prev;

const [objectID] = objectIDs.split(",");

if (!objectID || prev.some((game) => game.objectID === objectID))
return prev;

return [
...prev,
{
name: $title.textContent,
objectID,
},
];
}, []);
};
8 changes: 0 additions & 8 deletions src/main/state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ interface State {
repacks: Repack[];
repackersFriendlyNames: RepackerFriendlyName[];
eventResults: Map<[string, any[]], any>;

Check warning on line 6 in src/main/state-manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 6 in src/main/state-manager.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
steamDBAlgoliaCredentials: {
applicationId: string;
apiKey: string;
};
}

const initialState: State = {
repacks: [],
repackersFriendlyNames: [],
eventResults: new Map(),
steamDBAlgoliaCredentials: {
applicationId: "",
apiKey: "",
},
};

export class StateManager {
Expand Down

0 comments on commit df11365

Please sign in to comment.