Skip to content

Instantly share code, notes, and snippets.

@chapel
Created October 30, 2010 12:05
Show Gist options
  • Save chapel/655238 to your computer and use it in GitHub Desktop.
Save chapel/655238 to your computer and use it in GitHub Desktop.
var chain = require("slide/chain"),
asyncMap = require("slide/async-map"),
ClientPool = require('./client_pool'),
LimitingQueue = require('./lib/limitingqueue'),
q = new LimitingQueue(10),
api = new ClientPool(80, 'www.bungie.net', 100),
db = new ClientPool(5984, 'secret', 100)
apikey = 'secret'
function getIds(gamerTag, callback) {
var pages = 0,
gameIds = []
function getPage(gamerTag, page, cb) {
apiGet('/api/reach/reachapijson.svc/player/gamehistory/'+apikey+'/'+gamerTag+'/arena/'+page, function(data) {
if (data) {
parseData(data, function(check) {
if (check) {
pages++
getPage(gamerTag, pages, callback)
} else {
cb(gameIds)
}
})
}
else new Error('Page '+page+' not accessible.')
})
}
function parseData(data, cb) {
var games = data.RecentGames,
count = games.length;
games.some(function(game) {
var gameId = game.GameId,
gameDate = new Date(parseInt(game.GameTimestamp.substr(6))),
tempDate = new Date()
tempDate.setDate(1), tempDate.setHours(3), tempDate.setMinutes(0), tempDate.setSeconds(0)
checkDate = new Date(tempDate)
if (gameDate > checkDate) {
gameIds.push(gameId)
count--
if (count <= 0) cb(true)
} else {
cb(false)
return true
}
})
}
getPage(gamerTag, pages, callback);
}
function apiGet(url, callback) {
q.push(function(){
var request = api.request('GET', url, {'host': 'www.bungie.net'});
request.end();
request.on('response', function(response) {
if (response.statusCode == 200) {
var buffer = '';
response.on('data', function (chunk) {
buffer+=chunk;
});
response.on('end', function() {
callback(JSON.parse(buffer));
});
} else {
response.on('end', function() {
callback(false);
});
}
});
});
}
getIds('thechapel', function(ids) {
console.log(ids)
})
/Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:24
cb(gameIds)
^
TypeError: undefined is not a function
at CALL_NON_FUNCTION (native)
at /Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:24:13
at /Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:48:9
at Array.some (native)
at parseData (/Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:36:11)
at /Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:18:9
at IncomingMessage.<anonymous> (/Volumes/Fatty/chapel/Dropbox/Projects/reach arena stats/newparser.js:68:25)
at IncomingMessage.emit (events:41:20)
at HTTPParser.onMessageComplete (http:112:23)
at Client.onData [as ondata] (http:902:27)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment