UNPKG

bundlecamp-xbox-live

Version:

A utility for fetching xbox live gamer data

56 lines (54 loc) 2.27 kB
var array = require('async-arrays'); var request = require('request'); function XBoxLive(){ var baseUrl = "http://"+(process.env.XBOX_LIVE_MAGIC_COOKIE ? process.env.XBOX_LIVE_MAGIC_COOKIE + "@" : "") +"xbox-live.herokuapp.com/%.php?gamertag="; this.source = 'xboxleaders'; this.sources = { xboxleaders : { profileURL : baseUrl.replace("%", "profile"), gamesURL : baseUrl.replace("%", "games"), achievementsURL : baseUrl.replace("%", "achievements"), friendsURL : baseUrl.replace("%", "friends"), postProcess : function(payload){ return payload.data; }, addGameID : function(path, id){ return path + '&titleid=' + id; } }, xboxapi : { profileURL : "https://xboxapi.com/profile/", gamesURL : "https://xboxapi.com/games/", acheivementsURL : "https://xboxapi.com/achievements/", friendsURL : "https://xboxapi.com/friends/", postProcess : function(payload){ return payload.Player; }, addGameID : function(path, id){ return path + '/' +id; } } //RIP http://xboxapi.duncanmackenzie.net/ }; } XBoxLive.prototype.fetch = function(type, gamertag, game, callback){ if((!callback) && typeof game == 'function'){ callback = game; game = undefined; } //todo: check game type if(!array.contains(['profile', 'games', 'achievements', 'friends'], type)) throw('Unknown type'); var uri = this.sources[this.source][type+'URL']+gamertag if(game && this.sources[this.source].addGameID) uri = this.sources[this.source].addGameID(uri, game); var ob = this; request({ method: 'GET', uri: uri }, function (error, response, body) { var result = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(body.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + body + ')'); if(ob.sources[ob.source].postProcess) result = ob.sources[ob.source].postProcess(result); if(result && result.code && result.message) callback(result); else callback(error, result); }); }; module.exports = XBoxLive;