Formatting. If latest-canonical.json exists, load it and serve data immediately.

This commit is contained in:
Mario Zechner 2023-05-18 18:34:14 +02:00
parent ae8de5ccc2
commit 2e7b783739
2 changed files with 41 additions and 34 deletions

View File

@ -58,7 +58,7 @@ function hoferToCanonical(rawItems, today) {
id: item.ProductId,
name: item.ProductName,
price: item.Price,
priceHistory: [{date: today, price: item.Price}],
priceHistory: [{ date: today, price: item.Price }],
unit: `${item.Unit} ${item.UnitType}`
});
}
@ -66,32 +66,32 @@ function hoferToCanonical(rawItems, today) {
}
async function fetchHofer() {
const BASE_URL = `https://shopservice.roksh.at`
const CATEGORIES = BASE_URL+`/category/GetFullCategoryList/`
const CONFIG={headers: {authorization: null}}
const ITEMS = BASE_URL+`/productlist/CategoryProductList`
const BASE_URL = `https://shopservice.roksh.at`
const CATEGORIES = BASE_URL + `/category/GetFullCategoryList/`
const CONFIG = { headers: { authorization: null } }
const ITEMS = BASE_URL + `/productlist/CategoryProductList`
// fetch access token
const token_data ={"OwnWebshopProviderCode":"","SetUserSelectedShopsOnFirstSiteLoad":true,"RedirectToDashboardNeeded":false,"ShopsSelectedForRoot":"hofer","BrandProviderSelectedForRoot":null,"UserSelectedShops":[]}
const token = (await axios.post("https://shopservice.roksh.at/session/configure", token_data, {headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }})).headers['jwt-auth'];
CONFIG.headers.authorization = "Bearer " + token;
// fetch access token
const token_data = { "OwnWebshopProviderCode": "", "SetUserSelectedShopsOnFirstSiteLoad": true, "RedirectToDashboardNeeded": false, "ShopsSelectedForRoot": "hofer", "BrandProviderSelectedForRoot": null, "UserSelectedShops": [] }
const token = (await axios.post("https://shopservice.roksh.at/session/configure", token_data, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } })).headers['jwt-auth'];
CONFIG.headers.authorization = "Bearer " + token;
// concat all subcategories (categories.[i].ChildList)
const categories = (await axios.post(CATEGORIES, {}, CONFIG)).data;
const subCategories = categories.reduce((acc, category) => acc.concat(category.ChildList), []);
// concat all subcategories (categories.[i].ChildList)
const categories = (await axios.post(CATEGORIES, {}, CONFIG)).data;
const subCategories = categories.reduce((acc, category) => acc.concat(category.ChildList), []);
let hoferItems = [];
for (let subCategory of subCategories) {
let categoryData = (await axios.get(`${ITEMS}?progId=${subCategory.ProgID}&firstLoadProductListResultNum=4&listResultProductNum=24`, CONFIG)).data;
const numPages = categoryData.ProductListResults[0].ListContext.TotalPages;
let hoferItems = [];
for (let subCategory of subCategories) {
let categoryData = (await axios.get(`${ITEMS}?progId=${subCategory.ProgID}&firstLoadProductListResultNum=4&listResultProductNum=24`, CONFIG)).data;
const numPages = categoryData.ProductListResults[0].ListContext.TotalPages;
for (let iPage = 1; iPage <= numPages; iPage++) {
let items = (await axios.post(`${BASE_URL}/productlist/GetProductList`, {CategoryProgId: subCategory.ProgID, Page: iPage}, CONFIG)).data;
hoferItems = hoferItems.concat(items.ProductList);
for (let iPage = 1; iPage <= numPages; iPage++) {
let items = (await axios.post(`${BASE_URL}/productlist/GetProductList`, { CategoryProgId: subCategory.ProgID, Page: iPage }, CONFIG)).data;
hoferItems = hoferItems.concat(items.ProductList);
}
}
}
return hoferItems;
return hoferItems;
}
function mergePriceHistory(oldItems, items) {

View File

@ -1,21 +1,28 @@
const fs = require("fs");
const analysis = require("./analysis");
(async () => {
let items = await analysis.updateData("data");
setInterval(async () => { items = await analysis.updateData("data") }, 1000 * 60 * 60 * 24);
let items = [];
if (fs.existsSync("data/latest-canonical.json")) {
items = JSON.parse(fs.readFileSync("data/latest-canonical.json"));
analysis.updateData("data");
} else {
items = await analysis.updateData("data");
}
setInterval(async () => { items = await analysis.updateData("data") }, 1000 * 60 * 60 * 24);
const express = require('express')
const compression = require('compression');
const app = express()
const port = 3000
const express = require('express')
const compression = require('compression');
const app = express()
const port = 3000
app.use(compression());
app.use(compression());
app.get('/api/index', (req, res) => {
res.send(items)
})
app.get('/api/index', (req, res) => {
res.send(items)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
})();