forked from PerssonP/malmofoods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
316 lines (256 loc) · 9.75 KB
/
api.ts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import * as dotenv from 'dotenv';
dotenv.config();
import express from 'express';
import cheerio from 'cheerio';
import moment from 'moment';
import NodeCache from 'node-cache';
import he from 'he';
import puppeteer from 'puppeteer';
import { weekdayFirstUpper } from './momentUtil.js';
type SimpleArrayData = string[];
type ArrayData = {
title: string;
description: string;
}[];
type ObjectData = {
[key: string]: string
};
type Menu = {
name: string;
data: SimpleArrayData | ArrayData | ObjectData
}
moment.locale('sv'); // Set global locale to Swedish;
const router = express.Router();
const cache = new NodeCache({ stdTTL: 86400 }); // TTL: 24h
const setInCache = (input: Menu) => {
cache.set(input.name, { date: moment().format('YYYY-MM-DD'), content: input });
}
const getFromCache = (key: string): { date: string; content: Menu | null } => {
const data = cache.get(key);
if (data) return data as { date: string; content: Menu };
return { date: '', content: null };
}
const sources: { [key: string]: (m: moment.Moment) => Promise<SimpleArrayData | ArrayData | ObjectData> } = {
'miamarias': async (m) => {
const result = await fetch('http://www.miamarias.nu/');
const body = await result.text();
const $ = cheerio.load(body);
const h5arr = $('h5.et_pb_toggle_title').toArray();
const node = h5arr.find(el => {
return $(el).text().includes(weekdayFirstUpper(m));
});
if (!node) throw new Error('Wrong day');
const parse = $(node).parent().find('p').toArray()
.map((el) => $(el).text().trim()) // Get all texts
.filter((value, index, originalArray) =>
!!value && // Remove empty texts
!value.endsWith(' kr') && // Remove categories
originalArray.slice(index + 1).indexOf(value) === -1 // Remove possible duplicates
);
const answer = {
name: 'miamarias',
data: [
{ title: 'Fisk', description: parse[0] },
{ title: 'Kött', description: parse[1] },
{ title: 'Veg', description: parse[2] }
]
}
setInCache(answer);
return answer.data;
},
'spill': async (m) => {
const result = await fetch('https://restaurangspill.se/');
const body = await result.text();
const $ = cheerio.load(body);
const section = $('h2:contains(Gängtappen)').parents()[2];
const node = $(section).find('.uppercase');
const currentDay = node.text().split(',')[1].trim();
if (currentDay != m.format('DD/M')) throw new Error('Wrong day');
const answer = {
name: 'spill',
data: $(node).siblings().children().toArray().map((el) => $(el).text().trim()).filter(text => text !== '')
}
setInCache(answer);
return answer.data;
},
'kolga': async (m) => {
const result = await fetch('https://kolga.gastrogate.com/lunch/');
const body = await result.text();
const $ = cheerio.load(body);
const header = $(`.menu_header h3:contains(${weekdayFirstUpper(m)}):contains(${m.format('D')})`).first();
if (header.length === 0) throw new Error('Wrong day');
const content = $(header).parents('thead').siblings('tbody')[0];
const answer = {
name: 'kolga',
data: $(content).find('.td_title').map((_, el) => $(el).text().trim()).get()
}
setInCache(answer);
return answer.data;
},
'p2': async (m) => {
const result = await fetch('https://www.restaurangp2.se/lunch');
const body = await result.text();
const $ = cheerio.load(body);
const node = $(`#${m.locale('en').format('dddd').toLowerCase()}`);
if (node.length === 0) throw new Error('Wrong day');
const courses = $(node).find('tr');
const answer = {
name: 'p2',
data: courses.map((_, el) => {
const arr = $(el).find('p').map((i, child) => $(child).text()).get();
if (arr.length < 2) throw new Error('Parsing failed');
return { title: arr[0], description: arr[1] };
}).get()
}
setInCache(answer);
return answer.data;
},
'dockanshamnkrog': async (m) => {
const result = await fetch('http://dockanshamnkrog.se/lunchmeny/');
const body = await result.text();
const $ = cheerio.load(body);
const menu = $('h2:contains(Lunch)').parent();
const week = menu.children(':contains(VECKA)');
if (!week.text().trim().endsWith(m.week().toString())) {
throw new Error('Wrong week');
}
const day = menu.children(`p:contains(${weekdayFirstUpper(m)})`)
if (day.text() === '') throw new Error('Day not found');
const answer = {
name: 'dockanshamnkrog',
data: [
day.text().split('\n')[1]
]
}
setInCache(answer);
return answer.data;
},
'namdo': async (m) => {
const result = await fetch('http://namdo.se/meny/');
const body = await result.text();
const $ = cheerio.load(body);
const node = $(`.fdm-section-${m.format('dddd').replace('å', 'a').replace('ö', 'o')}-${m.week() % 2 === 0 ? 'jamn' : 'ojamn'}`);
if (node.length === 0) throw new Error('Wrong day');
const titles = $(node).find('.fdm-item-title');
const descriptions = $(node).find('.fdm-item-content');
if (titles.length !== descriptions.length) throw new Error('Parsing length mismatch!');
const answer = { name: 'namdo', data: [] as ArrayData };
titles.each((i, el) => {
answer.data.push({ title: $(el).text(), description: $(descriptions[i]).text() });
});
setInCache(answer);
return answer.data;
},
'docksideburgers': async (m) => {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
await page.goto('https://www.facebook.com/DocksideBurgers/');
const element = await page.waitForSelector("::-p-xpath(//span[contains(., 'Månadens')])")
const text = await element?.evaluate(e => e.textContent);
await browser.close();
return [ 'Från Docksides facebook:', text || '' ] // todo
},
'storavarvsgatan6': async (m) => {
const result = await fetch('https://storavarvsgatan6.se/meny.html');
const body = await result.text();
const $ = cheerio.load(body);
const weekNode = $('p:contains("Veckans meny")').first();
if (Number(weekNode.text().trim().split('.').pop()) !== m.week()) throw new Error('Weekly menu not yet posted');
const weekDayNode = weekNode.siblings(`p:contains(${weekdayFirstUpper(m)})`);
const menu: string[] = [];
let row = weekDayNode.next();
while (row.text().trim() !== '') {
menu.push(row.text());
row = row.next();
}
const answer = {
name: 'storavarvsgatan6',
data: menu
};
setInCache(answer);
return answer.data;
},
'laziza': async () => {
return ['Libanesisk buffé'] // todo
},
'thapthim': async (m) => {
const result = await fetch('https://api.thapthim.se/?read=lunchinfo&store=vh');
const json = await result.json() as any;
const answer = {
name: 'thapthim',
data: [] as ArrayData
}
const weekly: any[] = json.weekexp.Veckans.filter((value: any) => !!value?.title);
for (const meal of weekly) {
answer.data.push({ title: meal.title, description: he.decode(meal.desc) })
}
const daily = json.weekexp[weekdayFirstUpper(m)]?.filter((value: any) => !!value?.title);
if (!daily || daily.length === 0) throw new Error('Day not found')
for (const meal of daily) {
answer.data.push({ title: meal.title, description: he.decode(meal.desc) })
}
setInCache(answer);
return answer.data;
},
'eatery': async (m) => {
const result = await fetch('https://api.eatery.se/wp-json/eatery/v1/load');
const json = await result.json() as any;
const lunchmenuID = json.eateries["\/vastra-hamnen"].menues.lunchmeny
const title = json.menues[lunchmenuID].content.title;
const content = json.menues[lunchmenuID].content.content;
if (Number(title.split(' ').at(-1)) !== m.week()) throw new Error('Weekly menu not yet posted');
const $ = cheerio.load(content);
const items = $(`p:contains(${m.format('dddd').toUpperCase()})`).text().split('\n');
if (items.length < 1) throw new Error('No data found for day');
const answer = {
name: 'eatery',
data: items.slice(1)
};
setInCache(answer);
return answer.data;
},
'valfarden': async (m) => {
const result = await fetch('https://valfarden.nu/dagens-lunch/');
const body = await result.text();
const $ = cheerio.load(body);
const weekNode = $('h2:contains(Vecka)');
if (weekNode.text().split(' ')[1].slice(0, -1) !== m.week().toString())
throw new Error('Wrong week');
const dayNodes = weekNode.siblings().toArray().map(e => $(e).text());
const menu: string[] = [];
let index = dayNodes.findIndex(n => n.startsWith(weekdayFirstUpper(m)));
if (index === -1) throw new Error('Wrong day');
index++;
let node = dayNodes.at(index);
const endString = weekdayFirstUpper(m.add(1, 'day'));
while (node !== undefined && !node.startsWith(endString)) {
node = node.trim();
if (node !== '' && node !== '–') menu.push(node);
index++;
node = dayNodes.at(index);
}
const answer = {
name: 'valfarden',
data: menu
};
setInCache(answer);
return answer.data;
}
}
router.get('/api/:source', async (req, res, next) => {
try {
const m = moment();
const source = req.params.source;
if (req.query.force !== 'true') {
const cacheData = getFromCache(source);
if (cacheData.content !== null && cacheData.date === m.format('YYYY-MM-DD')) return res.send(cacheData.content.data);
}
const answer = await sources[source](m);
return res.send(answer);
} catch (error) {
console.log(error);
if (error instanceof Error) return res.send({ error: `Error: ${error.message}` });
else return res.send({ error: `Error: ${error}` });
}
});
export default router;