fix
This commit is contained in:
parent
3438daaa80
commit
224dd161b0
@ -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() {
|
||||
|
@ -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());
|
||||
});
|
||||
|
||||
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"])) {
|
||||
let anyStock = false;
|
||||
|
||||
for (let index in json["offers"]) {
|
||||
if (json["offers"][index]["availability"].includes("InStock")) {
|
||||
anyStock = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (anyStock) {
|
||||
stock = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (json["offers"]["availability"].includes("InStock")) {
|
||||
stock = true;
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return stock;
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.error(`Error occured during stock check!`);
|
||||
|
Loading…
x
Reference in New Issue
Block a user