heissepreise/stores/mpreis.js

67 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-05-25 13:54:28 +02:00
const axios = require("axios");
const utils = require("./utils");
2023-05-25 13:54:28 +02:00
2023-05-27 22:27:09 +02:00
const conversions = {
2023-06-01 08:25:04 +02:00
'cm': { unit: 'cm', factor: 1 },
'dag': { unit: 'g', factor: 10 },
'dl': { unit: 'ml', factor: 10 },
'grm': { unit: 'g', factor: 1 },
'kgm': { unit: 'g', factor: 1000 },
'ltr': { unit: 'ml', factor: 1000 },
'mlt': { unit: 'ml', factor: 1 },
'mtr': { unit: 'm', factor: 1 },
'stk': { unit: 'stk', factor: 1 },
'stk.': { unit: 'stk', factor: 1 },
'g': { unit: 'g', factor: 1 },
'anw': { unit: 'stk', factor: 1 },
'l': { unit: 'ml', factor: 1000 },
'm': { unit: 'cm', factor: 100 },
'ml': { unit: 'ml', factor: 1 },
'kg': { unit: 'g', factor: 1000 },
'paar': { unit: 'stk', factor: 1 },
'stück': { unit: 'stk', factor: 1 },
'bl.': { unit: 'stk', factor: 1 },
'pkg': { unit: 'stk', factor: 1 },
'gr': { unit: 'g', factor: 1 },
'er': { unit: 'stk', factor: 1 },
2023-05-27 22:27:09 +02:00
};
2023-05-25 13:54:28 +02:00
exports.getCanonical = function(item, today) {
2023-05-27 22:27:09 +02:00
let quantity = item.prices[0].presentationPrice.measurementUnit.quantity
2023-06-01 08:25:04 +02:00
let unit = item.prices[0].presentationPrice.measurementUnit.unitCode.toLowerCase()
if(['xro', 'h87', 'hlt'].indexOf(unit)!=-1) {
const q = utils.parseUnitAndQuantityAtEnd(item.mixins.productCustomAttributes.packagingUnit)
quantity = q[0] ?? quantity;
unit = q[1];
}
if (!(unit in conversions)) {
unit = 'stk';
}
const isWeighted = (item.mixins.productCustomAttributes?.packagingDescription ?? "").startsWith("Gewichtsware");
return utils.convertUnit({
2023-05-25 13:54:28 +02:00
id: item.code,
2023-05-28 19:10:08 +02:00
name: item.name[0],
isWeighted,
price: isWeighted ? item.prices[0].effectiveAmount : item.prices[0].presentationPrice.effectiveAmount,
2023-05-25 13:54:28 +02:00
priceHistory: [{ date: today, price: item.prices[0].presentationPrice.effectiveAmount }],
2023-05-27 22:27:09 +02:00
unit,
quantity,
2023-05-26 18:12:29 +02:00
bio: item.mixins.mpreisAttributes.properties?.includes('BIO'),
}, conversions, 'mpreis');
2023-05-25 13:54:28 +02:00
}
exports.fetchData = async function() {
const MPREIS_URL = `https://ax2ixv4hll-dsn.algolia.net/1/indexes/prod_mpreis_8450/browse?X-Algolia-API-Key=NmJlMTI0NjY1NGU4MDUwYTRlMmYzYWFjOWFlY2U4MGFkNGZjMDY2NmNjNjQzNWY3OWJlNDY4OTY0ZjEwOTEwYWZpbHRlcnM9cHVibGlzaGVk&X-Algolia-Application-Id=AX2IXV4HLL&X-Algolia-Agent=Vue.js`
let mpreisItems = [];
let res = (await axios.get(MPREIS_URL)).data;
mpreisItems = mpreisItems.concat(res.hits);
cursor = res.cursor;
while (cursor) {
res = (await axios.get(MPREIS_URL + `&cursor=${cursor}`)).data;
mpreisItems = mpreisItems.concat(res.hits);
cursor = res.cursor;
}
return mpreisItems;
}
exports.urlBase = "https://www.mpreis.at/shop/p/"