Fix image gallery: scrape real product image URLs from JV website

This commit is contained in:
Omair Saleh
2026-04-28 18:13:56 +08:00
parent 4abe884e0c
commit fc84c8f263
2 changed files with 37 additions and 12 deletions
+29
View File
@@ -52,6 +52,28 @@ async function serveStatic(pathname: string): Promise<Response | null> {
return null
}
// In-memory cache for scraped image URLs per SKU
const imageCache: Record<string, string[]> = {}
async function scrapeProductImages(sku: string): Promise<string[]> {
if (imageCache[sku]) return imageCache[sku]
const images = await readJson(join(PIPELINE_ROOT, 'product-images.json')) as Record<string, any>
const productPath = images?.[sku]?.url
if (!productPath) return []
try {
const res = await fetch(`https://www.justvitamins.co.uk${productPath}`, {
headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' }
})
const html = await res.text()
// Extract filenames like {uuid}-{n}.jpg from anywhere in the page
const matches = [...html.matchAll(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-\d+)\.jpg/g)]
const filenames = [...new Set(matches.map(m => m[1]))]
const urls = filenames.map(f => `https://images.justvitamins.co.uk/product-images/Normal/${f}.jpg`)
imageCache[sku] = urls
return urls
} catch { return [] }
}
const server = Bun.serve({
port: PORT,
async fetch(req) {
@@ -71,6 +93,13 @@ const server = Bun.serve({
return new Response(JSON.stringify(resolved), { headers: cors })
}
// GET /api/product-images/:sku
const imgMatch = path.match(/^\/api\/product-images\/([^/]+)$/)
if (imgMatch) {
const urls = await scrapeProductImages(imgMatch[1])
return new Response(JSON.stringify({ urls }), { headers: cors })
}
// GET /api/intelligence/:sku/:view
const intelMatch = path.match(/^\/api\/intelligence\/([^/]+)\/([^/]+)$/)
if (intelMatch) {