heissepreise/stores/unimarkt.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

75 lines
2.2 KiB
JavaScript

const axios = require("axios");
const utils = require("./utils");
const HTMLParser = require("node-html-parser");
const units = {
mbe: { unit: "wg", factor: 1 },
};
exports.getCanonical = function (item, today) {
let [quantity, unit] = utils.parseUnitAndQuantityAtEnd(item.unit.replace("/ EINWEG", "").replace("/ MEHRWEG", ""));
return utils.convertUnit(
{
id: item.id,
name: item.name,
// description: "", not available
price: item.price,
priceHistory: [{ date: today, price: item.price }],
quantity,
unit,
bio: item.name.toLowerCase().includes("bio"),
url: item.canonicalUrl,
},
units,
"unimarkt",
{
unit: "stk",
quantity: 1,
}
);
};
exports.fetchData = async function () {
const UNIMARKT_BASE_URL = `https://shop.unimarkt.at/`;
const UNIMARKT_MAIN_CATEGORIES = [
"obst-gemuese",
"kuehlprodukte",
"fleisch-wurst",
"brot-gebaeck",
"getraenke",
"lebensmittel",
"suesses-snacks",
"haushalt",
];
let unimarktItems = [];
for (let category of UNIMARKT_MAIN_CATEGORIES) {
var res = await axios.get(UNIMARKT_BASE_URL + category, {
validateStatus: function (status) {
return status >= 200 && status < 300;
},
});
if (res && res.data) {
var root = HTMLParser.parse(res.data);
root.querySelectorAll(".articleListItem .produktContainer").forEach((product) => {
unimarktItems.push({
id: product._attrs["data-articleid"],
name: product.querySelector(".name").text,
price: parseFloat(product._attrs["data-price"]),
unit: product.querySelector(".grammatur").text,
canonicalUrl: product.querySelector(".image > a")._attrs["href"],
});
});
}
}
return unimarktItems;
};
exports.initializeCategoryMapping = async () => {};
exports.mapCategory = (rawItem) => {};
exports.urlBase = "https://shop.unimarkt.at";