Create a gist now

Instantly share code, notes, and snippets.

@betzerra /README.md forked from oncletom/README.md
Last active Dec 19, 2015

What would you like to do?
Instagram Hashtag Archiver

Instagram Hashtag Archiver

This script helps you to download locally the latest pictures related to a specific Instagram hashtag. It will fetch them and sort them by username.

No resume feature. No extra metadata. No OAuth pain.

Install

You need to clone this Gist and install at least CasperJS 1.1.

brew update
brew install casperjs --devel

git clone https://gist.github.com/7970412843b1d442a6ed.git instagram-hashtag-gist

Usage

cd instagram-hashtag-gist

# Downloading the 200 latest pictures of Hack the Barbican 2013
# > hackthebarbican.org
casperjs instagram-hashtag.js htb2013 --limit=200

# Downloading the 100 latest pictures related to Sud Web
# > http://sudweb.fr
casperjs instagram-hashtag.js sudweb

Sample Output

.
├── username1
│   ├── 20d3e07acd9611e1b31022000a1d03a8_7.jpg
│   └── 27d48126cd9711e1a98b22000a1e879e_7.jpg
├── username2
│   └── 19ba6698c8ef11e1985822000a1d011d_7.jpg
├── username3
│   └── 425da056ab3e11e28b3722000a1f99d9_7.jpg
├── username4
│   └── b1365f08d2d111e2aea022000a9d0ee7_7.jpg
└── username5
    ├── 607ac52aeb8811e2be0d22000a9f14df_7.jpg
    ├── 68742bfaebd711e2974122000a9e2969_7.jpg
    ├── cf24dfb8ebbc11e28cf922000a1fbaf5_7.jpg
    ├── d75cdb0aebbb11e2b39c22000a1f8adc_7.jpg
    └── ef0e89eceb9811e2b83c22000a9e184f_7.jpg
"use strict";
var casper = require('casper').create({
waitTimeout: 10000,
pageSettings: {
loadImages: false,
loadPlugins: false
},
viewportSize: {
height: 1000,
width: 1024
}
});
var instagramTag = casper.cli.get(0);
var threshold = casper.cli.get('limit') || 100;
var baseUrl = "http://iconosquare.com/tag/" + instagramTag + '/';
var downloaded = [];
var queued = [];
if (!instagramTag){
casper.echo('Requiring at least a valid Instagram hashtag to query.').exit();
}
function queue(url){
queued.push("http://www.iconosquare.com" + url);
}
function processQueue(){
if (queued.length === 0){
return;
}
console.log("DOWNLOAD TIME!!!");
var i=0;
casper.start().each(queued, function(self, link) {
self.thenOpen(link, function() {
i++;
var username = casper.getElementInfo('a.list-username-user').text;
var photoURL = casper.getElementAttribute('a.imgWebviewerDetail:first-child img', 'src');
if (photoURL) {
var photoFilename = photoURL.split('/').pop();
var path = username + '/' + photoFilename;
self.thenOpen(photoURL, function() {
this.download(photoURL, path);
console.log('#' + i + " - @" + username + " - " + photoURL);
});
}
});
});
}
function clickAndLoad(){
casper.click('.more');
casper.waitWhileVisible('#conteneurLoaderEnCours', function(){});
casper.then(function(){
var elements = casper.getElementsAttribute('.photos-wrapper .lienPhotoGrid', 'href');
console.log("Found " + elements.length + " pictures…");
if (elements.length < threshold){
casper.waitForSelector(".more", clickAndLoad, function(){
elements.map(queue);
processQueue();
});
}
else{
elements.map(queue);
processQueue();
}
});
}
casper.start(baseUrl, clickAndLoad);
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment