-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (100 loc) · 3.11 KB
/
index.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
'use strict';
const express = require('express');
const app = express();
const fs = require('fs');
const fetch = require('node-fetch');
const filename = 'products.json';
const productsUrl = 'https://www.mcdonalds.at/unsere-produkte';
const rProducts = /href="(?<url>https:\/\/www.mcdonalds.at\/produkt\/[a-zA-Z0-9-]+)"/g;
const rContent = /<b>Portion<\/b>(?<content>.+?)<b>per 100g<\/b>/;
const rKcal = /<b>(?<kcal>\d*\.*?\d)kcal/;
const rProtein = /Eiweiß<\/b><\/p><p><b>(?<protein>\d*\.*?\d)g/;
const excludes = [
'tee',
'sprite',
'saft',
'tea',
'cola',
'fanta',
'espresso',
'voeslauer',
'cafe',
'drink',
'caffe',
'cappuccino',
];
app.get('/ranking', async(req, res) => {
if (fs.existsSync(filename)) {
const data = fs.readFileSync(filename, 'utf8');
res.end(data);
} else {
const urls = await getProductUrls();
const products = await getProducts(urls);
const json = JSON.stringify(products);
fs.writeFileSync(filename, json);
res.end(json);
}
});
const server = app.listen(8080, () => {
const host = server.address().address;
const port = server.address().port;
console.log('Mc-Getter listening at http://%s:%s', host, port);
});
// const main = async() => {
// const urls = await getProductUrls();
// const products = await getProducts(urls);
// console.table(products);
// }
const getProducts = async(urls) => {
let products = [];
for (const u of urls) {
let p = await getProductByUrl(u);
if (!p.mn || p.mn === -1 || p.protein <= 0) continue;
if (products.find(x => x.name === p.name)) continue;
console.log('Adding: ', p.name);
products.push(p);
}
products.sort((a, b) => a.mn - b.mn);
return products;
};
const getProductByUrl = async(url) => {
const name = url.split('/').pop();
const isBad = excludes.some(x => name.includes(x));
if (isBad) return createEmptyProduct();
try {
let html = await getHtmlByUrl(url);
html = html.replace(/[\n\t]/g, '');
const content = rContent.exec(html).groups.content;
const kcal = +rKcal.exec(content).groups.kcal;
const protein = +rProtein.exec(content).groups.protein;
const mn = Math.round((kcal / protein) * 100) / 100;
return createProduct(name, kcal, protein, mn);
} catch (ex) {
console.error('Error at: ', url);
return createEmptyProduct();
}
};
const createEmptyProduct = () => createProduct(null, null, null, null);
const createProduct = (name, kcal, protein, mn) => {
return {
name: name,
kcal: kcal,
protein: protein,
mn: mn,
};
};
const getProductUrls = async() => {
const html = await getHtmlByUrl(productsUrl);
const urls = [];
let match = rProducts.exec(html);
do {
urls.push(match.groups.url);
} while ((match = rProducts.exec(html)) !== null);
console.log(`Found ${urls.length} products`);
return urls;
};
const getHtmlByUrl = async(url) => {
const response = await fetch(url);
return await response.text();
};
// main();