fix: harden scraper price parsing

This commit is contained in:
2026-04-23 10:31:08 -04:00
parent 807849e257
commit 244a88e63c
4 changed files with 77 additions and 33 deletions

View File

@@ -240,7 +240,6 @@ function parseEbayListings(
!text.includes("core") &&
!text.includes("ram") &&
!text.includes("ssd") &&
!/\d{4}/.test(text) && // Avoid years like "2024"
!text.includes('"') // Avoid measurements
) {
priceElement = el;

View File

@@ -890,30 +890,15 @@ export function parseFacebookAds(
: priceObj.amount;
cents = Math.round(dollars * 100);
} else if (priceObj.amount_with_offset_in_currency != null) {
// Fallback: try to extract cents from amount_with_offset_in_currency
// This appears to use some exchange rate/multiplier format
const encodedAmount = Number(priceObj.amount_with_offset_in_currency);
if (!Number.isNaN(encodedAmount) && encodedAmount > 0) {
// Estimate roughly - this field doesn't contain real cents
// Use formatted_amount to get the actual dollar amount
if (priceObj.formatted_amount) {
const match = priceObj.formatted_amount.match(/[\d,]+\.?\d*/);
if (match) {
const dollars = Number.parseFloat(match[0].replace(/,/g, ""));
if (!Number.isNaN(dollars)) {
cents = Math.round(dollars * 100);
} else {
cents = encodedAmount; // fallback
}
} else {
cents = encodedAmount; // fallback
}
} else {
cents = encodedAmount; // fallback
}
} else {
continue; // Invalid price
}
if (!priceObj.formatted_amount) continue;
const match = priceObj.formatted_amount.match(/[\d,]+\.?\d*/);
if (!match) continue;
const dollars = Number.parseFloat(match[0].replace(/,/g, ""));
if (Number.isNaN(dollars)) continue;
cents = Math.round(dollars * 100);
} else {
continue; // No price available
}
@@ -977,7 +962,9 @@ export function parseFacebookAds(
};
results.push(listingDetails);
} catch {}
} catch (error) {
console.warn("Failed to parse Facebook ad:", error);
}
}
return results;