-
-
Save mageddo/0103ff99ec0aef30a2fd705faf7b532e to your computer and use it in GitHub Desktop.
Gets the float value of a Steam inventory item
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
'use strict'; | |
/*jshint node:true */ | |
// npm install lodash colors minimist request sync-request | |
var _ = require('lodash'), | |
colours = require('colors/safe'), | |
parseArgs = require('minimist'), | |
request = require('request'), | |
requestSync = require('sync-request'), | |
usage = 'usage: STEAM_API_KEY=<steam api key> node steamGetFloatValue.js -u <item URL> || -s <steam id> -i <asset id> [ -g <game id> ]'; | |
var argv = parseArgs(process.argv.slice(2), {string: 's' }); // force -s value to string, JS can't handle a number that large | |
var apiKey = process.env.STEAM_API_KEY, | |
gameId = argv.g || '730', | |
itemId = argv.i, | |
steamId = argv.s, | |
itemUrl = argv.u; | |
if (!apiKey) { | |
console.log(colours.red('STEAM_API_KEY environment variable not set.')); | |
console.log(usage); | |
process.exit(1); | |
} | |
if (!itemUrl && (!itemId || !steamId)) { | |
console.log(colours.red('missing item or steam id')); | |
console.log(usage); | |
process.exit(1); | |
} | |
if (itemUrl) { | |
// split itemUrl on slashes | |
var arrItemUrl = itemUrl.split('/'); | |
// if a vanity profile is present | |
if (arrItemUrl[3] === 'id') { | |
// convert name to steamID | |
steamId = getSteamIdFromSteamName(arrItemUrl[4]); | |
} else { | |
steamId = arrItemUrl[4].toString(); | |
} | |
// split 7th array item on underscore | |
var arrItemId = arrItemUrl[6].split('_'); | |
// assetid is 3rd item in underscore split array | |
itemId = arrItemId[2]; | |
} | |
var url = 'http://api.steampowered.com/IEconItems_' + | |
gameId + '/GetPlayerItems/v0001/?key=' + | |
apiKey + '&SteamID=' + steamId; | |
var userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'; | |
var options = { | |
url: url, | |
headers: { | |
'User-Agent': userAgent | |
} | |
}; | |
// | |
// main | |
// | |
//console.log('trying url ' + url); | |
doApiCall(options, gameId, steamId, itemId); | |
// | |
// end main | |
// | |
function doApiCall(options, gameId, steamId, itemId) { | |
request.get(options, function(err, res, body) { | |
//console.log('got response: ' + res); | |
if (!res || res.statusCode === 503) { | |
console.log(colours.yellow('Steam API crapping out, trying again')); | |
//sleep 1 sec then doApiCall(); | |
setTimeout(doApiCall(options, gameId, steamId, itemId), 1000); | |
} else { | |
// body comes back as JSON | |
var inventory = JSON.parse(body); | |
// if we get a good response, search inventory for item id | |
if (inventory && inventory.result.status === 1) { | |
// console.log('looking for item ' + itemId); | |
var item = _.find(inventory.result.items, { 'id': parseInt(itemId) }); | |
if (item) { | |
var float = _.find(item.attributes, { 'defindex': 8 }); | |
console.log('found item ' + colours.green(getMarketHashName(gameId, steamId, itemId)) + | |
' / id: ' + item.id + | |
' / original_id : ' + item.original_id + | |
' / float value: ' + colours.green(float.float_value.toString().substr(0,7)) | |
); | |
} else { | |
console.log(colours.red('item id ' + itemId + ' not found in user inventory')); | |
process.exit(); | |
} | |
} else { | |
//console.dir(body); | |
console.log(colours.red('got an empty or bad response - check user id')); | |
process.exit(); | |
} | |
} | |
}); | |
} | |
function getSteamIdFromSteamName(steamName) { | |
var resolveVanityUrl = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=' + | |
apiKey + '&vanityurl=' + steamName; | |
var res = requestSync('GET', resolveVanityUrl); | |
var result = JSON.parse(res.getBody()); | |
if (result.response.success == 1) { | |
console.log('using steamID: ' + result.response.steamid); | |
return result.response.steamid.toString(); | |
} else { | |
throw 'steamId not returned'; | |
} | |
} | |
function getMarketHashName(gameId, steamId, itemId) { | |
var inventoryJsonUrl = 'http://steamcommunity.com/profiles/' + steamId + '/inventory/json/' + gameId + '/2/trading=1'; | |
var res = requestSync('GET', inventoryJsonUrl); | |
var result = JSON.parse(res.getBody()); | |
if (result.success) { | |
var classId = result.rgInventory[itemId.toString()].classid, | |
instanceId = result.rgInventory[itemId.toString()].instanceid; | |
return result.rgDescriptions[classId + '_' + instanceId].market_hash_name; | |
} else { | |
throw 'JSON inventory not returned'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment