Created
March 28, 2023 01:42
-
-
Save braddevans/6eefd9b5eae22ae7c8e3ee4b648e09d3 to your computer and use it in GitHub Desktop.
get game hours by most played steam requires <apikey> and <steamid> changing
This file contains hidden or 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
const request = require('request'); | |
const Handlebars = require('handlebars'); | |
// Replace with your Steam Web API key | |
const API_KEY = '<apikey>'; | |
// Replace with the Steam profile URL of the user | |
const PROFILE_URL = 'https://steamcommunity.com/profiles/<steamid>'; | |
// Retrieve the SteamID from the profile URL | |
const STEAMID_REGEX = /\/profiles\/(\d+)/; | |
const match = PROFILE_URL.match(STEAMID_REGEX); | |
if (!match) { | |
console.error(`Invalid profile URL: ${PROFILE_URL}`); | |
return; | |
} | |
let steam_apps; | |
let steam_app_kv = new Map(); | |
const steamid = match[1]; | |
function getAPPS() { | |
return new Promise((resolve, reject) => { | |
request(`https://api.steampowered.com/ISteamApps/GetAppList/v0002/`, async (error, response, body) => { | |
if (error) { | |
reject(error); | |
} else { | |
console.log(JSON.parse(body).applist.apps); | |
steam_apps = JSON.parse(body).applist.apps; | |
for (let i = 0; i < steam_apps.length; i++) { | |
steam_app_kv[String(steam_apps[i].appid)] = steam_apps[i].name; | |
} | |
console.log(steam_apps) | |
resolve(body); | |
} | |
}); | |
}); | |
} | |
function getByID(appid) { | |
return steam_app_kv[appid]; | |
} | |
// Make a request to the Steam Web API to retrieve the list of games played by the user | |
request(`https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=${API_KEY}&steamid=${steamid}&format=json`, async (error, response, body) => { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
await getAPPS(); | |
const games = JSON.parse(body).response.games; | |
// Sort the games by playtime in descending order | |
games.sort((a, b) => b.playtime_forever - a.playtime_forever); | |
const checkid0 = steam_app_kv[games[0].appid]; | |
console.log(checkid0); | |
for (let i = 0; i < games.length; i++) { | |
console.log(`[${i}] parsing id: ` + games[i].appid); | |
const app_name = steam_app_kv[games[i].appid]; | |
console.log(`[${i}] name: ${app_name}`); | |
if ([undefined, null].includes(app_name)){ | |
games[i].name = "App removed from store"; | |
} else { | |
games[i].name = app_name; | |
} | |
games[i].playtime_hours = (games[i].playtime_forever / 60).toFixed(2); | |
} | |
console.log(games) | |
// Generate HTML code to display the sorted list of games and their playtime | |
const template = Handlebars.compile(` | |
<h1>Games played by {{steamid}}</h1> | |
<ol> | |
{{#each games}} | |
<li>[{{this.appid}}] {{this.name}} - {{this.playtime_hours}} Hours</li> | |
{{/each}} | |
</ol> | |
`); | |
const html = template({steamid, games}); | |
// Display the generated HTML code in the console | |
console.log(html); | |
const express = require('express'); | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send(html); | |
}); | |
const port = 3000; | |
app.listen(port, () => { | |
console.log(`Server listening on port ${port}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment