2023-05-25 13:54:28 +02:00
|
|
|
const axios = require("axios");
|
2023-05-28 19:48:06 +02:00
|
|
|
const utils = require("./utils");
|
2023-05-25 13:54:28 +02:00
|
|
|
const HITS = Math.floor(30000 + Math.random() * 2000);
|
|
|
|
|
2023-06-03 15:09:29 +02:00
|
|
|
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 },
|
2023-05-27 22:00:27 +02:00
|
|
|
};
|
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
exports.getCanonical = function (item, today) {
|
|
|
|
let quantity = 1,
|
|
|
|
unit = "kg";
|
2023-06-02 18:34:14 +02:00
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
if (item.data.grammagePriceFactor == 1) {
|
2023-06-03 15:09:29 +02:00
|
|
|
if (item.data.grammage.indexOf("Per ") == 0) item.data.grammage = item.data.grammage.replace("Per ", "");
|
2023-06-02 16:45:54 +02:00
|
|
|
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);
|
2023-05-27 22:00:27 +02:00
|
|
|
}
|
2023-06-02 16:56:22 +02:00
|
|
|
|
2023-06-03 15:09:29 +02:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
};
|
2023-05-25 13:54:28 +02:00
|
|
|
|
2023-06-02 16:45:54 +02:00
|
|
|
exports.fetchData = async function () {
|
2023-06-06 18:36:55 +02:00
|
|
|
const items = [];
|
|
|
|
const lookup = {};
|
|
|
|
let numDuplicates = 0;
|
2023-06-06 15:15:39 +02:00
|
|
|
|
|
|
|
for (let i = 1; i <= utils.globalCategories.length; i++) {
|
|
|
|
const category = utils.globalCategories[i - 1];
|
|
|
|
const categoryCode = i < 10 ? "" + i : String.fromCharCode("A".charCodeAt(0) + (i - 10));
|
|
|
|
|
|
|
|
for (let j = 1; j <= category.subcategories.length; j++) {
|
|
|
|
const subCategoryCode = j < 10 ? "" + j : String.fromCharCode("A".charCodeAt(0) + (j - 10));
|
|
|
|
const code = `B2-${categoryCode}${subCategoryCode}`;
|
|
|
|
|
|
|
|
const BILLA_SEARCH = `https://shop.billa.at/api/search/full?searchTerm=*&storeId=00-10&pageSize=${HITS}&category=${code}`;
|
|
|
|
const data = (await axios.get(BILLA_SEARCH)).data;
|
|
|
|
data.tiles.forEach((item) => {
|
|
|
|
try {
|
2023-06-06 18:36:55 +02:00
|
|
|
const canonicalItem = exports.getCanonical(item);
|
|
|
|
if (lookup[canonicalItem.id]) {
|
|
|
|
numDuplicates++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lookup[canonicalItem.id] = item;
|
2023-06-06 15:15:39 +02:00
|
|
|
items.push(item);
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore super tiles
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 18:36:55 +02:00
|
|
|
console.log(`Duplicate items in BILLA data: ${numDuplicates}, total items: ${items.length}`);
|
2023-06-06 15:15:39 +02:00
|
|
|
return items;
|
2023-06-03 15:09:29 +02:00
|
|
|
};
|
2023-06-02 10:38:14 +02:00
|
|
|
|
2023-06-03 15:09:29 +02:00
|
|
|
exports.urlBase = "https://shop.billa.at";
|