diff --git a/src/core.ts b/src/core.ts index 8317574..b510681 100644 --- a/src/core.ts +++ b/src/core.ts @@ -28,7 +28,7 @@ start(); async function debugStart() { browser = await launch({ headless: false }); - console.log(await crawlProductStock("https://www.macrovet.nl/K-Othrine-7-5-SC-1-liter-Insectenbestrijdingsmiddel/202561")); + console.log(await crawlProductStock("https://www.hondenbed.nl/hondenkussen-taupe-bruin.html?utm_medium=affiliate&utm_source=tradetracker")); } async function start() { diff --git a/src/modules/websites.ts b/src/modules/websites.ts index 367759a..cd6beb7 100644 --- a/src/modules/websites.ts +++ b/src/modules/websites.ts @@ -17,38 +17,41 @@ export namespace Template { export namespace LDJsonParser { export async function check(html: string) { try { + const snippets: Array = []; const $ = cheerio.load(html); - let stock = false; - $('script[type="application/ld+json"]').each((index, element) => { - let json = JSON.parse($(element).html()); - - if (json["@type"]) { - if (json["@type"].includes("Product")) { - if (Array.isArray(json["offers"])) { - let anyStock = false; - - for (let index in json["offers"]) { - if (json["offers"][index]["availability"].includes("InStock")) { - anyStock = true; - } - } + snippets.push($(element).html()); + }); - if (anyStock) { - stock = true; - return; - } - } else { - if (json["offers"]["availability"].includes("InStock")) { - stock = true; - return; + console.log("Found " + snippets.length + " snippets"); + + for (const snippet of snippets) { + try { + let json = JSON.parse(snippet); + + console.log(json); + + if (json["@type"]) { + if (json["@type"].includes("Product")) { + if (Array.isArray(json["offers"])) { + for (let index in json["offers"]) { + if (json["offers"][index]["availability"].includes("InStock")) { + return true; + } + } + } else { + if (json["offers"]["availability"].includes("InStock")) { + return true; + } } } } + } catch (error) { + continue; } - }); + } - return stock; + return false; } catch (error) { console.log(error); console.error(`Error occured during stock check!`);