Created
December 10, 2012 18:58
-
-
Save katanacrimson/4252536 to your computer and use it in GitHub Desktop.
node.js script to compare Steam games owned between multiple users
This file contains 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
var request = require('request'), | |
async = require('async'), | |
libxmljs = require('libxmljs'), | |
optimist = require('optimist') | |
.string('_') | |
.usage('Usage: $0 {username} [{username} [...]]'), | |
argv = optimist.argv | |
module.exports = {} | |
module.exports.compareUserGames = compareUserGames = function(users, end) { | |
// should use url.parse() at some point to validate urls. | |
userGamesList = {}, sameGames = false | |
async.forEachSeries(users, function(user, done) { | |
getUserGames(user, function(err, uid, userGames) { | |
if(err) done(err) | |
userGamesList[uid] = userGames | |
done(null) | |
}) | |
}, function(err) { | |
if(err) throw new Error(err) | |
for(u in userGamesList) { | |
if(sameGames === false) { | |
sameGames = userGamesList[u] | |
} else { | |
for(var g in sameGames) { | |
if(sameGames.hasOwnProperty(g) && !userGamesList[u].hasOwnProperty(g)) | |
delete sameGames[g] | |
} | |
} | |
} | |
end(null, sameGames, userGamesList) | |
}) | |
} | |
module.exports.getUserGames = getUserGames = function(userURL, done) { | |
request(userURL + 'games/?tab=recent&xml=1', function(err, resp, page) { | |
//page = fs.readFileSync('katana.xml'), err = false | |
if(err) { | |
throw new Error(err) | |
} else if (resp.statusCode != 200) { | |
throw new Error('http response ' + resp.statusCode) | |
} else { | |
var games = {}, userID = '' | |
xmlDoc = libxmljs.parseXml(page) | |
userID = xmlDoc.get('//steamID64').text() | |
xmlDoc | |
.find('//game') | |
.map(function(i) { | |
var r = {} | |
i.childNodes().forEach(function(n) { | |
if(n.name() !== 'text') | |
r[n.name()] = n.text() | |
}) | |
if(Object.keys(r).length !== 0) | |
return r | |
}) | |
.forEach(function(g) { | |
if(g && g.appID !== undefined) | |
games[g.appID] = g | |
}) | |
done(null, userID, games) | |
} | |
}) | |
} | |
module.exports.parseMatchedGames = parseMatchedGames = function(err, result, gameList) { for(var i in result) { console.log(result[i].name + ' <' + result[i].storeLink + '>') }; console.log('------------') } | |
module.exports.parseGames = parseGames = function(err, uid, gameList) { for(var i in gameList) { console.log(gameList[i].name + ' <' + gameList[i].storeLink + '>') } } | |
if(argv._.length > 1) { | |
var users = argv._.map(function(user){ | |
if(!user.match(/^[0-9]{17,25}$/)) { | |
// assume not a steamID64, go with profile url | |
user = 'http://steamcommunity.com/id/' + user | |
} else { | |
user = 'http://steamcommunity.com/profiles/' + user | |
} | |
return user + '/' | |
}) | |
if(users.length >= 2) { | |
compareUserGames(users, parseMatchedGames) | |
} else { | |
getUserGames(users.shift(), parseGames) | |
} | |
} else { | |
console.log(optimist.help()) | |
} |
This file contains 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
{ | |
"name":"steam-compare", | |
"private":true, | |
"version":"0.0.1", | |
"dependencies":{ | |
"request":"2.12.x", | |
"async":"0.1.x", | |
"libxmljs":"0.6.x", | |
"optimist":"0.3.x" | |
}, | |
"engine": "node >= 0.8.x" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment