heissepreise/stores/spar.js
Mario Zechner 23f512087e Refactored and fix #55
- `readJson()` now just checks for the file extension to decide whether to uncompress instead of taking a flag.
- moved migration logic from index.js to analysis.js:migrateToGzip
- fixed `restore()` in analysis.js
- also calling `migrateToGzip()` in replay.js
- Fix billa canonicalization for Dossier data
- Fix spar canonicalization for Dossier data and data from 2022.
2023-06-02 18:34:14 +02:00

76 lines
3.0 KiB
JavaScript

const axios = require("axios");
const utils = require("./utils");
const HITS = Math.floor(30000 + Math.random() * 2000);
const conversions = {
g: { unit: "g", factor: 1 },
kg: { unit: "g", factor: 1000 },
l: { unit: "ml", factor: 1000 },
ml: { unit: "ml", factor: 1 },
stk: { unit: "stk", factor: 1 },
stück: { unit: "stk", factor: 1 },
"100ml": { unit: "ml", factor: 100 },
wg: { unit: "wg", factor: 1 },
"100g": { unit: "g", factor: 100 },
m: { unit: "cm", factor: 100 },
cm: { unit: "cm", factor: 100 },
ml: { unit: "ml", factor: 1 },
meter: { unit: "cm", factor: 100 },
mm: { unit: "cm", factor: 0.1 },
"stk.": { unit: "cm", factor: 0.1 },
cl: { unit: "ml", factor: 10 },
blatt: { unit: "stk", factor: 1 },
};
exports.getCanonical = function (item, today) {
let price, unit, quantity;
if (item.masterValues["quantity-selector"]) {
const [str_price, str_unit] = item.masterValues["price-per-unit"].split("/");
price = parseFloat(str_price.replace("€", ""));
} else {
price = item.masterValues.price;
}
let description = item.masterValues["short-description-3"] ?? item.masterValues["short-description-2"];
if (!description || description.length == 0) {
description = (item.masterValues["short-description"] ?? item.masterValues.name).toLowerCase();
if (description.endsWith("per kg"))
[quantity, unit] = [1, "kg"];
else if (description.endsWith("im topf"))
[quantity, unit] = [1, "kg"];
else
[quantity, unit] = [1, "stk."];
} else {
const s = description.replace(" EINWEG", "").replace(" MEHRWEG", "").trim();
const q = utils.parseUnitAndQuantityAtEnd(s);
quantity = q[0];
unit = q[1];
}
if (conversions[unit] === undefined) {
// use price per unit to calculate quantity (less accurate)
let [unitPrice, unit_] = item.masterValues["price-per-unit"].split("/");
unitPrice = parseFloat(unitPrice.replace("€", ""));
quantity = parseFloat((price / unitPrice).toFixed(3));
unit = unit_.toLowerCase();
}
return utils.convertUnit({
id: item.masterValues["code-internal"],
sparId: item.masterValues["product-number"],
name: item.masterValues.title + " " + (item.masterValues["short-description"] ?? item.masterValues.name).toLowerCase(),
price,
priceHistory: [{ date: today, price }],
unit,
quantity,
isWeighted: item.masterValues['item-type'] === 'WeightProduct',
bio: item.masterValues.biolevel === "Bio",
url: item.masterValues.url,
}, conversions, 'spar');
}
exports.fetchData = async function () {
const SPAR_SEARCH = `https://search-spar.spar-ics.com/fact-finder/rest/v4/search/products_lmos_at?query=*&q=*&page=1&hitsPerPage=${HITS}`;
const rawItems = (await axios.get(SPAR_SEARCH)).data.hits;
return rawItems?.hits || rawItems;
}
exports.urlBase = "https://www.interspar.at/shop/lebensmittel"