|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import SteamUser from "steam-user"; |
| 4 | + |
| 5 | +import Cache from "./cache.mjs"; |
| 6 | +import Config from "./config.mjs"; |
| 7 | + |
| 8 | +const updateRate = process.env.UPDATE_RATE ? parseInt(process.env.UPDATE_RATE, 10) : 10000; |
| 9 | + |
| 10 | +const client = new SteamUser(); |
| 11 | +const config = new Config(); |
| 12 | +await config.init(); |
| 13 | +const cache = new Cache(); |
| 14 | +await cache.init(); |
| 15 | + |
| 16 | +const sendWebhook = async (webhook) => { |
| 17 | + try { |
| 18 | + const response = await fetch(`https://api.github.com/repos/${webhook.repo}/actions/workflows/${webhook.workflow_id}/dispatches`, { |
| 19 | + method: "POST", |
| 20 | + headers: { |
| 21 | + Accept: "application/vnd.github.everest-preview+json", |
| 22 | + "Content-Type": "application/json", |
| 23 | + Authorization: `Bearer ${webhook.access_token}`, |
| 24 | + }, |
| 25 | + body: JSON.stringify({ |
| 26 | + ref: webhook.branch || "main", |
| 27 | + }), |
| 28 | + }); |
| 29 | + if (response && !response.ok) { |
| 30 | + console.error(`Failed to send webhook: ${response.status} ${response.statusText} ${response.url}`); |
| 31 | + } else { |
| 32 | + console.info(`Webhook sent successfully to ${webhook.repo} for workflow ${webhook.workflow_id}`); |
| 33 | + } |
| 34 | + } catch (err) { |
| 35 | + console.error(err); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +client.setOptions({ |
| 40 | + enablePicsCache: true, |
| 41 | + changelistUpdateInterval: updateRate, |
| 42 | + machineIdType: SteamUser.EMachineIDType.PersistentRandom, |
| 43 | +}); |
| 44 | + |
| 45 | +client.logOn(config.getSteamLogins()); |
| 46 | + |
| 47 | +const initAfterLogin = async () => { |
| 48 | + Object.keys(config.getApp()).forEach(async (appid) => { |
| 49 | + // for some reason getProductInfo only times out |
| 50 | + // hopefully waiting 30s after boot is enough to make picsCache never be empty |
| 51 | + // if (!client.picsCache.apps[appid]) { |
| 52 | + // console.debug("Product info fetching"); |
| 53 | + // await client.getProductInfo([appid], []); |
| 54 | + // console.debug("Product info fetched"); |
| 55 | + // } |
| 56 | + if (!client.picsCache.apps[appid]?.appinfo) { |
| 57 | + console.info(`App ${appid} is not available`); |
| 58 | + return; |
| 59 | + } |
| 60 | + const branch = config.getBranch(appid); |
| 61 | + const branchData = client.picsCache.apps[appid].appinfo.depots?.branches?.[branch]; |
| 62 | + if (!branchData) { |
| 63 | + console.info(`Branch ${branch} is not available for app ${appid}`); |
| 64 | + return; |
| 65 | + } |
| 66 | + if (cache.is_buildid_updated(appid, branchData.buildid)) { |
| 67 | + config.getApp(appid)?.webhooks.forEach((webhook) => { |
| 68 | + sendWebhook(webhook, appid); |
| 69 | + }); |
| 70 | + } |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
| 74 | +client.on("loggedOn", () => { |
| 75 | + console.info("Logged into Steam"); |
| 76 | + setTimeout(initAfterLogin, 30000); |
| 77 | +}); |
| 78 | + |
| 79 | +client.on("error", (err) => { |
| 80 | + console.error(err); |
| 81 | +}); |
| 82 | + |
| 83 | +client.on("appUpdate", (appid, data) => { |
| 84 | + console.info(`App ${appid} has been updated`); |
| 85 | + if (!config.getApp(appid)) { |
| 86 | + console.info(`App ${appid} is not being monitored`); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const branch = config.getBranch(appid); |
| 91 | + const newBranchData = data?.appinfo?.depots?.branches?.[branch]; |
| 92 | + const oldBranchData = client.picsCache.apps[appid]?.appinfo?.depots?.branches?.[branch]; |
| 93 | + |
| 94 | + if ( |
| 95 | + (newBranchData && cache.is_buildid_updated(appid, newBranchData.buildid)) || |
| 96 | + (oldBranchData && newBranchData && oldBranchData.buildid !== newBranchData.buildid) |
| 97 | + ) { |
| 98 | + config.getApp(appid)?.webhooks?.forEach((webhook) => { |
| 99 | + sendWebhook(webhook, appid); |
| 100 | + }); |
| 101 | + } |
| 102 | +}); |
0 commit comments