aboutsummaryrefslogtreecommitdiff
path: root/food.js
blob: cce4ef4e8305df716b01fd18cb2be8feff84d788 (plain)
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
const parse = require("./dbparse.js");
const open = require("./Functions/open.js");
const { weekdayToNumber } = require("./Functions/dateFuncs.js");

function* scrapeFood(data)
{
	const foodRegex = /<title>(\w{2} (?:\d\d?\.){2}\d{4})<\/title><description><!\[CDATA\[(Lounas) ?:? ?(.*?)(Kasvislounas) ?:? ?(.*?)]]><\/description>/gm;
	const foods = data.matchAll(foodRegex);
	for(const food of foods)
	{
		yield [
			weekdayToNumber(food[1]),	// index
			food[1],	// header (date)
			[food[2], food[3]],	// first header, first food
			[food[4], food[5]]	// second header, second food
		];
	}
}

async function buildFoods(DB)
{
	await DB.query_raw("DELETE FROM foods");
	let foodData = await Promise.all([
		open.url(getFoodLink(1)),
		open.url(getFoodLink(2))
	]);
	foodData = foodData.map((f) => f.toString("utf-8"));
	const foodInitOperations = [];
	const foods = foodData.map(f => scrapeFood(f));
	for(let week = 1; week <= 2; week++)
	{
		for(const food of foods[week - 1])
		{
			foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [
				week,
				food[0],
				food[2][0],
				food[1],
				food[2][1]
			]));
			foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [
				week,
				food[0],
				food[3][0],
				food[1],
				food[3][1]
			]));
		}
	}
	await Promise.all(foodInitOperations);
	console.log("Foods updated.")
	return 0;
}

function getFoodLink(week)
{
	return `https://eruokalista.lohja.fi/AromieMenus/FI/Default/Lohja/Koulut/Rss.aspx?Id=97f76449-f57c-4217-aede-b5f9dbf2b41e&DateMode=${week}`;
}


exports.foods = scrapeFood;
exports.link = getFoodLink;
exports.build = buildFoods;