From 22f380c3907b9acdd6296026762bda4ba6e1b930 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Sat, 27 May 2023 19:49:23 +0200 Subject: [PATCH] Generate quantity and unified unit for spar data --- stores/spar.js | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/stores/spar.js b/stores/spar.js index cda8502..e7749e4 100644 --- a/stores/spar.js +++ b/stores/spar.js @@ -1,16 +1,46 @@ const axios = require("axios"); 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 }, + 'kg': { unit: 'g', factor: 1000 }, + 'l': { unit: 'ml', factor: 1000 }, + '100ml': { unit: 'ml', factor: 100 }, + 'WG': { unit: 'wg', factor: 1 }, + '100g': { unit: 'g', factor: 100 }, + 'm': { unit: 'cm', factor: 100 }, + 'ml': { unit: 'ml', factor: 1 }, +}; + exports.getCanonical = function(item, today) { - let price, unit; + 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("€", "")); - unit = str_unit.trim(); } else { price = item.masterValues.price; - unit = item.masterValues["short-description-3"]; + } + if("short-description-3" in item.masterValues) { + let [rawQuantity, rawUnit] = item.masterValues["short-description-3"].replace(" EINWEG", "").replace(" MEHRWEG", "").trim().split(' '); + const conv = conversions[rawUnit]; + quantity = parseFloat(rawQuantity.replace(',','.')) * conv.factor; + unit = conv.unit; + } + else{ + // use price per unit to calculate quantity (less accurate) + let [unitPrice, rawUnit] = item.masterValues['price-per-unit'].split('/'); + unitPrice = parseFloat(unitPrice.replace("€", "")); + const conv = conversions[rawUnit]; + if(conv === undefined) + console.error("unknown unit: " + rawUnit) + quantity = Math.round(price / unitPrice * conv.factor) + unit = conv.unit; } return { id: item.masterValues["code-internal"], @@ -19,6 +49,7 @@ exports.getCanonical = function(item, today) { price, priceHistory: [{ date: today, price }], unit, + quantity, bio: item.masterValues.biolevel === "Bio" }; } @@ -27,4 +58,4 @@ 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; -} \ No newline at end of file +}