Skip to content

Instantly share code, notes, and snippets.

@harthur
Created May 2, 2012 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harthur/2581133 to your computer and use it in GitHub Desktop.
Save harthur/2581133 to your computer and use it in GitHub Desktop.
Download pictures with cat tag from Instagram
var http = require("http"),
url = require("url"),
fs = require("fs"),
async = require("async"),
Instagram = require('instagram-node-lib');
Instagram.set('client_id', /* client key */);
Instagram.set('client_secret', /* client secret */);
fetchTag('cat', 400);
function fetchTag(tag, total) {
var count = 0;
var fetched = {}; // keep duplicates at bay
var next_max_id = null;
async.whilst(
function () {
return count < total;
},
function (done) {
console.log("fetching photos for tag '" + tag + "'");
var options = {
name: tag,
count: total,
complete: function(data, pagination) {
next_max_id = pagination.next_max_id;
data.forEach(function(datum) {
var uri = datum.images.low_resolution.url;
if (!fetched[uri]) {
count++;
saveFile(count, uri);
fetched[uri] = true;
}
});
setTimeout(done, 10000);
}
}
if (next_max_id) {
options.next_max_id = next_max_id;
}
Instagram.tags.recent(options);
}
);
}
function saveFile(id, uri) {
var options = url.parse(uri);
http.get(options, function(res) {
var imagedata = "";
res.setEncoding('binary');
res.on('data', function(chunk) {
imagedata += chunk
});
res.on('end', function(){
fs.writeFile("instagrams/" + id + '.jpg', imagedata, 'binary', function(err) {
if (err) throw err;
console.log("File " + id + " saved");
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment