This commit is contained in:
Nick Leeman 2023-06-21 16:48:31 +02:00
parent 3438daaa80
commit 224dd161b0
2 changed files with 28 additions and 25 deletions

View File

@ -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() {

View File

@ -17,38 +17,41 @@ export namespace Template {
export namespace LDJsonParser {
export async function check(html: string) {
try {
const snippets: Array<string> = [];
const $ = cheerio.load(html);
let stock = false;
$('script[type="application/ld+json"]').each((index, element) => {
let json = JSON.parse($(element).html());
snippets.push($(element).html());
});
if (json["@type"]) {
if (json["@type"].includes("Product")) {
if (Array.isArray(json["offers"])) {
let anyStock = false;
console.log("Found " + snippets.length + " snippets");
for (let index in json["offers"]) {
if (json["offers"][index]["availability"].includes("InStock")) {
anyStock = true;
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;
}
}
if (anyStock) {
stock = true;
return;
}
} else {
if (json["offers"]["availability"].includes("InStock")) {
stock = true;
return;
}
}
}
} catch (error) {
continue;
}
});
}
return stock;
return false;
} catch (error) {
console.log(error);
console.error(`Error occured during stock check!`);