Last active
July 10, 2024 15:47
-
-
Save STRML/bc6f400033207f503dce16ffedf06ea9 to your computer and use it in GitHub Desktop.
A simple script for downloading all images for a listing on BringATrailer.com
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
(async function() { | |
const delay = (ms) => new Promise(r => setTimeout(r, ms)); | |
// Given a url, try to download a high-res version of it | |
// This prevents us from downloading a downscaled thumbnail | |
function replaceUrlParam(url) { | |
return url | |
.split('?')[0] // get rid of querystring | |
.replace('-scaled.', '.'); // Remove `-scaled` to attempt to get the full res image | |
} | |
// Open the full gallery. Wait a bit for the JS to execute. | |
async function openFullGallery() { | |
const showBtn = document.querySelector('.gallery > a > .show'); | |
if (!showBtn) return; | |
showBtn.click(); | |
await delay(50); | |
return openFullGallery(); | |
} | |
// Open the full gallery. We try it a few times to be sure it takes | |
await openFullGallery(); | |
// Get the full list of gallery image urls. | |
const imageUrls = [...document.querySelectorAll('.gallery > a')] | |
.map(a => replaceUrlParam(a.href)); | |
// Create an anchor link to virtually click. | |
const a = document.createElement('a'); | |
a.download = ''; | |
// Now set each one to the downloadable image and download it. | |
for(const url of imageUrls) { | |
a.href = url; | |
a.click(); | |
await delay(200); | |
} | |
console.log('BaT image download complete!') | |
})().catch(console.error); |
Great collaboration - this helped me document a car I'm hoping to buy. Thanks, all.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good catch @handro123, fixed.