Skip to content

Instantly share code, notes, and snippets.

@MalikAQayum
Forked from Jimbly/index.js
Created Nov 20, 2019
Embed
What would you like to do?
Steam CD Key Batch query
/* jshint esversion: 6*/
const assert = require('assert');
const async = require('async');
const fs = require('fs');
let request = require('request');
const FileCookieStore = require('tough-cookie-filestore');
if (!fs.existsSync('cookies.json')) { fs.writeFileSync('cookies.json', '{}'); }
let j = request.jar(new FileCookieStore('cookies.json'));
request = request.defaults({ jar : j });
// Fill cookie string here by grabbing it from a request in Chrome
// Should look like 'sessionid=c7...; steamLogin=2...; ...'
let cookies_from_browser = 'fill=me; in=please'; // Make sure not to check this in
let cookie_url = 'https://partner.steamgames.com';
cookies_from_browser.split('; ').forEach(function (cookie) {
j.setCookie(cookie, cookie_url);
});
function queryKeys(next) {
let keys = fs.readFileSync('keys.csv', 'utf8').split('\n').map(function (s) {
return s.trim();
});
let out = [];
async.eachSeries(keys, function (key, next) {
if (!key) {
console.log('');
out.push('');
return void next();
}
let retries = 0;
function doAttempt() {
let url = `https://partner.steamgames.com/querycdkey/cdkey?cdkey=${key}&method=Query`;
request(url, function (err, resp, body) {
let result = body.split('<h2>Activation Details</h2>')[1];
if (!result && !err) {
if (retries < 5) {
++retries;
console.log(`Error, retrying ${retries}...`);
return doAttempt();
}
fs.writeFileSync('out.html', body);
console.log('Error quering CD Key, check out.html to see the response returned from Steamworks,');
console.log(' if it is a login page, your cookies_from_browser string is probably invalid');
process.exit(-1);
}
assert(result);
result = result.split('</table>')[0];
result = result.match(/<td>.*<\/td>/gu);
result = result.map(function (line) {
return line.replace(/<[^>]*>/gu, '');
});
let line = [key, (result[0] === 'Activated') ? '"' + result[1] + '"' : result[0]].join('\t');
console.log(line);
out.push(line);
return next();
});
}
doAttempt();
}, function (err) {
fs.writeFileSync('out.tsv', out.join('\n'));
next(err);
});
}
queryKeys(function (err) {
if (err) {
throw err;
} else {
console.log('Done.');
}
});
{
"dependencies": {
"async": "^2.4.1",
"request": "^2.81.0",
"tough-cookie-filestore": "0.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment