Skip to content

Commit

Permalink
Minor example script updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Sep 3, 2024
1 parent 8f8f94e commit 00137ff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 34 deletions.
10 changes: 7 additions & 3 deletions examples/basicbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
const SteamUser = require('../index.js'); // Replace this with `require('steam-user');` if used outside of the module directory
let client = new SteamUser();

client.logOn({
accountName: 'username',
password: 'password'
const collectCredentials = require('./lib/collect_credentials');

collectCredentials().then((credentials) => {
client.logOn({
accountName: credentials.accountName,
password: credentials.password
});
});

client.on('loggedOn', function(details) {
Expand Down
62 changes: 31 additions & 31 deletions examples/legacycdkeysdumper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const SteamUser = require('steam-user');
const fs = require('fs');
const SteamUser = require('../index.js'); // Replace this with `require('steam-user');` if used outside of the module directory
const {writeFile} = require('fs/promises');

const credentials = {
accountName: 'username', // your steam username
password: 'password', // your steam password
};
const collectCredentials = require('./lib/collect_credentials');

const client = new SteamUser({
enablePicsCache: true,
Expand All @@ -15,49 +12,52 @@ client.on('loggedOn', () => {
});

client.on('ownershipCached', async () => {
// These keys in appdata indicate the presence of a legacy cd-key
const LEGACY_CD_KEY_INDICATORS = [
'hadthirdpartycdkey',
'legacykeydisklocation',
'legacykeyfromapp',
'legacykeylinkedexternally',
'legacykeyproofofpurchaseticket',
'legacykeyregistrationmethod',
'legacykeyregistrylocation',
'showcdkeyinmenu',
'showcdkeyonlaunch',
'supportscdkeycopytoclipboard',
'thirdpartycdkey',
];

let apps = client.getOwnedApps({
excludeExpiring: false,
excludeFree: false,
excludeShared: false
}).filter(appid => {
// these keys should indicate presence of a legacy cd key
return [
'hadthirdpartycdkey',
'legacykeydisklocation',
'legacykeyfromapp',
'legacykeylinkedexternally',
'legacykeyproofofpurchaseticket',
'legacykeyregistrationmethod',
'legacykeyregistrylocation',
'showcdkeyinmenu',
'showcdkeyonlaunch',
'supportscdkeycopytoclipboard',
'thirdpartycdkey',
].some((key) => Object.keys((client.picsCache
&& client.picsCache.apps[appid]
&& client.picsCache.apps[appid].appinfo
&& client.picsCache.apps[appid].appinfo.extended) || {}).map((k) => k.toLowerCase())
.includes(key));
return LEGACY_CD_KEY_INDICATORS.some((key) => Object.keys(client.picsCache?.apps[appid]?.appinfo?.extended || {})
.map((k) => k.toLowerCase())
.includes(key)
);
});

console.log(`Requesting legacy cd keys for ${apps.length} owned apps...`);
let keys = {};
for (let appid of apps) {
let appName = client.picsCache?.apps[appid]?.appinfo?.common?.name || appid;
try {
let {key} = await client.getLegacyGameKey(appid);
keys[appid] = key;
console.log(`Successfully retrieved key for "${appName}" (${appid})`);
} catch (err) {
if (err.eresult !== SteamUser.EResult.Fail) { // usually just means no cd key present
console.error(`App: ${appid}`, err);
}
console.log(`Failed to retrieve key for "${appName}" (${appid}): ${SteamUser.EResult[err.eresult] || err.eresult || err.message}`);
}
}
fs.writeFileSync('legacy_keys.json', JSON.stringify(keys, null, 4));

await writeFile('legacy_keys.json', JSON.stringify(keys, null, '\t'));
console.log('Done! Logging off...');
client.logOff();
});

client.on('disconnected', () => process.exit(0));

credentials.logonID = Math.round(Date.now() / 1000); // To avoid getting kicked by LogonSessionReplaced
client.logOn(credentials);
collectCredentials().then((credentials) => {
credentials.logonID = Math.round(Date.now() / 1000); // To avoid getting kicked by LogonSessionReplaced
client.logOn(credentials);
});
41 changes: 41 additions & 0 deletions examples/lib/collect_credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const {createInterface} = require('readline');

/**
* @returns {Promise<{accountName: string, password: string}>}
*/
module.exports = async function collectCredentials() {
let accountName = await promptAsync('Account Name: ');
let password = await promptAsync('Password: ', true);
return {accountName, password};
}

Check failure on line 10 in examples/lib/collect_credentials.js

View workflow job for this annotation

GitHub Actions / lint / lint

Missing semicolon

/**
*
* @param {string} question
* @param {boolean} [sensitiveInput=false]
* @return Promise<string>
*/
function promptAsync(question, sensitiveInput) {
return new Promise((resolve) => {
let rl = createInterface({
input: process.stdin,
output: sensitiveInput ? null : process.stdout,
terminal: true
});

if (sensitiveInput) {
// We have to write the question manually if we didn't give readline an output stream
process.stdout.write(`${question.trim()} [masked] `);
}

rl.question(question, (result) => {
if (sensitiveInput) {
// We have to manually print a newline
process.stdout.write('\n');
}

rl.close();
resolve(result);
});
});
}

0 comments on commit 00137ff

Please sign in to comment.