forked from badlogic/heissepreise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.js
240 lines (209 loc) · 9.35 KB
/
analysis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const fs = require("fs");
const axios = require("axios")
function currentDate() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const day = String(currentDate.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
function readJSON(file) {
return JSON.parse(fs.readFileSync(file));
}
function writeJSON(file, data) {
fs.writeFileSync(file, JSON.stringify(data, null, 2));
}
function sparToCanonical(rawItems, today) {
const canonicalItems = [];
for (let i = 0; i < rawItems.length; i++) {
const item = rawItems[i];
let price, unit;
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"];
}
canonicalItems.push({
store: "spar",
id: item.masterValues["code-internal"],
name: item.masterValues.title + " " + item.masterValues["short-description"],
price,
priceHistory: [{ date: today, price }],
unit
});
}
return canonicalItems;
}
function billaToCanonical(rawItems, today) {
const canonicalItems = [];
for (let i = 0; i < rawItems.length; i++) {
const item = rawItems[i];
canonicalItems.push({
store: "billa",
id: item.data.articleId,
name: item.data.name,
price: item.data.price.final,
priceHistory: [{ date: today, price: item.data.price.final }],
unit: item.data.grammage
});
}
return canonicalItems;
}
function hoferToCanonical(rawItems, today) {
const canonicalItems = [];
for (let i = 0; i < rawItems.length; i++) {
const item = rawItems[i];
canonicalItems.push({
store: "hofer",
id: item.ProductID,
name: item.ProductName,
price: item.Price,
priceHistory: [{ date: today, price: item.Price }],
unit: `${item.Unit} ${item.UnitType}`
});
}
return canonicalItems;
}
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`
// 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), []);
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);
}
}
return hoferItems;
}
function mergePriceHistory(oldItems, items) {
if (oldItems == null) return items;
const lookup = {}
for (oldItem of oldItems) {
lookup[oldItem.store + oldItem.id] = oldItem;
}
for (item of items) {
let oldItem = lookup[item.store + item.id];
let currPrice = item.priceHistory[0];
if (oldItem) {
if (oldItem.priceHistory[0].price == currPrice.price) {
item.priceHistory = oldItem.priceHistory;
continue;
}
for (oldPrice of oldItem.priceHistory) {
item.priceHistory.push(oldPrice);
}
}
}
return items;
}
/// Given a directory of raw data of the form `billa-$date.json` and `spar-$date.json`, constructs
/// a canonical list of all products and their historical price data.
function replay(rawDataDir) {
const today = currentDate();
const files = fs.readdirSync(rawDataDir).filter(
file => file.indexOf("canonical") == -1 && (file.indexOf("billa-") == 0 || file.indexOf("spar") == 0 || file.indexOf("hofer") == 0)
);
const dateSort = (a, b) => {
const dateA = new Date(a.match(/\d{4}-\d{2}-\d{2}/)[0]);
const dateB = new Date(b.match(/\d{4}-\d{2}-\d{2}/)[0]);
return dateA - dateB;
};
const sparFiles = files.filter(file => file.indexOf("spar-") == 0).sort(dateSort).map(file => rawDataDir + "/" + file);
const sparFilesCanonical = sparFiles.map(file => sparToCanonical(readJSON(file), file.match(/\d{4}-\d{2}-\d{2}/)[0]));
const billaFiles = files.filter(file => file.indexOf("billa-") == 0).sort(dateSort).map(file => rawDataDir + "/" + file);
const billaFilesCanonical = billaFiles.map(file => billaToCanonical(readJSON(file), file.match(/\d{4}-\d{2}-\d{2}/)[0]));
const hoferFiles = files.filter(file => file.indexOf("hofer-") == 0).sort(dateSort).map(file => rawDataDir + "/" + file);
const hoferFilesCanonical = hoferFiles.map(file => hoferToCanonical(readJSON(file), file.match(/\d{4}-\d{2}-\d{2}/)[0]));
const allFilesCanonical = [];
const len = Math.max(sparFilesCanonical.length, Math.max(billaFilesCanonical.length, hoferFilesCanonical.length));
sparFilesCanonical.reverse();
billaFilesCanonical.reverse();
hoferFilesCanonical.reverse();
for (let i = 0; i < len; i++) {
const canonical = [];
let billa = billaFilesCanonical.pop();
if (billa) canonical.push(...billa);
let spar = sparFilesCanonical.pop();
if (spar) canonical.push(...spar);
let hofer = hoferFilesCanonical.pop();
if (hofer) canonical.push(...hofer);
allFilesCanonical.push(canonical);
}
if (allFilesCanonical.length == 0) return null;
if (allFilesCanonical.length == 1) return allFilesCanonical[0];
let prev = allFilesCanonical[0];
let curr = null;
for (let i = 1; i < allFilesCanonical.length; i++) {
curr = allFilesCanonical[i];
mergePriceHistory(prev, curr);
prev = curr;
}
return curr;
}
const HITS = Math.floor(30000 + Math.random() * 2000);
const SPAR_SEARCH = `https://search-spar.spar-ics.com/fact-finder/rest/v4/search/products_lmos_at?query=*&q=*&page=1&hitsPerPage=${HITS}`;
const BILLA_SEARCH = `https://shop.billa.at/api/search/full?searchTerm=*&storeId=00-10&pageSize=${HITS}`;
exports.updateData = async function (dataDir, done) {
const today = currentDate();
console.log("Fetching data for date: " + today);
let items = [];
for (const shop of ['spar', 'billa', 'hofer']) {
let start = performance.now();
let itemsCanonical;
let shopItems;
const itemsFile = `${dataDir}/${shop}-${today}.json`
const load = fs.existsSync(itemsFile);
if(load) {
shopItems = JSON.parse(fs.readFileSync(itemsFile));
}
else {
if(shop === 'spar') shopItems = (await axios.get(SPAR_SEARCH)).data.hits;
if(shop === 'billa') shopItems = (await axios.get(BILLA_SEARCH)).data.hits;
if(shop === 'hofer') shopItems = await fetchHofer();
fs.writeFileSync(itemsFile, JSON.stringify(shopItems, null, 2));
}
if(shop === 'spar') itemsCanonical = sparToCanonical(shopItems, today);
if(shop === 'billa') itemsCanonical = billaToCanonical(shopItems, today);
if(shop === 'hofer') itemsCanonical = hoferToCanonical(shopItems, today);
items = items.concat(itemsCanonical);
console.log(`${load ? 'Loaded' : 'Fetched'} ${shop.toUpperCase()} data, took ${(performance.now() - start) / 1000} seconds`);
}
if (fs.existsSync(`${dataDir}/latest-canonical.json`)) {
const oldItems = JSON.parse(fs.readFileSync(`${dataDir}/latest-canonical.json`));
mergePriceHistory(oldItems, items);
console.log("Merged price history");
}
fs.writeFileSync(`${dataDir}/latest-canonical.json`, JSON.stringify(items, null, 2));
if (done) done(items);
return items;
}
async function restore() {
console.log("Restoring data");
writeJSON("data/latest-canonical.json", replay("data"));
await exports.updateData("data");
const today = currentDate();
const items = readJSON("data/latest-canonical.json");
for (item of items) {
if (item.priceHistory[0].date == today && item.priceHistory.length > 1) {
console.log(`${item.store} ${item.name} ${item.priceHistory[1].price} -> ${item.priceHistory[0].price}`);
}
}
}
// writeJSON("data/latest-canonical-replay.json", replay("data"));
// restore()