-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
136 lines (127 loc) · 3.8 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const Themeparks = require("themeparks");
const express = require("express");
const app = express();
const db = require("knex")({
client: "pg",
connection: process.env.DATABASE_URL,
});
app.set("db", db);
const DisneyWorldMagicKingdom =
new Themeparks.Parks.DisneylandParisMagicKingdom();
const CheckWaitTimes = () => {
return DisneyWorldMagicKingdom.GetWaitTimes()
.then((rideTimes) => {
const currentHour = Number(
new Date().toLocaleTimeString([], {
hour: "2-digit",
timeZone: "Europe/Paris",
hour12: false,
})
);
const currentMinutes = Number(
new Date().toLocaleTimeString([], {
minute: "2-digit",
timeZone: "Europe/Paris",
hour12: false,
})
);
console.log(currentHour, currentMinutes);
if (currentHour < 9) {
return (9 - currentHour) * 60 - currentMinutes;
}
if (currentHour >= 22 && currentMinutes > 0) {
return 9 * 60 + (24 - currentHour) * 60 - currentMinutes;
}
if (currentMinutes % 5 !== 0) {
return (60 - currentMinutes) % 5;
}
rideTimes
.filter((_, index) => index > 32)
.forEach((ride, index) => {
db("disneyapi")
.returning(["id", "name", "wait", "status", "time", "hour"])
.insert({
id: index,
name: ride.name,
wait:
ride.status === "Closed" || ride.status === "Down"
? null
: ride.waitTime,
status: ride.status,
time: new Date().toLocaleString("en-US", {
timeZone: "Europe/Paris",
}),
hour: new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
timeZone: "Europe/Paris",
hour12: false,
}),
})
.then(() => console.log("Row was added"))
.catch((error) => console.error(error));
});
return 5;
})
.catch((error) => {
console.error(error);
})
.then((next) => {
console.log(`Running next in ${next} minutes`);
setTimeout(CheckWaitTimes, 1000 * 60 * next);
});
};
CheckWaitTimes();
app.get("/data", (req, res) => {
db.select()
.from("disneyapi")
.then((disneyapi) => res.send(disneyapi));
});
app.get("/avg", (req, res) => {
db.select("id")
.distinct()
.from("disneyapi")
.then((results) => {
const idValues = results.map((row) => row.id);
let data = {};
idValues.forEach((id) => {
db.select("id", "hour", "name")
.avg("wait as avg_waiting_time")
.from("disneyapi")
.where({ id: id })
.groupBy("id", "hour", "name")
.orderBy("id", "asc")
.orderBy("hour", "asc")
.then((results) => {
results.forEach((row) => {
data[(row.id, row.name)] = data[(row.id, row.name)] || {};
data[(row.id, row.name)][row.hour] =
Math.round(Number(row.avg_waiting_time)) || null;
});
if (Object.keys(data).length === idValues.length) {
res.send(data);
}
});
});
});
});
app.get("/avg/:number", (req, res) => {
let data = {};
let number = req.params.number;
db.select("id", "hour", "name")
.avg("wait as avg_waiting_time")
.from("disneyapi")
.where({ id: number })
.groupBy("id", "hour", "name")
.orderBy("hour", "asc")
.then((results) => {
results.forEach((row) => {
data[(row.id, row.name)] = data[(row.id, row.name)] || {};
data[(row.id, row.name)][row.hour] = Math.round(
Number(row.avg_waiting_time)
);
});
res.json(data);
});
});
app.listen(process.env.PORT);