Last active
December 23, 2023 04:00
-
-
Save thanhlongzzz/20563b9aa12a0974bc5ecf352665853d to your computer and use it in GitHub Desktop.
printerest downloader script
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
//login printerest => search a keyword => F12 => paste this script to console | |
function getLargestUrl(srcset) { | |
const urls = srcset.split(', '); | |
// Extract width and URL for each entry | |
const entries = urls.map(entry => { | |
const [url, widthString] = entry.split(' '); | |
const width = parseInt(widthString, 10); | |
return { | |
url, | |
width | |
}; | |
}); | |
const filteredEntries = entries.filter(item => { | |
if (item.url.includes('originals')){ | |
return true; | |
} | |
return false; | |
}); | |
return filteredEntries[0].url; | |
} | |
function downloadImage(url) { | |
if (url) { | |
fetch(url) | |
.then(response => response.blob()) | |
.then(blob => { | |
const link = document.createElement('a'); | |
const fileName = url.split('/').pop().split('?')[0]; | |
if (fileName.includes('.htm')) { | |
console.log('error: limit reached ' + url) | |
return; | |
} | |
link.href = window.URL.createObjectURL(blob); | |
link.download = fileName; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
}) | |
.catch(error => console.error(`Error downloading image: ${error}`)); | |
} | |
} | |
let listUrl = new Map(); | |
function delay(milliseconds) { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)); | |
} | |
async function main(){ | |
for(let i=0;i<10;i++){ | |
window.scrollBy(0, 500); | |
let img = document.querySelectorAll('div[data-test-id="non-story-pin-image"] img') | |
img.forEach(item =>{ | |
let srcset = item.getAttribute('srcset'); | |
let originUrl = getLargestUrl(srcset); | |
listUrl.set(originUrl,true); | |
//console.log(originUrl) | |
}) | |
await delay(500); | |
} | |
for (const key of listUrl.keys()) { | |
console.log('downloading: '+key); | |
downloadImage(key); | |
await delay(1000); | |
} | |
//console.log(listUrl) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment