Created
October 9, 2011 23:40
-
-
Save strager/1274374 to your computer and use it in GitHub Desktop.
Get Steam username from UID
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 http = require('http'); | |
var steamUidRe = /<steamID64>(.*)<\/steamID64>/; | |
function getSteamUid(username, callback) { | |
var callbackCalled = false; | |
var req = http.request({ | |
host: 'steamcommunity.com', | |
port: 80, | |
path: '/id/' + encodeURI(username) + '?xml=1', | |
method: 'GET' | |
}, function (res) { | |
var data = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
if (callbackCalled) { | |
return; | |
} | |
data += chunk; | |
var match = steamUidRe.exec(data); | |
if (match) { | |
// TODO Close connection somehow | |
callbackCalled = true; | |
callback(null, match[1]); | |
} | |
}); | |
res.on('end', function () { | |
if (!callbackCalled) { | |
callback(new Error('Failed to load UID')); | |
} | |
}); | |
}); | |
req.on('error', function (err) { | |
callback(err); | |
}); | |
req.end(); | |
} | |
var steamUsernameRe = /<steamID>\s*<!\[CDATA\[(.*?)\]\]>\s*<\/steamID>/; | |
function getSteamUsername(uid, callback) { | |
var callbackCalled = false; | |
var req = http.request({ | |
host: 'steamcommunity.com', | |
port: 80, | |
path: '/profiles/' + encodeURI(uid) + '?xml=1', | |
method: 'GET' | |
}, function (res) { | |
var data = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
if (callbackCalled) { | |
return; | |
} | |
data += chunk; | |
var match = steamUsernameRe.exec(data); | |
if (match) { | |
// TODO Close connection somehow | |
callbackCalled = true; | |
callback(null, match[1]); | |
} | |
}); | |
res.on('end', function () { | |
if (!callbackCalled) { | |
callback(new Error('Failed to load username')); | |
} | |
}); | |
}); | |
req.on('error', function (err) { | |
callback(err); | |
}); | |
req.end(); | |
} | |
var steamUsernameHackRe = /\/([^\/]*)\/$/; | |
function getSteamUsernameHack(uid, callback) { | |
var req = http.request({ | |
host: 'steamcommunity.com', | |
port: 80, | |
path: '/profiles/' + encodeURI(uid), | |
method: 'HEAD' | |
}, function (res) { | |
if (!res.headers.location) { | |
} | |
var match = steamUsernameHackRe.exec(res.headers.location || ''); | |
if (match) { | |
return callback(null, match[1]); | |
} else { | |
return callback(new Error('Hack failed')); | |
} | |
}); | |
req.on('error', function (err) { | |
callback(err); | |
}); | |
req.end(); | |
} | |
function getSteamUsernameFast(uid, callback) { | |
getSteamUsernameHack(uid, function (err, username) { | |
if (err) { | |
getSteamUsername(uid, callback); | |
} else { | |
callback(null, username); | |
} | |
}); | |
} | |
if (require.main === module) { | |
if (process.argv.length < 3) { | |
console.log('Usage: node steam.js uid [uid ...]'); | |
process.exit(1); | |
return; | |
} | |
process.argv.slice(2).forEach(function (uid) { | |
getSteamUsernameFast(uid, function (err, username) { | |
if (err) throw err; | |
console.log(uid + ' ' + username); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment