aboutsummaryrefslogtreecommitdiff
path: root/scrape.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 /scrape.js
parent01f3f5f2ab89432a253c24f76227b8f6855d8446 (diff)
downloadLYLLRuoka-5ac7049a9d30733165cc212dee308163c2a14644.tar.gz
LYLLRuoka-5ac7049a9d30733165cc212dee308163c2a14644.zip
Food scraping updation
Diffstat (limited to 'scrape.js')
-rw-r--r--scrape.js75
1 files changed, 0 insertions, 75 deletions
diff --git a/scrape.js b/scrape.js
deleted file mode 100644
index c7a5743..0000000
--- a/scrape.js
+++ /dev/null
@@ -1,75 +0,0 @@
-const https = require("https");
-const parse = require("./dbparse.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 = ["ma", "ti", "ke", "to", "pe", "la", "su"];
-
- 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, neatify(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}`;
-}
-
-function neatify(food)
-{
- return food.replaceAll(")", ")<br>").replaceAll(" :", ":").replaceAll(":", ":<br>");
-}
-
-exports.food = scrapeFood;
-exports.link = getFoodLink;