Initial commit
This commit is contained in:
53
src/index.ts
Normal file
53
src/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import * as express from "express";
|
||||
import fetch from "node-fetch";
|
||||
import {slice} from "./utils";
|
||||
|
||||
const app = express();
|
||||
const data = {
|
||||
dayCount: 500000,
|
||||
firstVac: 1400000,
|
||||
secondVac: 5000000,
|
||||
finalDate: new Date()
|
||||
};
|
||||
|
||||
(async function fetchData() {
|
||||
const res = await fetch("https://impfdashboard.de/static/data/germany_vaccinations_timeseries_v2.tsv");
|
||||
const html = await res.text();
|
||||
const arrayOfLines = html.split(/[\r\n]+/);
|
||||
let sumOfTodays = 0;
|
||||
let countDays = 0;
|
||||
for (const line of slice(arrayOfLines, arrayOfLines.length-8)) {
|
||||
const arr = line.split(/\s+/);
|
||||
if (arr.length < 10) continue;
|
||||
if (!Number.isNaN(+(arr[2]))) {
|
||||
sumOfTodays += +(arr[2]);
|
||||
countDays++;
|
||||
}
|
||||
data.firstVac = +(arr[5]) || data.firstVac;
|
||||
data.secondVac = +(arr[9]) || data.secondVac;
|
||||
}
|
||||
data.dayCount = sumOfTodays / countDays;
|
||||
const daysLeft = (83703925*2 - data.firstVac - data.secondVac) / data.dayCount * 0.8;
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + daysLeft * 24 * 60 * 60 * 1000);
|
||||
data.finalDate = date;
|
||||
setTimeout(fetchData, 1000*60*60);
|
||||
})()
|
||||
|
||||
app.set('view engine', 'pug')
|
||||
app.set('views', './views')
|
||||
app.use("/public", express.static("./public"))
|
||||
app.get("/", (req, res) => {
|
||||
res.render("index", data);
|
||||
})
|
||||
app.get("/impressum", (req, res) => {
|
||||
res.render("impressum");
|
||||
})
|
||||
|
||||
const listener = app.listen(
|
||||
3000,
|
||||
() => {
|
||||
const addr = listener.address();
|
||||
console.log(`App listening on port ${typeof addr === "object" ? addr.port : addr}!`);
|
||||
}
|
||||
)
|
||||
12
src/utils.ts
Normal file
12
src/utils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function* slice(src, start, finish = src.length) {
|
||||
let index = 0;
|
||||
for (const value of src) {
|
||||
if (index >= finish) {
|
||||
return;
|
||||
}
|
||||
if (index >= start) {
|
||||
yield value;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user