From 5ac7049a9d30733165cc212dee308163c2a14644 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Fri, 11 Mar 2022 20:41:42 +0200 Subject: Food scraping updation --- scrape.js | 75 --------------------------------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 scrape.js (limited to 'scrape.js') 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 = ["", ""]; - let foodTags = [""]; - 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(")", ")
").replaceAll(" :", ":").replaceAll(":", ":
"); -} - -exports.food = scrapeFood; -exports.link = getFoodLink; -- cgit v1.2.3