Created
July 27, 2017 11:44
-
-
Save NoelLH/3d445247a83a23ccd21877399a965046 to your computer and use it in GitHub Desktop.
Get amounts total for first 16 pages of grants grouped by issue
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
// Hardcoded number of pages - hacky demo! | |
var request = require('request'); | |
var numLoaded = 0; | |
var grants = []; | |
var totalPages = 16; | |
function readPage(pageNum, resolve) { | |
request( | |
'https://1kfs7evxca.execute-api.eu-west-1.amazonaws.com/beta/grants?page=' + pageNum, | |
function (err, res, body) { | |
if (err) { | |
console.log('ERROR ON PAGE ' + pageNum, err); | |
} else { | |
var text = JSON.parse(body); | |
grants = grants.concat(text.data.grants); | |
numLoaded++; | |
if (numLoaded === totalPages) { | |
resolve(); | |
} | |
} | |
} | |
); | |
} | |
var allLoadedPromise = new Promise(function(resolve) { | |
for (var ii = 1; ii <= totalPages; ii++) { | |
readPage(ii, resolve) | |
} | |
}); | |
allLoadedPromise.then(function() { | |
var amountByIssue = []; | |
for (var ii = 0; ii < grants.length; ii++) { | |
if (!amountByIssue[grants[ii].data.issue]) { | |
amountByIssue[grants[ii].data.issue] = 0.00; | |
} | |
amountByIssue[grants[ii].data.issue] += parseFloat(grants[ii].data.amount_awarded); | |
} | |
console.log(amountByIssue); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment