borders/util/get_data.js
2022-04-23 23:20:03 -04:00

116 lines
2.8 KiB
JavaScript

const dotenv = require("dotenv");
dotenv.config();
const { URL, URLSearchParams } = require("url");
const fs = require("fs");
const path = require("path");
const key = process.env.STEAM_API_KEY;
let appIds = {};
async function makeReq(cursor) {
const fetch = await import("node-fetch");
const url = new URL(
"https://api.steampowered.com/ILoyaltyRewardsService/QueryRewardItems/v1/"
);
const searchParams = new URLSearchParams({
key,
"community_item_classes[0]": 14,
});
if (cursor !== null) searchParams.set("cursor", cursor); // pagination
url.search = searchParams;
const req = await fetch.default(url);
const json = await req.json();
return json.response;
}
async function getAppInfo(appid) {
if (appIds[appid]) {
return appIds[appid];
}
const fetch = await import("node-fetch");
const url = new URL(
"https://api.steampowered.com/IStoreService/GetAppList/v1/"
);
// what the fuck this is a hack
// todo: paginate all app info and store it instead of querying the api 447 times
const searchParams = new URLSearchParams({
key,
last_appid: parseInt(appid) - 1,
max_results: 1,
});
url.search = searchParams;
const req = await fetch.default(url);
const json = await req.json();
appIds[appid] = json.response.apps[0];
return json.response.apps[0];
}
async function main() {
const borders = [];
let cursor = null;
let total = 0;
let done = 0;
while (true) {
const json = await makeReq(cursor);
cursor = json.next_cursor;
total = json.total_count;
if (json.count === 0) break; // found all borders
for (const border of json.definitions) {
const appid = border.appid;
const name = border.community_item_data.item_name;
const community_item_type = border.community_item_type;
const filename_large = border.community_item_data.item_image_large;
const filename_small = border.community_item_data.item_image_small;
const appInfo = await getAppInfo(appid);
const borderURL = `https://cdn.akamai.steamstatic.com/steamcommunity/public/images/items/${appid}`;
if (filename_large != undefined) {
borders.push({
name: `${name}-LARGE`,
borderURL: `${borderURL}/${filename_large}`,
itemType: community_item_type,
appInfo,
});
}
if (filename_small != undefined) {
borders.push({
name: `${name}-SMALL`,
borderURL: `${borderURL}/${filename_small}`,
itemType: community_item_type,
appInfo,
});
}
done++;
console.log(`${done}/${total}`);
}
}
const cwd = process.cwd();
const filePath = path.join(
cwd,
cwd.includes("util") ? ".." : "",
"util/border_data.json"
);
fs.writeFileSync(filePath, JSON.stringify(borders));
}
main();