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 --- server.js | 68 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index f4e89cf..79fc50c 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,10 @@ //const http = require("http"); const https = require("https"); const url = require("url"); -const scrape = require("./scrape.js"); +const food = require("./food.js"); const SQL_DBS = require("./database.js"); const DBPARSE = require("./dbparse.js"); -const openFile = require("./Functions/open.js").file; +const open = require("./Functions/open.js"); const strFuncs = require("./Functions/stringFuncs.js"); const dateFuncs = require("./Functions/dateFuncs.js"); @@ -27,14 +27,13 @@ async function init() let visitorCount = 0; // await for needed things in async - let [foodsThisWeek, foodsNextWeek, dbcredentials, httpsKey, httpsCert] = await Promise.all([ - scrape.food(scrape.link(1)), - scrape.food(scrape.link(2)), - openFile("../dblogin.txt"), - openFile("../Certificate/key.pem"), - openFile("../Certificate/cert.pem") + let [dbcredentials, httpsKey, httpsCert] = await Promise.all([ + open.file("../dblogin.txt"), + open.file("../Certificate/key.pem"), + open.file("../Certificate/cert.pem") ]); - + + // https options, you need to get a certificate in the file ../Certificate for the server to work const httpsOpts = { key: httpsKey, @@ -44,8 +43,15 @@ async function init() // get the MySQL DB connection const SQLDB = new SQL_DBS.Database(JSON.parse(dbcredentials)); - // get the food "database" - const foods = [foodsThisWeek, foodsNextWeek]; + // Add the foods to the database + await food.build(SQLDB); + setInterval( + () => + { + food.build(SQLDB); + }, + 7 * 24 * 60 * 60 * 1000 + ); // server code async function server(req, res) @@ -79,7 +85,6 @@ async function init() "path": path, "path404": errorPath, "query": q.query, - "foods": foods, "sqldb": SQLDB }; @@ -157,10 +162,9 @@ async function buildMain(args) // get the passed arguments const path = args["path"]; const query = args["query"]; - const foods = args["foods"]; const index = query.index; const SQLDB = args["sqldb"]; - const data = await openFile(path); + const data = await open.file(path); let data_string = data.toString("utf-8"); // here are the things to replace in the html page @@ -242,19 +246,21 @@ async function buildMain(args) data_string = data_string.replace('
', '