Skip to content

Instantly share code, notes, and snippets.

@harthur
Created May 2, 2012 22:50
Show Gist options
  • Select an option

  • Save harthur/2581133 to your computer and use it in GitHub Desktop.

Select an option

Save harthur/2581133 to your computer and use it in GitHub Desktop.

Revisions

  1. harthur created this gist May 2, 2012.
    68 changes: 68 additions & 0 deletions fetchgrams.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    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");
    })
    })
    })
    }