mirror of
https://github.com/badlogic/heissepreise.git
synced 2024-07-01 04:46:04 +02:00
Formatting. If latest-canonical.json exists, load it and serve data immediately.
This commit is contained in:
parent
ae8de5ccc2
commit
2e7b783739
42
analysis.js
42
analysis.js
|
@ -58,7 +58,7 @@ function hoferToCanonical(rawItems, today) {
|
||||||
id: item.ProductId,
|
id: item.ProductId,
|
||||||
name: item.ProductName,
|
name: item.ProductName,
|
||||||
price: item.Price,
|
price: item.Price,
|
||||||
priceHistory: [{date: today, price: item.Price}],
|
priceHistory: [{ date: today, price: item.Price }],
|
||||||
unit: `${item.Unit} ${item.UnitType}`
|
unit: `${item.Unit} ${item.UnitType}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -66,32 +66,32 @@ function hoferToCanonical(rawItems, today) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchHofer() {
|
async function fetchHofer() {
|
||||||
const BASE_URL = `https://shopservice.roksh.at`
|
const BASE_URL = `https://shopservice.roksh.at`
|
||||||
const CATEGORIES = BASE_URL+`/category/GetFullCategoryList/`
|
const CATEGORIES = BASE_URL + `/category/GetFullCategoryList/`
|
||||||
const CONFIG={headers: {authorization: null}}
|
const CONFIG = { headers: { authorization: null } }
|
||||||
const ITEMS = BASE_URL+`/productlist/CategoryProductList`
|
const ITEMS = BASE_URL + `/productlist/CategoryProductList`
|
||||||
|
|
||||||
// fetch access token
|
// fetch access token
|
||||||
const token_data ={"OwnWebshopProviderCode":"","SetUserSelectedShopsOnFirstSiteLoad":true,"RedirectToDashboardNeeded":false,"ShopsSelectedForRoot":"hofer","BrandProviderSelectedForRoot":null,"UserSelectedShops":[]}
|
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'];
|
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;
|
CONFIG.headers.authorization = "Bearer " + token;
|
||||||
|
|
||||||
// concat all subcategories (categories.[i].ChildList)
|
// concat all subcategories (categories.[i].ChildList)
|
||||||
const categories = (await axios.post(CATEGORIES, {}, CONFIG)).data;
|
const categories = (await axios.post(CATEGORIES, {}, CONFIG)).data;
|
||||||
const subCategories = categories.reduce((acc, category) => acc.concat(category.ChildList), []);
|
const subCategories = categories.reduce((acc, category) => acc.concat(category.ChildList), []);
|
||||||
|
|
||||||
let hoferItems = [];
|
let hoferItems = [];
|
||||||
for (let subCategory of subCategories) {
|
for (let subCategory of subCategories) {
|
||||||
let categoryData = (await axios.get(`${ITEMS}?progId=${subCategory.ProgID}&firstLoadProductListResultNum=4&listResultProductNum=24`, CONFIG)).data;
|
let categoryData = (await axios.get(`${ITEMS}?progId=${subCategory.ProgID}&firstLoadProductListResultNum=4&listResultProductNum=24`, CONFIG)).data;
|
||||||
const numPages = categoryData.ProductListResults[0].ListContext.TotalPages;
|
const numPages = categoryData.ProductListResults[0].ListContext.TotalPages;
|
||||||
|
|
||||||
for (let iPage = 1; iPage <= numPages; iPage++) {
|
for (let iPage = 1; iPage <= numPages; iPage++) {
|
||||||
let items = (await axios.post(`${BASE_URL}/productlist/GetProductList`, {CategoryProgId: subCategory.ProgID, Page: iPage}, CONFIG)).data;
|
let items = (await axios.post(`${BASE_URL}/productlist/GetProductList`, { CategoryProgId: subCategory.ProgID, Page: iPage }, CONFIG)).data;
|
||||||
hoferItems = hoferItems.concat(items.ProductList);
|
hoferItems = hoferItems.concat(items.ProductList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return hoferItems;
|
return hoferItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
function mergePriceHistory(oldItems, items) {
|
function mergePriceHistory(oldItems, items) {
|
||||||
|
|
33
index.js
33
index.js
|
@ -1,21 +1,28 @@
|
||||||
|
const fs = require("fs");
|
||||||
const analysis = require("./analysis");
|
const analysis = require("./analysis");
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
let items = await analysis.updateData("data");
|
let items = [];
|
||||||
setInterval(async () => { items = await analysis.updateData("data") }, 1000 * 60 * 60 * 24);
|
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 express = require('express')
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = 3000
|
const port = 3000
|
||||||
|
|
||||||
app.use(compression());
|
app.use(compression());
|
||||||
|
|
||||||
app.get('/api/index', (req, res) => {
|
app.get('/api/index', (req, res) => {
|
||||||
res.send(items)
|
res.send(items)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Example app listening on port ${port}`)
|
console.log(`Example app listening on port ${port}`)
|
||||||
})
|
})
|
||||||
})();
|
})();
|
Loading…
Reference in New Issue
Block a user