-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (86 loc) · 2.91 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require("express");
const app = express();
const moment = require("moment");
const puppeteer = require('puppeteer');
const cors = require("cors");
const mongoose = require("mongoose");
const { logPup } = require("./logger");
require("dotenv").config()
const Days = mongoose.model("days", new mongoose.Schema({
date: String,
list: Array,
}))
mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_IP}/sspsag-suplovani?authSource=admin`, { useNewUrlParser: true });
mongoose.connection.on("connected", async () => {
console.log("Server connected to database.");
});
app.use(cors());
const getData = async () => {
const ulr = "https://ssps.cz/";
logPup("Starting browser.");
const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']});
logPup("Browser started, creating page.");
const page = await browser.newPage();
logPup("Page created, opening url.");
await page.goto(ulr);
let titles = await page.$$eval(".supplementation-report strong", els => els.map(el => el.innerText));
const lists = await page.$$eval(".supplementation-report ul", els => els.map(el => el.innerText));
const data = [];
for (let i = 0; i < titles.length; i++) {
let title = titles[i];
let date = title.split(" ").slice(3).join(" ");
date = moment(date, "D. M. YYYY").toISOString();
data.push({
date,
list: null
});
}
for (let i = 0; i < lists.length; i++) {
const suplovani = lists[i].split("\n").map(item => item.split("").slice(1).join(""));
data[i].list = [];
for (let j = 0; j < suplovani.length; j++) {
let content = suplovani[j].split(" ");
let text = content.slice(1).join(" ");
let grade = content[0];
let hour = content[1].split(",").map(item => parseInt(item.slice(0, item.indexOf("."))));
let type = "other";
if (content.includes("supluje")) type = "supl";
if (content.includes("odpadá")) type = "odp";
data[i].list[j] = { text, grade, hour, type };
}
}
await browser.close();
logPup("Browser closed.");
for (let i = 0; i < data.length; i++) {
const day = data[i];
//Find if day is in database
const dayInDb = await Days.findOne({ date: day.date });
if (dayInDb) {
//Update day
dayInDb.list = day.list;
await dayInDb.save();
} else {
//Create day
await Days.create(day);
}
}
logPup("Data updated.");
};
getData();
const getDataEvery5Minutes = () => {
const now = moment();
const hour = now.hour() + 1;
const minute = now.minute();
if (hour < 5 || hour > 20) return;
if (minute % 5 === 0) {
logPup("Running getData.");
getData();
}
};
getDataEvery5Minutes();
setInterval(getDataEvery5Minutes, 15000);
app.get("/suplovani", async (req, res) => {
const days = await Days.find().sort({ date: -1 }).limit(5);
res.send(days);
});
app.listen(6574, () => console.log("Server is running on 6574"))