Skip to content

Commit

Permalink
feature: get steam new games
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEnx authored and JackEnx committed Apr 22, 2024
1 parent 1395cd4 commit ea00b68
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getNewRepacksFromXatab,
// getNewRepacksFromOnlineFix,
readPipe,
steamGetNewGames,
startProcessWatcher,
writePipe,
} from "./services";
Expand Down Expand Up @@ -126,4 +127,7 @@ const loadState = async () => {
import("./events");
};

loadState().then(() => checkForNewRepacks());
loadState().then(() => {
steamGetNewGames();
checkForNewRepacks();
});
1 change: 1 addition & 0 deletions src/main/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./fifo";
export * from "./torrent-client";
export * from "./how-long-to-beat";
export * from "./process-watcher";
export * from "./steam-get-new-games";
104 changes: 104 additions & 0 deletions src/main/services/steam-get-new-games.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import axios from "axios";
import { steamGameRepository } from "@main/repository";
import { logger } from "./logger";

interface SteamAppsResponse {
applist: { apps: Array<{ appid: number; name: string }> };
}

const sleep = async (time: number) => {
await new Promise((resolve) => {
setTimeout(() => {
resolve(null);
}, time);
});
};

const steamGameInformation = async (gameId: number) => {
return await axios
.get(`http://store.steampowered.com/api/appdetails?appids=${gameId}`, {
method: "GET",
})
.then((res) => {
return res.data;
});
};

const minutesToMilliseconds = (minutes: number) => {
const milliseconds = 1000;
const seconds = 60;
return minutes * seconds * milliseconds;
};

export const steamGetNewGames = async () => {
const lastGameId = (
await steamGameRepository
.createQueryBuilder()
.orderBy("id", "DESC")
.limit(1)
.execute()
)?.[0]?.SteamGame_id;

if (!lastGameId) return;

const steamGames: Array<{ appid: number; name: string }> | null = await axios
.get(
"https://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json"
)
.then((res) => {
if (res.status === 200) return res.data;
return null;
})
.then((data: SteamAppsResponse) => {
if (!data) return data;
return data.applist.apps
.filter((v) => v.appid > lastGameId)
.sort((a, b) => a.appid - b.appid);
})
.catch((err) => {
logger.error(err, { method: "steamGetNewGames" });
return null;
});

if (!steamGames) return;

const fetchSize = 20;
const sleepTime = minutesToMilliseconds(3);

let fetchArr = [];

try {
for (let i = 0; i < steamGames.length; i += fetchSize) {
for (
let j = i;
j < steamGames.length && fetchArr.length < fetchSize;
j++
) {
fetchArr.push(steamGameInformation(steamGames[j].appid));
}

const games: Array<{
id: number | null;
name: string | null;
}> = (await Promise.all(fetchArr))
.map((result: any) => {

Check warning on line 84 in src/main/services/steam-get-new-games.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 84 in src/main/services/steam-get-new-games.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, out/Hydra-win32-x64, Hydra-win32-x64)

Unexpected any. Specify a different type

Check warning on line 84 in src/main/services/steam-get-new-games.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, out/Hydra-linux-x64, Hydra-linux-x64)

Unexpected any. Specify a different type
const data = result[Object.keys(result)[0]]?.data;
const type: string = data?.type;
if (type === "game") {
return { id: data.steam_appid, name: data.name };
} else {
return { id: null, name: null };
}
})
.filter((game) => game.id);

fetchArr = [];

await steamGameRepository.insert(games);

await sleep(sleepTime);
}
} catch (err) {
logger.error(err, { method: "steamGetNewGames" });
}
};

0 comments on commit ea00b68

Please sign in to comment.