Last active
October 17, 2016 09:02
-
-
Save ttezel/e894241e0c96604bebde4853f1c13a27 to your computer and use it in GitHub Desktop.
Download instagram photos with >= N likes (run from an instagram user's profile page)
This file contains 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 MAX_PHOTOS = 40; | |
var MIN_NUM_LIKES = 40; | |
var currIndex = 1; | |
var numDownloaded = 0; | |
var previewOpened = function (imgIndex) { | |
return !!($('._tf9x3').text() && $('img._icyx7').get(imgIndex-1)) | |
} | |
var maybeDownloadImageAtIndex = function (index, cb) { | |
console.log('checking image', index); | |
var $img = $('img').get(index); | |
$img.click(); | |
while (!previewOpened(index)) { | |
// wait til preview open | |
console.log('waiting for preview to open. index', index) | |
setTimeout(function () { | |
maybeDownloadImageAtIndex(index, cb); | |
}, 10); | |
return | |
} | |
var numLikes = Number($('._tf9x3').text().split(' ')[0]); | |
console.log('numLikes', numLikes); | |
var caption = $('li._nk46a h1').text(); | |
// console.log('caption', caption) | |
if (numLikes >= MIN_NUM_LIKES && caption.indexOf('#eeeeeats') !== -1) { | |
console.log('downloading', caption) | |
// create a download link for the image and click to invoke download. | |
var imgUrl = $('img._icyx7').get(index-1).src | |
// console.log('imgUrl', imgUrl) | |
var a = document.createElement('a'); | |
a.href = imgUrl; | |
a.setAttribute('download', 'foo.jpg'); | |
a.filename = 'foo.jpg' | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
numDownloaded++; | |
console.log('downloaded', numDownloaded, 'images'); | |
} | |
// close the preview | |
$('button._3eajp').click(); | |
cb() | |
}; | |
var scrollBottom = function () { | |
var scrollTo = $(document).height(); | |
console.log('scrolling to', scrollTo) | |
$(document).scrollTop(scrollTo); | |
// scroll up and back down to bottom to re-trigger a fetch | |
// since sometimes instagram's scroll handler won't get triggered | |
$(document).scrollTop(scrollTo-1000); | |
$(document).scrollTop(scrollTo); | |
} | |
var awaitScrolled = function (cb) { | |
scrollBottom(); | |
var loadMoreLink = $('a._oidfu').get(0); | |
if (loadMoreLink) { | |
console.log('clicking "Load more"') | |
loadMoreLink.click() | |
} | |
setTimeout(function () { | |
scrollBottom() | |
setTimeout(function () { | |
cb() | |
}, 100) | |
}, 100) | |
} | |
var recursiveGetImage = function () { | |
awaitScrolled(function () { | |
if (!($('img').get(currIndex))) { | |
console.log('No more images to download! Stopped at index', currIndex); | |
return; | |
} | |
checkAndMaybeDownloadImage(); | |
}); | |
var checkAndMaybeDownloadImage = function () { | |
maybeDownloadImageAtIndex(currIndex, function () { | |
currIndex++; | |
setTimeout(function () { | |
if (numDownloaded < MAX_PHOTOS) { | |
setTimeout(function () { | |
recursiveGetImage(); | |
}, 200); | |
} else { | |
console.log('Done downloading! Downloaded', numDownloaded, 'images.') | |
} | |
}, 100); | |
}); | |
} | |
} | |
var script = document.createElement('script'); | |
script.src = 'https://code.jquery.com/jquery-3.1.1.js'; | |
script.type = 'text/javascript'; | |
document.getElementsByTagName('head')[0].appendChild(script); | |
script.onload = function () { | |
recursiveGetImage() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment