-
-
Save STRML/bc6f400033207f503dce16ffedf06ea9 to your computer and use it in GitHub Desktop.
(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); |
Thank you both for the updates!
It looks like the issue when -scaled
is in the middle of the URL instead of just at the end is still present. This is the test listing that I used to check: https://bringatrailer.com/listing/1979-bmw-320i-23/
One new issue I am having is
.replace('-scaled', '');
. Some pictures in the BMW listing have-scaled
in the middle of the filename and those weren't getting downloaded as the URL would 404. Example: https://bringatrailer.com/wp-content/uploads/2023/05/1979_bmw_320i_Doc-May-18-2023-2-50-PM-p3-36336-scaled-1-84154.jpg?fit=1587%2C2048 would get turned into: https://bringatrailer.com/wp-content/uploads/2023/05/1979_bmw_320i_Doc-May-18-2023-2-50-PM-p3-36336-1-84154.jpgReplacing it with
.replace('-scaled.', '.');
would make sure it's only replaced at the end of the filename.
Good catch @handro123, fixed.
Great collaboration - this helped me document a car I'm hoping to buy. Thanks, all.
Nice one @JLWalsh! Showing the whole gallery rather than paging through the carousel is a nice tweak.
I've updated the source to use this mechanism. I made a few small tweaks, namely I continue downloading the original source file by removing
-scaled
from the URL. Additionally, I simply check if the gallery is open instead of waiting 1000ms.