// // Price is in pence (integer), divide by 100 = GBP const articleRegex = /data-name="([^"]{10,200})"[^>]*data-product-price="\s*(\d+)\s*"[^>]*>[\s\S]{0,500}?href="(https?:\/\/(?:www\.)?gbics\.com\/[^"]+)"/gi; let match; while ((match = articleRegex.exec(collapsed)) !== null) { const name = match[1].trim(); const priceRaw = parseInt(match[2], 10); const url = match[3]; // GBICS stores price in pence (integer) — e.g. 2395 = £23.95 OR £2,395.00 (full pounds)? // Check by data-price-asc context: "data-price-asc=\"2395\"" with "£2,395.00" → price is in full GBP (no pence) const price = priceRaw > 0 ? priceRaw : undefined; if (name.length < 10) continue; const reach = detectReach(name); const partParts = name.split(/\s+-\s+/); const partNumber = partParts[0]?.trim().slice(0, 80) || url.split("/").filter(Boolean).pop() || ""; products.push({ partNumber, name, url, price: price && price > 0 && price < 50000 ? price : undefined, formFactor: cat.formFactor, speed: cat.speed, speedGbps: cat.speedGbps, reachLabel: reach?.label, reachMeters: reach?.meters, fiberType: detectFiber(name), wavelength: detectWavelength(name), compatibleWith: extractCompatibleVendor(name), }); } // Fallback: aria-label pattern if (products.length === 0) { const ariaRegex = /aria-label="([^"]+£[^"]+)"\s+href="(https?:\/\/(?:www\.)?gbics\.com\/[^"]+)"/gi; while ((match = ariaRegex.exec(collapsed)) !== null) { const label = match[1].trim(); const url = match[2]; const priceMatch = label.match(/£\s*([\d,.]+)/); const name = label.split(",")[0]?.trim() || label; const price = priceMatch ? parseFloat(priceMatch[1].replace(",", "")) : undefined; if (name.length < 10) continue; const reach = detectReach(name); products.push({ partNumber: name.split(/\s+-\s+/)[0]?.trim().slice(0, 80) || "", name, url, price: price && price > 0 && price < 50000 ? price : undefined, formFactor: cat.formFactor, speed: cat.speed, speedGbps: cat.speedGbps, reachLabel: reach?.label, reachMeters: reach?.meters, fiberType: detectFiber(name), wavelength: detectWavelength(name), compatibleWith: extractCompatibleVendor(name), }); } } // Fallback: try "Now: £XX.XX" pattern near product links if (products.length === 0) { const altRegex = /href="(https?:\/\/(?:www\.)?gbics\.com\/[^"]+)"[^>]*>\s*([^<]{15,})<\/a>/gi; while ((match = altRegex.exec(collapsed)) !== null) { const url = match[1]; const name = match[2].trim(); if (name.length < 10 || products.find((p) => p.url === url)) continue; if (!/transceiver|sfp|qsfp|xfp|osfp|base/i.test(name)) continue; const context = collapsed.slice(Math.max(0, match.index - 300), match.index + 600); const priceMatch = context.match(/Now:\s*£\s*([\d,.]+)/) || context.match(/£\s*([\d,.]+)/); const price = priceMatch ? parseFloat(priceMatch[1].replace(",", "")) : undefined; const reach = detectReach(name); products.push({ partNumber: name.split(/\s+-\s+/)[0]?.trim().slice(0, 80) || "", name, url, price: price && price > 0 && price < 50000 ? price : undefined, formFactor: cat.formFactor, speed: cat.speed, speedGbps: cat.speedGbps, reachLabel: reach?.label, reachMeters: reach?.meters, fiberType: detectFiber(name), wavelength: detectWavelength(name), compatibleWith: extractCompatibleVendor(name), }); } } const seen = new Set(); return products.filter((p) => { if (seen.has(p.url)) return false; seen.add(p.url); return true; }); } async function fetchPage(url: string): Promise { const resp = await fetch(url, { headers: HEADERS, signal: AbortSignal.timeout(30000) }); if (!resp.ok) throw new Error(`HTTP ${resp.status} for ${url}`); return resp.text(); } export async function scrapeGbics(): Promise { console.log("=== GBICS.com Scraper Starting ===\n"); const vendorId = await ensureVendor("GBICS", "compatible", "https://www.gbics.com", "https://www.gbics.com/optical-transceivers/"); let totalProducts = 0; let priceUpdates = 0; for (const cat of CATEGORIES) { console.log(`\n--- ${cat.formFactor} (${cat.speed}) [${cat.path}] ---`); try { const html = await fetchPage(BASE + cat.path); const catProducts = parseProductList(html, cat); console.log(` Found ${catProducts.length} products`); for (const product of catProducts) { try { const txId = await findOrCreateScrapedTransceiver({ partNumber: product.partNumber, vendorId, formFactor: product.formFactor, speedGbps: product.speedGbps, speed: product.speed, reachMeters: product.reachMeters, reachLabel: product.reachLabel, fiberType: product.fiberType, wavelengths: product.wavelength, category: "DataCenter", }); if (product.price && product.price > 0) { const hash = contentHash(JSON.stringify({ price: product.price, part: product.partNumber })); const updated = await upsertPriceObservation({ transceiverId: txId, sourceVendorId: vendorId, price: product.price, currency: "GBP", stockLevel: "in_stock", url: product.url, contentHash: hash, }); if (updated) priceUpdates++; } totalProducts++; } catch (err) { console.warn(` Error: ${(err as Error).message.slice(0, 80)}`); } } } catch (err) { console.error(` Category failed: ${(err as Error).message}`); } await sleep(2000); } console.log(`\n=== GBICS Complete: ${totalProducts} products, ${priceUpdates} prices ===`); } if (require.main === module) { scrapeGbics() .then(() => pool.end()) .catch((err) => { console.error("Fatal:", err); pool.end(); process.exit(1); }); }