-
Notifications
You must be signed in to change notification settings - Fork 0
/
valfarden.ts
57 lines (48 loc) · 1.75 KB
/
valfarden.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
import { RestaurantMetaProps } from '@devolunch/shared';
import { Page } from 'puppeteer';
export const meta: RestaurantMetaProps = {
title: 'Välfärden',
url: 'https://valfarden.nu/dagens-lunch/',
imageUrl: 'https://valfarden.nu/wp-content/uploads/2015/01/hylla.jpg',
googleMapsUrl: 'https://goo.gl/maps/cLAKuD2B95N8bqr19',
coordinate: {
lat: 55.61123819992324,
lon: 12.994400413711007,
},
};
export const browserScrapeFunction = (page: Page) =>
page.evaluate(() => {
const todaySwedishFormat = new Date()
.toLocaleString('sv-SE', {
weekday: 'long',
})
.toLowerCase();
const lunchNode = [...document.querySelectorAll('p, h2, h3')].find((a) =>
new RegExp(/vecka\s([1-9][0-9]?(\.[0-9]{1,2})?)/).test((a as HTMLElement)?.innerText.toLowerCase()),
);
const weekdays = ['måndag', 'tisdag', 'onsdag', 'torsdag', 'fredag'];
let closestParentElement = lunchNode as HTMLElement;
while (true as const) {
// If we find 3 or more weekdays in the parentElement we're gucci
if (weekdays.filter((w) => closestParentElement?.textContent?.toLowerCase().includes(w))?.length >= 3) {
break;
}
closestParentElement = closestParentElement?.parentElement as HTMLElement;
if (!closestParentElement) {
return [];
}
}
const lunchMenuDiv = lunchNode?.parentNode?.parentNode as HTMLDivElement;
const raw = lunchMenuDiv?.innerText?.split('\n')?.filter((a) => a.trim() && a !== '—');
const todayIndex = raw?.findIndex((a) => a.toLowerCase().includes(todaySwedishFormat));
return [
{
type: 'meat' as const,
title: raw[todayIndex + 1],
},
{
type: 'veg' as const,
title: raw[todayIndex + 2],
},
];
});