aboutsummaryrefslogtreecommitdiff
path: root/scrape.js
blob: 45dd0b452904f2b929f972cf43a7d8d725c58401 (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
64
65
66
67
const https = require("https"); const parse = require("./parse.js"); const fs = require("fs"); const events = require("events");

async function urlOpen(path)
{
	return new Promise((resolve, reject) =>
	{
		let req = https.get(path, res =>
		{
			res.on("data", resolve);
		});
	});
	req.on("error", e =>
	{
		console.error(e);
	});
	req.end();
}

async function scrapeFood(url)
{
	let data = await urlOpen(url);
	data = data.toString("utf-8");

	let foodList = [];
	const weekdays = ["su", "ma", "ti", "ke", "to", "pe", "la"];

	let titleTags = ["<title>", "</title>"];
	let foodTags = ["<![CDATA[", "]]>"];
	const getSpan = (data, tags, i = 0) =>
	{
		return [
			parse.find(data, tags[0], i) + tags[0].length,
			parse.find(data, tags[1], i)
		];
	}
	let mainTitle = parse.find(data, titleTags[1]) + titleTags[1].length;
	let titleSpan = getSpan(data, titleTags, mainTitle);
	let foodSpan = getSpan(data, foodTags);

	while (
		   (titleSpan[0] !== -1)
		&& (titleSpan[1] !== -1)
		&& (foodSpan[0] !== -1)
		&& (foodSpan[1] !== -1)
	)
	{
		let title = data.substring(titleSpan[0], titleSpan[1]);
		let food = data.substring(foodSpan[0], foodSpan[1]);

		let weekdayIndex = weekdays.findIndex(val => { return val === title.substring(0, 2); });
		if (weekdayIndex !== -1)
			foodList[weekdayIndex] = [title, food];

		titleSpan = getSpan(data, titleTags, foodSpan[1]);
		foodSpan = getSpan(data, foodTags, titleSpan[1]);
	}

	return foodList;
}

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

exports.food = scrapeFood;
exports.link = getFoodLink;