-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (70 loc) · 2.74 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
const express = require("express");
const fetch = require('node-fetch');
const app = express();
const HTMLParser = require('node-html-parser');
const PORT = process.env.PORT || 1987;
const COOL_NAMES = ['Christian', 'Marlon', 'Nadine', 'Peter', 'Hendrik', 'Dawid', 'Valentin', 'Thorben', 'Ann-Kathrin'];
app.listen(PORT, () => {
console.log("Server running on port 1987");
});
app.get("/", (req, res, next) => {
res.send('läuft');
});
function formatDate(d) {
let date = new Date(d);
let month = ('0' + (date.getMonth() + 1)).substring(-2);
let day = ('0' + date.getDate()).substring(-2);
return `${month}${day}`;
}
function findCommonElements(arr1, arr2) {
return arr1.some(item => arr2.indexOf(item) >= 0);
}
let cache = {};
app.get("/no-not-na-day-n", (req, res, next) => {
const route = 'https://welcher-tag-ist-heute.org';
let startTime = new Date().getMilliseconds();
// get current day to check if it's already cached
let currentDay = formatDate(Date.now());
if (currentDay in cache) {
let totalExecutionTime = new Date().getMilliseconds() - startTime;
let result = cache[currentDay];
result.fromCache = true;
result.executionTime = totalExecutionTime;
res.json(result);
} else {
fetch(route)
.then(res => res.text())
.then(body => {
let parsedBody = HTMLParser.parse(body);
let slides = parsedBody.querySelectorAll('.slides a');
let result = [];
let days = [];
if (slides) {
slides.forEach(slide => {
let title = slide.querySelector('h2').innerHTML.trim();
let description = slide.querySelector('p.text').innerHTML.trim();
days.push({
title: title,
description: description
});
});
}
let names = parsedBody.querySelector('#name p.text').innerHTML.trim().replace(/, /gi, ',').split(',');
let hurray = 'Kein cooler Namenstag heute :(';
if (findCommonElements(COOL_NAMES, names)) {
hurray = 'Jemand hat heute einen Namenstag!!!';
}
let totalExecutionTime = new Date().getMilliseconds() - startTime;
result = {
executionTime: totalExecutionTime,
fromCache: false,
result: days,
names: names,
hurray: hurray
};
// save to cache
cache[currentDay] = result;
res.json(result);
});
}
});