Skip to content

Instantly share code, notes, and snippets.

@teetteet
Forked from viktorkelemen/gist:1801533
Created October 8, 2012 22:17
Show Gist options
  • Save teetteet/3855330 to your computer and use it in GitHub Desktop.
Save teetteet/3855330 to your computer and use it in GitHub Desktop.
Fetching the top colourlovers.com palettes with NodeJS
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