2026-01-20 13:58:13 -05:00
|
|
|
import axios from 'axios';
|
2026-01-20 14:09:10 -05:00
|
|
|
import { load, type CheerioAPI } from 'cheerio';
|
2026-01-20 13:58:13 -05:00
|
|
|
import {
|
|
|
|
|
parsePrice,
|
|
|
|
|
ParsedPrice,
|
|
|
|
|
findMostLikelyPrice,
|
|
|
|
|
} from '../utils/priceParser';
|
|
|
|
|
|
|
|
|
|
export interface ScrapedProduct {
|
|
|
|
|
name: string | null;
|
|
|
|
|
price: ParsedPrice | null;
|
|
|
|
|
imageUrl: string | null;
|
|
|
|
|
url: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
// Site-specific scraper configurations
|
|
|
|
|
interface SiteScraper {
|
|
|
|
|
match: (url: string) => boolean;
|
|
|
|
|
scrape: ($: CheerioAPI, url: string) => Partial<ScrapedProduct>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const siteScrapers: SiteScraper[] = [
|
|
|
|
|
// Amazon
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /amazon\.(com|co\.uk|ca|de|fr|es|it|co\.jp|in|com\.au)/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
2026-01-20 20:46:17 -05:00
|
|
|
// Helper to check if element is inside a coupon/savings container
|
|
|
|
|
const isInCouponContainer = (el: ReturnType<typeof $>) => {
|
|
|
|
|
const parents = el.parents().toArray();
|
|
|
|
|
for (const parent of parents) {
|
|
|
|
|
const id = $(parent).attr('id') || '';
|
|
|
|
|
const className = $(parent).attr('class') || '';
|
|
|
|
|
const text = $(parent).text().toLowerCase();
|
|
|
|
|
if (/coupon|savings|save\s*\$|clipcoupon|promoprice/i.test(id + className)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Check if the immediate container mentions "save" or "coupon"
|
|
|
|
|
if (text.includes('save $') || text.includes('coupon') || text.includes('clip')) {
|
|
|
|
|
// Only consider it a coupon if it's a small container
|
|
|
|
|
if (text.length < 100) return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Try to get the main displayed price from specific containers first
|
|
|
|
|
// These are the primary price display areas on Amazon
|
|
|
|
|
const primaryPriceContainers = [
|
|
|
|
|
'#corePrice_feature_div',
|
|
|
|
|
'#corePriceDisplay_desktop_feature_div',
|
|
|
|
|
'#apex_desktop_newAccordionRow',
|
|
|
|
|
'#apex_offerDisplay_desktop',
|
2026-01-20 19:24:40 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
2026-01-20 20:46:17 -05:00
|
|
|
|
|
|
|
|
// First, try the primary price containers
|
|
|
|
|
for (const containerId of primaryPriceContainers) {
|
|
|
|
|
const container = $(containerId);
|
|
|
|
|
if (!container.length) continue;
|
|
|
|
|
|
|
|
|
|
// Look for the main price display (not savings/coupons)
|
|
|
|
|
const priceElements = container.find('.a-price .a-offscreen');
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < priceElements.length; i++) {
|
|
|
|
|
const el = $(priceElements[i]);
|
|
|
|
|
|
|
|
|
|
// Skip if this is inside a coupon container
|
|
|
|
|
if (isInCouponContainer(el)) continue;
|
|
|
|
|
|
|
|
|
|
// Skip if the parent has "savings" or similar class
|
|
|
|
|
const parentClass = el.parent().attr('class') || '';
|
|
|
|
|
if (/savings|coupon|save/i.test(parentClass)) continue;
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
const text = el.text().trim();
|
2026-01-20 20:46:17 -05:00
|
|
|
const parsed = parsePrice(text);
|
|
|
|
|
|
|
|
|
|
// Validate the price is reasonable (not a $1 coupon)
|
|
|
|
|
if (parsed && parsed.price >= 2) {
|
|
|
|
|
price = parsed;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback: try other known price selectors
|
|
|
|
|
if (!price) {
|
|
|
|
|
const fallbackSelectors = [
|
|
|
|
|
'#priceblock_dealprice',
|
|
|
|
|
'#priceblock_saleprice',
|
|
|
|
|
'#priceblock_ourprice',
|
|
|
|
|
'#price_inside_buybox',
|
|
|
|
|
'#newBuyBoxPrice',
|
|
|
|
|
'span[data-a-color="price"] .a-offscreen',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const selector of fallbackSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length && !isInCouponContainer(el)) {
|
|
|
|
|
const text = el.text().trim();
|
|
|
|
|
const parsed = parsePrice(text);
|
|
|
|
|
if (parsed && parsed.price >= 2) {
|
|
|
|
|
price = parsed;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Last resort: look for the whole/fraction price format
|
|
|
|
|
if (!price) {
|
|
|
|
|
const whole = $('#corePrice_feature_div .a-price-whole').first().text().replace(',', '');
|
|
|
|
|
const fraction = $('#corePrice_feature_div .a-price-fraction').first().text();
|
|
|
|
|
if (whole) {
|
|
|
|
|
const priceStr = `$${whole}${fraction ? '.' + fraction : ''}`;
|
|
|
|
|
const parsed = parsePrice(priceStr);
|
|
|
|
|
if (parsed && parsed.price >= 2) {
|
|
|
|
|
price = parsed;
|
|
|
|
|
}
|
2026-01-20 19:24:40 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Product name
|
|
|
|
|
const name = $('#productTitle').text().trim() ||
|
|
|
|
|
$('h1.a-size-large').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
// Image
|
|
|
|
|
const imageUrl = $('#landingImage').attr('src') ||
|
|
|
|
|
$('#imgBlkFront').attr('src') ||
|
|
|
|
|
$('img[data-a-dynamic-image]').attr('src') ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Walmart
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /walmart\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
// Walmart uses various price containers
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'[data-testid="price-wrap"] [itemprop="price"]',
|
|
|
|
|
'[itemprop="price"]',
|
|
|
|
|
'.price-characteristic',
|
|
|
|
|
'[data-automation="product-price"]',
|
|
|
|
|
'.prod-PriceHero .price-group',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
const content = el.attr('content');
|
|
|
|
|
const text = content || el.text().trim();
|
|
|
|
|
price = parsePrice(text);
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also try to get price from the whole dollars + cents pattern
|
|
|
|
|
if (!price) {
|
|
|
|
|
const dollars = $('[data-testid="price-wrap"] .f2').text().trim();
|
|
|
|
|
const cents = $('[data-testid="price-wrap"] .f6').text().trim();
|
|
|
|
|
if (dollars) {
|
|
|
|
|
price = parsePrice(`$${dollars}${cents ? '.' + cents : ''}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('h1[itemprop="name"]').text().trim() ||
|
|
|
|
|
$('h1.prod-ProductTitle').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('[data-testid="hero-image-container"] img').attr('src') ||
|
|
|
|
|
$('img.prod-hero-image').attr('src') ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Best Buy
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /bestbuy\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'[data-testid="customer-price"] span',
|
|
|
|
|
'.priceView-customer-price span',
|
|
|
|
|
'.priceView-hero-price span',
|
|
|
|
|
'[class*="customerPrice"]',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
price = parsePrice(el.text().trim());
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('h1.heading-5').text().trim() ||
|
|
|
|
|
$('.sku-title h1').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('img.primary-image').attr('src') ||
|
|
|
|
|
$('[data-testid="image-gallery-image"]').attr('src') ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Target
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /target\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'[data-test="product-price"]',
|
|
|
|
|
'[data-test="current-price"]',
|
|
|
|
|
'.styles__CurrentPriceFontSize-sc-1qc6t3e-1',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
price = parsePrice(el.text().trim());
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('[data-test="product-title"]').text().trim() ||
|
|
|
|
|
$('h1[class*="Heading"]').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('[data-test="image-gallery-item-0"] img').attr('src') ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// eBay
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /ebay\.(com|co\.uk|de|fr|ca|com\.au)/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'[data-testid="x-price-primary"] .ux-textspans',
|
|
|
|
|
'.x-price-primary .ux-textspans',
|
|
|
|
|
'#prcIsum',
|
|
|
|
|
'#mm-saleDscPrc',
|
|
|
|
|
'.vi-price .notranslate',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
price = parsePrice(el.text().trim());
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('h1.x-item-title__mainTitle span').text().trim() ||
|
|
|
|
|
$('h1[itemprop="name"]').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('[data-testid="ux-image-carousel"] img').attr('src') ||
|
|
|
|
|
$('#icImg').attr('src') ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Newegg
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /newegg\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const price = parsePrice($('.price-current').text().trim()) ||
|
|
|
|
|
parsePrice($('[itemprop="price"]').attr('content') || '');
|
|
|
|
|
|
|
|
|
|
const name = $('h1.product-title').text().trim() || null;
|
|
|
|
|
const imageUrl = $('img.product-view-img-original').attr('src') || null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Home Depot
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /homedepot\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'[data-testid="price-format"] span',
|
|
|
|
|
'.price-format__main-price span',
|
|
|
|
|
'#ajaxPrice',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
price = parsePrice(el.text().trim());
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('h1.product-title__title').text().trim() ||
|
|
|
|
|
$('h1[class*="product-details"]').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('img[data-testid="media-gallery-image"]').attr('src') || null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Costco
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /costco\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const price = parsePrice($('[automation-id="productPriceOutput"]').text().trim()) ||
|
|
|
|
|
parsePrice($('.price').first().text().trim());
|
|
|
|
|
|
|
|
|
|
const name = $('h1[itemprop="name"]').text().trim() ||
|
|
|
|
|
$('h1.product-title').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('img.product-image').attr('src') || null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// AliExpress
|
|
|
|
|
{
|
|
|
|
|
match: (url) => /aliexpress\.com/i.test(url),
|
|
|
|
|
scrape: ($) => {
|
|
|
|
|
const priceSelectors = [
|
|
|
|
|
'.product-price-value',
|
|
|
|
|
'[class*="uniformBannerBoxPrice"]',
|
|
|
|
|
'.snow-price_SnowPrice__mainS__1occeh',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let price: ParsedPrice | null = null;
|
|
|
|
|
for (const selector of priceSelectors) {
|
|
|
|
|
const el = $(selector).first();
|
|
|
|
|
if (el.length) {
|
|
|
|
|
price = parsePrice(el.text().trim());
|
|
|
|
|
if (price) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = $('h1[data-pl="product-title"]').text().trim() ||
|
|
|
|
|
$('h1.product-title-text').text().trim() ||
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
const imageUrl = $('img.magnifier-image').attr('src') || null;
|
|
|
|
|
|
|
|
|
|
return { name, price, imageUrl };
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Generic selectors as fallback
|
|
|
|
|
const genericPriceSelectors = [
|
2026-01-20 13:58:13 -05:00
|
|
|
'[itemprop="price"]',
|
|
|
|
|
'[data-price]',
|
|
|
|
|
'[data-product-price]',
|
|
|
|
|
'.price',
|
|
|
|
|
'.product-price',
|
|
|
|
|
'.current-price',
|
|
|
|
|
'.sale-price',
|
|
|
|
|
'.final-price',
|
|
|
|
|
'.offer-price',
|
|
|
|
|
'#price',
|
2026-01-20 19:24:40 -05:00
|
|
|
'[class*="price" i]',
|
|
|
|
|
'[class*="Price" i]',
|
2026-01-20 13:58:13 -05:00
|
|
|
];
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
const genericNameSelectors = [
|
2026-01-20 13:58:13 -05:00
|
|
|
'[itemprop="name"]',
|
|
|
|
|
'h1[class*="product"]',
|
|
|
|
|
'h1[class*="title"]',
|
|
|
|
|
'.product-title',
|
|
|
|
|
'.product-name',
|
|
|
|
|
'h1',
|
|
|
|
|
];
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
const genericImageSelectors = [
|
2026-01-20 13:58:13 -05:00
|
|
|
'[itemprop="image"]',
|
|
|
|
|
'[property="og:image"]',
|
|
|
|
|
'.product-image img',
|
|
|
|
|
'.main-image img',
|
|
|
|
|
'[data-zoom-image]',
|
|
|
|
|
'img[class*="product"]',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export async function scrapeProduct(url: string): Promise<ScrapedProduct> {
|
|
|
|
|
const result: ScrapedProduct = {
|
|
|
|
|
name: null,
|
|
|
|
|
price: null,
|
|
|
|
|
imageUrl: null,
|
|
|
|
|
url,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-20 14:09:10 -05:00
|
|
|
const response = await axios.get<string>(url, {
|
2026-01-20 13:58:13 -05:00
|
|
|
headers: {
|
|
|
|
|
'User-Agent':
|
2026-01-20 19:24:40 -05:00
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
|
2026-01-20 13:58:13 -05:00
|
|
|
Accept:
|
2026-01-20 19:24:40 -05:00
|
|
|
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
|
|
|
|
'Accept-Language': 'en-US,en;q=0.9',
|
2026-01-20 13:58:13 -05:00
|
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
2026-01-20 19:24:40 -05:00
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
Pragma: 'no-cache',
|
|
|
|
|
'Sec-Ch-Ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
|
|
|
|
|
'Sec-Ch-Ua-Mobile': '?0',
|
|
|
|
|
'Sec-Ch-Ua-Platform': '"Windows"',
|
|
|
|
|
'Sec-Fetch-Dest': 'document',
|
|
|
|
|
'Sec-Fetch-Mode': 'navigate',
|
|
|
|
|
'Sec-Fetch-Site': 'none',
|
|
|
|
|
'Sec-Fetch-User': '?1',
|
2026-01-20 13:58:13 -05:00
|
|
|
'Upgrade-Insecure-Requests': '1',
|
|
|
|
|
},
|
2026-01-20 19:24:40 -05:00
|
|
|
timeout: 20000,
|
2026-01-20 13:58:13 -05:00
|
|
|
maxRedirects: 5,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 14:09:10 -05:00
|
|
|
const $ = load(response.data);
|
2026-01-20 13:58:13 -05:00
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
// Try site-specific scraper first
|
|
|
|
|
const siteScraper = siteScrapers.find((s) => s.match(url));
|
|
|
|
|
if (siteScraper) {
|
|
|
|
|
const siteResult = siteScraper.scrape($, url);
|
|
|
|
|
if (siteResult.name) result.name = siteResult.name;
|
|
|
|
|
if (siteResult.price) result.price = siteResult.price;
|
|
|
|
|
if (siteResult.imageUrl) result.imageUrl = siteResult.imageUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try JSON-LD structured data
|
|
|
|
|
if (!result.price || !result.name) {
|
|
|
|
|
const jsonLdData = extractJsonLd($);
|
|
|
|
|
if (jsonLdData) {
|
|
|
|
|
if (!result.name && jsonLdData.name) result.name = jsonLdData.name;
|
|
|
|
|
if (!result.price && jsonLdData.price) result.price = jsonLdData.price;
|
|
|
|
|
if (!result.imageUrl && jsonLdData.image) result.imageUrl = jsonLdData.image;
|
|
|
|
|
}
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
// Fallback to generic scraping
|
2026-01-20 13:58:13 -05:00
|
|
|
if (!result.name) {
|
2026-01-20 19:24:40 -05:00
|
|
|
result.name = extractGenericName($);
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.price) {
|
2026-01-20 19:24:40 -05:00
|
|
|
result.price = extractGenericPrice($);
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.imageUrl) {
|
2026-01-20 19:24:40 -05:00
|
|
|
result.imageUrl = extractGenericImage($, url);
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
// Try Open Graph meta tags as last resort
|
2026-01-20 13:58:13 -05:00
|
|
|
if (!result.name) {
|
|
|
|
|
result.name = $('meta[property="og:title"]').attr('content') || null;
|
|
|
|
|
}
|
|
|
|
|
if (!result.imageUrl) {
|
|
|
|
|
result.imageUrl = $('meta[property="og:image"]').attr('content') || null;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`Error scraping ${url}:`, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 14:09:10 -05:00
|
|
|
interface JsonLdProduct {
|
|
|
|
|
'@type'?: string;
|
|
|
|
|
'@graph'?: JsonLdProduct[];
|
|
|
|
|
name?: string;
|
|
|
|
|
image?: string | string[] | { url?: string };
|
|
|
|
|
offers?: JsonLdOffer | JsonLdOffer[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface JsonLdOffer {
|
2026-01-20 19:24:40 -05:00
|
|
|
'@type'?: string;
|
2026-01-20 14:09:10 -05:00
|
|
|
price?: string | number;
|
|
|
|
|
priceCurrency?: string;
|
2026-01-20 19:24:40 -05:00
|
|
|
lowPrice?: string | number;
|
2026-01-20 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 13:58:13 -05:00
|
|
|
function extractJsonLd(
|
2026-01-20 14:09:10 -05:00
|
|
|
$: CheerioAPI
|
2026-01-20 13:58:13 -05:00
|
|
|
): { name?: string; price?: ParsedPrice; image?: string } | null {
|
|
|
|
|
try {
|
|
|
|
|
const scripts = $('script[type="application/ld+json"]');
|
|
|
|
|
for (let i = 0; i < scripts.length; i++) {
|
|
|
|
|
const content = $(scripts[i]).html();
|
|
|
|
|
if (!content) continue;
|
|
|
|
|
|
2026-01-20 14:09:10 -05:00
|
|
|
const data = JSON.parse(content) as JsonLdProduct | JsonLdProduct[];
|
2026-01-20 13:58:13 -05:00
|
|
|
const product = findProduct(data);
|
|
|
|
|
|
|
|
|
|
if (product) {
|
2026-01-20 19:24:40 -05:00
|
|
|
const result: { name?: string; price?: ParsedPrice; image?: string } = {};
|
2026-01-20 13:58:13 -05:00
|
|
|
|
|
|
|
|
if (product.name) {
|
|
|
|
|
result.name = product.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (product.offers) {
|
|
|
|
|
const offer = Array.isArray(product.offers)
|
|
|
|
|
? product.offers[0]
|
|
|
|
|
: product.offers;
|
2026-01-20 19:24:40 -05:00
|
|
|
|
|
|
|
|
// Get price, preferring lowPrice for ranges
|
|
|
|
|
const priceValue = offer.lowPrice || offer.price;
|
|
|
|
|
if (priceValue) {
|
2026-01-20 13:58:13 -05:00
|
|
|
result.price = {
|
2026-01-20 19:24:40 -05:00
|
|
|
price: parseFloat(String(priceValue)),
|
2026-01-20 13:58:13 -05:00
|
|
|
currency: offer.priceCurrency || 'USD',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (product.image) {
|
2026-01-20 14:09:10 -05:00
|
|
|
if (Array.isArray(product.image)) {
|
|
|
|
|
result.image = product.image[0];
|
|
|
|
|
} else if (typeof product.image === 'string') {
|
|
|
|
|
result.image = product.image;
|
|
|
|
|
} else if (product.image.url) {
|
|
|
|
|
result.image = product.image.url;
|
|
|
|
|
}
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-20 14:09:10 -05:00
|
|
|
} catch (_e) {
|
2026-01-20 13:58:13 -05:00
|
|
|
// JSON parse error, continue with other methods
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 14:09:10 -05:00
|
|
|
function findProduct(data: JsonLdProduct | JsonLdProduct[]): JsonLdProduct | null {
|
|
|
|
|
if (!data) return null;
|
2026-01-20 13:58:13 -05:00
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
|
for (const item of data) {
|
|
|
|
|
const found = findProduct(item);
|
|
|
|
|
if (found) return found;
|
|
|
|
|
}
|
2026-01-20 14:09:10 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data['@type'] === 'Product') {
|
|
|
|
|
return data;
|
2026-01-20 13:58:13 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 14:09:10 -05:00
|
|
|
if (data['@graph'] && Array.isArray(data['@graph'])) {
|
|
|
|
|
for (const item of data['@graph']) {
|
2026-01-20 13:58:13 -05:00
|
|
|
const found = findProduct(item);
|
|
|
|
|
if (found) return found;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
function extractGenericPrice($: CheerioAPI): ParsedPrice | null {
|
2026-01-20 13:58:13 -05:00
|
|
|
const prices: ParsedPrice[] = [];
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
for (const selector of genericPriceSelectors) {
|
2026-01-20 13:58:13 -05:00
|
|
|
const elements = $(selector);
|
|
|
|
|
elements.each((_, el) => {
|
2026-01-20 19:24:40 -05:00
|
|
|
const $el = $(el);
|
|
|
|
|
// Skip if this looks like an "original" or "was" price
|
|
|
|
|
const classAttr = $el.attr('class') || '';
|
|
|
|
|
const parentClass = $el.parent().attr('class') || '';
|
|
|
|
|
if (/original|was|old|regular|compare|strikethrough|line-through/i.test(classAttr + parentClass)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const text = $el.attr('content') || $el.attr('data-price') || $el.text();
|
2026-01-20 13:58:13 -05:00
|
|
|
const parsed = parsePrice(text);
|
2026-01-20 19:24:40 -05:00
|
|
|
if (parsed && parsed.price > 0) {
|
2026-01-20 13:58:13 -05:00
|
|
|
prices.push(parsed);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (prices.length > 0) break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return findMostLikelyPrice(prices);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
function extractGenericName($: CheerioAPI): string | null {
|
|
|
|
|
for (const selector of genericNameSelectors) {
|
2026-01-20 13:58:13 -05:00
|
|
|
const element = $(selector).first();
|
|
|
|
|
if (element.length) {
|
|
|
|
|
const text = element.text().trim();
|
|
|
|
|
if (text && text.length > 0 && text.length < 500) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:24:40 -05:00
|
|
|
function extractGenericImage($: CheerioAPI, baseUrl: string): string | null {
|
|
|
|
|
for (const selector of genericImageSelectors) {
|
2026-01-20 13:58:13 -05:00
|
|
|
const element = $(selector).first();
|
|
|
|
|
if (element.length) {
|
|
|
|
|
const src =
|
|
|
|
|
element.attr('src') ||
|
|
|
|
|
element.attr('content') ||
|
|
|
|
|
element.attr('data-zoom-image') ||
|
|
|
|
|
element.attr('data-src');
|
|
|
|
|
if (src) {
|
|
|
|
|
try {
|
|
|
|
|
return new URL(src, baseUrl).href;
|
2026-01-20 14:09:10 -05:00
|
|
|
} catch (_e) {
|
2026-01-20 13:58:13 -05:00
|
|
|
return src;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function scrapePrice(url: string): Promise<ParsedPrice | null> {
|
|
|
|
|
const product = await scrapeProduct(url);
|
|
|
|
|
return product.price;
|
|
|
|
|
}
|