-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.ts
More file actions
367 lines (301 loc) · 11.4 KB
/
api.ts
File metadata and controls
367 lines (301 loc) · 11.4 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import express from 'express';
import * as cheerio from 'cheerio';
import moment from 'moment';
import NodeCache from 'node-cache';
import he from 'he';
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 null;
}
const sources: { [key: string]: (m: moment.Moment) => Promise<SimpleArrayData | ArrayData | ObjectData> } = {
'miamarias': async (m) => {
if (m.day() < 1 || m.day() > 5)
throw new Error('No menu available for current day of week');
const result = await fetch('https://miamarias.nu/lunch/');
const body = await result.text();
const $ = cheerio.load(body);
if (Number($('h2:contains(Meny vecka)').text().trim().split(' ').at(-1)) !== m.week())
throw new Error('Wrong week');
const menus = $('.e-n-tabs-content').children().toArray();
const dishes = $(menus[m.day() - 1]).children().toArray();
const data = [];
for (let dish of dishes) {
const content = $(dish).children().toArray();
const title = $(content[0]).text().trim();
const description = $(content[2]).text().trim();
data.push({ title, description });
}
const answer = {
name: 'miamarias',
data
}
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');
const body = await result.text();
const $ = cheerio.load(body);
const node = $(`h3:contains("${weekdayFirstUpper(m)}")`)
if (node.length === 0) throw new Error('Wrong day');
const courses = node.parent().parent().parent().find('.lunchmeny_container');
const answer = {
name: 'p2',
data: courses.map((_, el) => {
const title = $(el).find('.lunch_title').text();
const description = $(el).find('.lunch_desc').text();
if (title.length == 0 || description.length == 0)
throw new Error('Parsing failed');
return { title, description };
}).get()
}
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' });
try {
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 || ''];
} catch (e) {
await browser.close();
throw e;
}*/
throw new Error("Not implemented");
},
'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("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) => {
// https://thatsup.website/storage/462/59069/V.21-MALM%C3%96-NY-.pdf
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: string = json.menues[lunchmenuID].content.title;
const content: string[] = json.menues[lunchmenuID].content.content.split('\n');
if (Number(title.match(/\d+/)?.[0]) !== m.week()) throw new Error('Weekly menu not yet posted');
const start = m.format('dddd').toUpperCase();
const end = m.add(1, 'day').format('dddd').toUpperCase();
const startIndex = content.findIndex(s => s.includes(start));
const endIndex = content.findIndex(s => s.includes(end));
if (startIndex === -1) throw new Error('Day not found');
const menus = content.slice(startIndex + 1, endIndex !== -1 ? endIndex : undefined)
.map(s => s.replace(/<\/?[^>]+(>|$)|( )/g, '')).filter(s => s.trim()) // Clean and remove empty rows
.map(s => s.replace(/&#(\d+);/g, (_, $1) => String.fromCharCode($1))); // Decode and replace charCodes, e.g. ” => ”
const answer = {
name: 'eatery',
data: menus
};
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 firstDayNode = $('p:contains(Måndag)');
if (firstDayNode.text().split(' ')[1].trim() !== moment().startOf('isoWeek').format('D/M'))
throw new Error('Wrong week');
const dayNodes = firstDayNode.siblings().toArray().map(e => $(e).text());
dayNodes.unshift(firstDayNode.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 (!['', '–', '—'].includes(node)) menu.push(node);
index++;
node = dayNodes.at(index);
}
const answer = {
name: 'valfarden',
data: menu
};
setInCache(answer);
return answer.data;
},
'ubatshallen': async (m) => {
const result = await fetch('https://www.ubatshallen.se/');
const body = await result.text();
const $ = cheerio.load(body);
const mainNode = $('div .entry-content');
const weekNbr = mainNode.children('h2:contains("Vecka")').text().trim().split(' ').at(-1);
if (weekNbr === undefined || weekNbr !== m.week().toString())
throw new Error('Wrong week');
const node = mainNode.children(`div:contains(${weekdayFirstUpper(m)})`);
if (node.length == 0)
throw new Error('No menu found for current day');
const food = node.text().split(/(Det gröna:)|(Husman:)|(Internationell:)/)
.filter(x => !!x)
.map((x, i) => {
if (i % 2 == 1) return x.slice(0, -1);
return x.trim();
});
food.shift();
if (food.length !== 6)
throw new Error('Parsing failed');
const menu = [{ title: food[0], description: food[1] }, { title: food[2], description: food[3] }, { title: food[4], description: food[5] }];
const answer = {
name: 'ubatshallen',
data: menu
};
setInCache(answer);
return answer.data;
},
'misswang': async (m) => {
const result = await fetch('https://misswang.se/');
const body = await result.text();
const $ = cheerio.load(body);
const node = $('#Lunch p');
const menu = node.toArray()
.map(e => $(e).text())
.slice(1)
.reduce<{ title: string, description: string }[]>((acc, curr, i) => {
if (curr.match(/^\d.+$/)) {
acc.push({ title: curr, description: '' })
} else {
acc.at(-1)!.description += curr;
}
return acc;
}, []);
const answer = {
name: 'misswang',
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 !== null && cacheData.date === m.format('YYYY-MM-DD')) {
res.send(cacheData.content.data);
return;
}
}
res.send(await sources[source](m));
} catch (error) {
console.log(error);
if (error instanceof Error)
res.send({ error: `Error: ${error.message}` });
else
res.send({ error: `Error: ${error}` });
}
});
export default router;