aboutsummaryrefslogtreecommitdiff
path: root/food.js
diff options
context:
space:
mode:
authorJoel Kronqvist <work.joelkronqvist@pm.me>2022-03-11 20:41:42 +0200
committerJoel Kronqvist <work.joelkronqvist@pm.me>2022-03-11 20:41:42 +0200
commit5ac7049a9d30733165cc212dee308163c2a14644 (patch)
tree92dbb85e2e3dc408d06b8cea6fbf32503482bbbb /food.js
parent01f3f5f2ab89432a253c24f76227b8f6855d8446 (diff)
downloadLYLLRuoka-5ac7049a9d30733165cc212dee308163c2a14644.tar.gz
LYLLRuoka-5ac7049a9d30733165cc212dee308163c2a14644.zip
Food scraping updation
Diffstat (limited to 'food.js')
-rw-r--r--food.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/food.js b/food.js
new file mode 100644
index 0000000..bd2ae75
--- /dev/null
+++ b/food.js
@@ -0,0 +1,61 @@
+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 INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [
+ week,
+ food[0],
+ food[2][0],
+ food[1],
+ food[2][1]
+ ]));
+ foodInitOperations.push(DB.execute("INSERT INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [
+ week,
+ food[0],
+ food[3][0],
+ food[1],
+ food[3][1]
+ ]));
+ }
+ }
+ await Promise.all(foodInitOperations);
+}
+
+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; \ No newline at end of file