Last active
June 11, 2022 14:36
-
-
Save H3mul/1431ccfe5d235d85610b70424cf45b55 to your computer and use it in GitHub Desktop.
Pinterest Image Downloader: drop into developer console on Pinterest page and run 'download_pins("<search string>")', where <search string> is a placeholder for an optional pin title filter. The script will pull all the pin images fitting the search filter using jQuery, zip using JSZip and offer download using FileSaver.js.
This file contains hidden or 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
//Pinterest Image Downloader | |
//USAGE: | |
//drop script into developer console on Pinterest page | |
//enter download_pins("<search string>") | |
//where <search string> is a placeholder for an optional pin title filter. | |
//The script will pull all the pin images fitting the search filter using jQuery, | |
//zip them all using JSZip and offer download using FileSaver.js. | |
download_pins = function(search_string) { | |
var main = function($){ | |
console.log('Fetching Pins...') | |
//settings: | |
var TITLE_FILTER = search_string; | |
var MAX_PINS = 1000; | |
var SCROLL_THRESHOLD = 1; | |
var TIMEOUT_STEP = 2000; | |
//globals | |
var zip = new JSZip(); | |
var zipfilecount = 0; | |
var pincount = 0; | |
//functions: | |
var funcs = {}; | |
funcs.scrollToBottom = function(){ | |
var pins = funcs.getPinDivs(); | |
bottom = document.body.scrollHeight; | |
current = window.innerHeight+ document.documentElement.scrollTop + SCROLL_THRESHOLD; | |
if((bottom-current) > 0 && pins.length < MAX_PINS){ | |
window.scrollTo(0, bottom); | |
setTimeout(funcs.scrollToBottom, TIMEOUT_STEP); | |
} | |
else{ | |
funcs.readPins(); | |
} | |
}; | |
funcs.getContainer = function(){ | |
return $('div.mainContainer'); | |
} | |
funcs.getPinDivs = function(){ | |
return funcs.getContainer().find('div[data-grid-item]'); | |
}; | |
funcs.readPins = function(){ | |
funcs.getPinDivs().each(function(){ | |
var pinurl = $(this).find('[href]').attr('href'); | |
var pintitle = $(this).find('div.pinWrapper span span').first().text(); | |
//debugger; | |
if(pintitle.match(TITLE_FILTER) && pinurl.match(/^\/pin\//)){ | |
pincount++; | |
funcs.savePinImage(pinurl); | |
} | |
if(pincount >= MAX_PINS){ | |
return false; | |
} | |
}); | |
} | |
funcs.savePinImage = function(pinurl){ | |
$.get(pinurl, function( data ) { | |
var pinpage = $(data.module.html); | |
pinpage.find('div.closeupCenterImage img[src]').each(function(){ | |
var imgurl = $(this).attr('src'); | |
if(imgurl.match(/i\.pinimg\.com\/564x/)){ | |
funcs.saveImage(imgurl); | |
return false; | |
} | |
}); | |
}); | |
} | |
funcs.saveImage = function(url){ | |
var filename = url.substr(url.lastIndexOf('/') + 1); | |
console.log(url); | |
funcs.convertFileToDataURLviaFileReader(url, function(data){ | |
data = data.substr(data.lastIndexOf(',') + 1); | |
zip.file(filename, data, {base64 : true}); | |
zipfilecount++; | |
if(pincount == zipfilecount){ | |
funcs.saveZip(); | |
} | |
}); | |
} | |
funcs.convertFileToDataURLviaFileReader =function(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function() { | |
var reader = new FileReader(); | |
reader.onloadend = function() { | |
callback(reader.result); | |
} | |
reader.readAsDataURL(xhr.response); | |
}; | |
xhr.open('GET', url); | |
xhr.responseType = 'blob'; | |
xhr.send(); | |
} | |
funcs.saveZip = function(){ | |
console.log(zipfilecount + " Pins found."); | |
console.log("Zipping and downloading ..."); | |
zip.generateAsync({type:"blob"}).then(function (blob) { // 1) generate the zip file | |
saveAs(blob, "pins.zip"); // 2) trigger the download | |
}); | |
} | |
funcs.scrollToBottom(); | |
} | |
//Script injection | |
var s = ['//code.jquery.com/jquery-latest.min.js', '//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js', '//fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js'] | |
var scriptsloaded = 0; | |
for(var i = 0; i<s.length; i++){ | |
var e = document.createElement('script'); | |
e.src = s[i]; | |
e.i = i | |
e.onload = function() { | |
scriptsloaded++; | |
console.log(this.src + ' injected'); | |
if(scriptsloaded == s.length){ | |
main(jQuery); | |
} | |
}; | |
document.head.appendChild(e); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment