heissepreise/stores/lidl.js
Mario Zechner 303d25ccb5 Categories for Billa & Spar, infra to add catgories for other stores.
Billa maps directly to the canonical categories. Spar uses a mapping file stores/spar-categories.json.

Each store has a generateCategoryMapping() function which is called once in analysis.js:updateData() and analysis.js:replay(). The function is responsible for

* Fetching the latest categories
* Merging them with already mapped categories
* Report new categories that haven't been mapped yet
* Report categories that have been mapped but are no longer part of the latest set of categories
* Save the merged mappings to disk

This schema might not work for all stores, in which case updateData() and replay() will use a knn approach to figure out the category for an item. See #81
2023-06-21 01:29:00 +02:00

65 lines
2.0 KiB
JavaScript

const axios = require("axios");
const utils = require("./utils");
const HITS = Math.floor(30000 + Math.random() * 2000);
const units = {
"": { unit: "stk", factor: 1 },
dosen: { unit: "stk", factor: 1 },
flasche: { unit: "stk", factor: 1 },
flaschen: { unit: "stk", factor: 1 },
"pkg.": { unit: "stk", factor: 1 },
};
exports.getCanonical = function (item, today) {
let quantity = 1;
let unit = "";
let text = (item.price.basePrice?.text ?? "").trim().split("(")[0].replaceAll(",", ".").toLowerCase();
let isWeighted = false;
if (text === "per kg") {
isWeighted = true;
unit = "kg";
} else {
if (text.startsWith("bei") && text.search("je ") != -1) text = text.substr(text.search("je "));
for (let s of ["ab ", "je ", "ca. ", "z.b.: ", "z.b. "]) text = text.replace(s, "").trim();
const regex = /^([0-9.x ]+)(.*)$/;
const matches = text.match(regex);
if (matches) {
matches[1].split("x").forEach((q) => {
quantity = quantity * parseFloat(q.split("/")[0]);
});
unit = matches[2].split("/")[0].trim().split(" ")[0];
}
unit = unit.split("-")[0];
}
return utils.convertUnit(
{
id: item.productId,
name: `${item.keyfacts?.supplementalDescription?.concat(" ") ?? ""}${item.fullTitle}`,
description: item.keyfacts?.description ?? "",
price: item.price.price,
priceHistory: [{ date: today, price: item.price.price }],
unit,
quantity,
url: item.canonicalUrl,
},
units,
"lidl"
);
};
exports.fetchData = async function () {
const LIDL_SEARCH = `https://www.lidl.at/p/api/gridboxes/AT/de/?max=${HITS}`;
return (await axios.get(LIDL_SEARCH)).data.filter((item) => !!item.price.price);
};
exports.initializeCategoryMapping = async () => {};
exports.mapCategory = (rawItem) => {};
exports.urlBase = "https://www.lidl.at";