From 518a7cb2f5681d73d43e8e173058f47e72230217 Mon Sep 17 00:00:00 2001 From: Nulo Date: Mon, 1 Jan 2024 16:49:43 -0300 Subject: [PATCH] carrefour: solo intentar parsear jsonld en error --- scraper/parsers/carrefour.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scraper/parsers/carrefour.ts b/scraper/parsers/carrefour.ts index aa32e11..4f6aa83 100644 --- a/scraper/parsers/carrefour.ts +++ b/scraper/parsers/carrefour.ts @@ -1,6 +1,6 @@ import { parseHTML } from "linkedom"; import { Precioish, type Precio } from "../scrap.js"; -import { getProductJsonLd, priceFromMeta } from "../common.js"; +import { getProductJsonLd, priceFromMeta, stockFromMeta } from "../common.js"; function getEanByTable(dom: Window): string { const eanLabelEl = dom.window.document.querySelector( @@ -54,14 +54,22 @@ export function getCarrefourProduct(html: string | Buffer): Precioish { const dom = parseHTML(html); const precioCentavos = priceFromMeta(dom); + const inStock = stockFromMeta(dom); const ean = eanFromSeedState(dom); - const ld = getProductJsonLd(dom); - const name = ld.name; - const imageUrl = ld.image; - const inStock = - ld.offers.offers[0].availability === "http://schema.org/InStock"; + let name, imageUrl; + try { + const ld = getProductJsonLd(dom); + name = ld.name; + imageUrl = ld.image; + } catch (error) { + if (inStock) { + throw error; + } else { + // algunas paginas sin stock no tienen json ld + } + } return { name,