// Name: Download Blobs from Website // Description: Downloads blob images from a specified website and saves them as PNG files. import '@johnlindquist/kit' async function downloadBlobs() { // Prompt the user to enter the website URL const url = await arg('Enter website URL to scrape blobs from:'); // Define the CSS selector to target the div elements containing blob images. // Updated selector to target divs with class 'img highres' and extract backgroundImage style const imageDivs = await scrapeSelector( url, 'div.img.highres', (element) => { const style = element.getAttribute('style'); const match = style?.match(/background-image: url\("([^"]*)"\)/); return match ? match[1] : null; // Extract blob URL from background-image style } ); // Filter out null or undefined URLs and non-blob URLs const blobUrls = imageDivs.filter(url => url && url.startsWith('blob:')) as string[]; if (blobUrls.length === 0) { await div(md('No blob images found on the page.')); return; // Exit the script if no blob images are found } // Define the directory to save the downloaded images const photosDir = home('photos', 'midsummer'); await ensureDir(photosDir); // Ensure the directory exists // Use Promise.all for efficient concurrent downloading of blobs await Promise.all(blobUrls.map(async (blobUrl, index) => { const fileName = `image-${index + 1}.png`; // Create a filename for each image const filePath = path.join(photosDir, fileName); // Construct the full file path try { // Download the blob as a buffer const buffer = await download(blobUrl); // Write the buffer to a PNG file await writeFile(filePath, buffer); console.log(`Downloaded: ${fileName}`); // Log successful download } catch (error) { // Handle download errors for individual images console.error(`Error downloading image ${fileName} from ${blobUrl}:`, error); } })); // Notify the user that the download is complete await div(md(`# Downloaded ${blobUrls.length} blobs to ${photosDir}`)); } // Execute the main function downloadBlobs();