-
-
Save teetteet/3855330 to your computer and use it in GitHub Desktop.
Fetching the top colourlovers.com palettes with NodeJS
This file contains hidden or 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 numResults = 3, | |
numIteration = 3; | |
function fetchColors(offset, numResults, successHandler) { | |
http.get({ | |
host: 'www.colourlovers.com', | |
port: 80, | |
path: '/api/palettes/top?format=json&numResults=' + numResults + '&resultOffset=' + offset | |
}, function(res) { | |
var body = ""; | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
body += chunk; | |
}); | |
res.on('end', function () { | |
successHandler(JSON.parse(body)); | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
} | |
var i = 0; | |
function doIteration() { | |
fetchColors( i * numResults, numResults, function (items) { | |
items.forEach( function (item) { | |
console.log(item.title); | |
console.log(item.colors); | |
}); | |
if (i < numIteration) { | |
i++; | |
doIteration(); | |
} | |
}); | |
} | |
doIteration(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment