heissepreise/stores/billa.js
2023-06-03 15:09:29 +02:00

48 lines
1.6 KiB
JavaScript

const axios = require("axios");
const utils = require("./utils");
const HITS = Math.floor(30000 + Math.random() * 2000);
const units = {
beutel: { unit: "stk", factor: 1 },
bund: { unit: "stk", factor: 1 },
packung: { unit: "stk", factor: 1 },
portion: { unit: "stk", factor: 1 },
rollen: { unit: "stk", factor: 1 },
teebeutel: { unit: "stk", factor: 1 },
waschgang: { unit: "wg", factor: 1 },
};
exports.getCanonical = function (item, today) {
let quantity = 1,
unit = "kg";
if (item.data.grammagePriceFactor == 1) {
if (item.data.grammage.indexOf("Per ") == 0) item.data.grammage = item.data.grammage.replace("Per ", "");
const grammage = item.data.grammage !== "" && item.data.grammage.trim().split(" ").length > 1 ? item.data.grammage : item.data.price.unit;
if (grammage) [quantity, unit] = grammage.trim().split(" ").splice(0, 2);
}
return utils.convertUnit(
{
id: item.data.articleId,
name: item.data.name,
price: item.data.price.final,
priceHistory: [{ date: today, price: item.data.price.final }],
isWeighted: item.data.isWeightArticle,
unit,
quantity,
bio: item.data.attributes && item.data.attributes.includes("s_bio"),
url: item.data.canonicalPath,
},
units,
"billa"
);
};
exports.fetchData = async function () {
const BILLA_SEARCH = `https://shop.billa.at/api/search/full?searchTerm=*&storeId=00-10&pageSize=${HITS}`;
return (await axios.get(BILLA_SEARCH)).data.tiles;
};
exports.urlBase = "https://shop.billa.at";