-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
executable file
·168 lines (145 loc) · 4.9 KB
/
api.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import express from 'express';
const app = express();
const options = {
method: 'GET',
headers: {
'User-Agent':
'Mozilla/5.0 (Linux; Android 9; ASUS_X00TD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 Mobile Safari/537.36',
Accept: '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
DNT: '1',
Connection: 'keep-alive',
Referer: `${process.env.BASE_URL}/booking`,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
TE: 'trailers',
},
};
const scan = async queryParams => {
const response = await fetch(
`${process.env.BASE_URL}/api/booking/feed?${queryParams}`,
options,
);
const bookings = await response.json();
return bookings.map(booking => {
const { start, end, resourceId } = booking;
const startDate = new Date(start);
const endDate = new Date(end);
return { startDate: startDate, endDate: endDate, court: resourceId };
});
};
const dateToYMD = date => {
const d = date.getDate();
const dd = new String(d).padStart(2, '0');
const m = date.getMonth() + 1; //Month from 0 to 11
const mm = new String(m).padStart(2, '0');
const y = date.getFullYear();
return `${y}-${mm}-${dd}`;
};
const run = async (bookings, hour) => {
const today = dateToYMD(new Date()); // today yyyy-mm-dd
const availableBookings = [];
for (let court = 1; court <= 12; court++) {
// 14th day was too noisy
for (let i = 0; i < 14; i++) {
if (i === 0 && new Date().getHours() >= hour) {
// Skip today if after hour
continue;
}
if (i === 14 && new Date().getHours() < 17) {
// Skip the 14 days ahead date if it is before 5pm today (unbookable)
continue;
}
const targetStartDate = new Date(`${today}T${hour}:00`);
targetStartDate.setDate(targetStartDate.getDate() + i);
if ([5, 6, 0].includes(targetStartDate.getDay())) {
// Skip Friday, Saturday, Sunday
continue;
}
const targetEndDate = new Date(targetStartDate);
targetEndDate.setHours(hour + 1);
const filteredBookings = bookings.filter(
b =>
b.court === court &&
dateToYMD(targetStartDate) === dateToYMD(b.startDate),
);
if (!filteredBookings.length) {
continue;
}
const isBooked = filteredBookings.some(b => {
const targetStartsDuringExistingBooking =
targetStartDate >= b.startDate && targetStartDate < b.endDate;
const targetEndsDuringExistingBooking =
targetEndDate > b.startDate && targetEndDate <= b.endDate;
const targetWrapsExistingBooking =
targetStartDate <= b.startDate && targetEndDate >= b.endDate;
return (
targetStartsDuringExistingBooking ||
targetEndsDuringExistingBooking ||
targetWrapsExistingBooking
);
});
// Mon, 25 Dec
const readableDate = targetStartDate.toLocaleDateString('en-GB', {
weekday: 'short',
month: 'short',
day: 'numeric',
});
// 20:00
const readableTime = targetStartDate.toLocaleTimeString('en-GB', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
});
if (!isBooked) {
availableBookings.push({
court: court,
date: targetStartDate,
readableString: `${readableDate} ${readableTime}`,
});
}
}
}
return availableBookings;
};
app.get('/', async (_, res) => {
const today = dateToYMD(new Date()); // today yyyy-mm-dd
const fortnightAhead = new Date();
fortnightAhead.setDate(fortnightAhead.getDate() + 15); // Add 15 days because API returns dates before "end"
const fortnight = dateToYMD(fortnightAhead);
const params = new URLSearchParams();
params.set('start', today);
params.set('end', fortnight);
const bookings = await scan(params.toString());
const fiveToSixBookings = await run(bookings, 17);
const sixToSevenBookings = await run(bookings, 18);
const allAvailableBookings = fiveToSixBookings
.concat(sixToSevenBookings)
.sort((a, b) => {
if (a.date < b.date) {
return -1;
} else if (a.date > b.date) {
return 1;
} else {
return a.court - b.court;
}
})
.map(ab => ({ court: ab.court, date: ab.readableString }));
const groupedAvailableBookings = allAvailableBookings.reduce(
(map, element) =>
map.set(element.date, [...(map.get(element.date) || []), element.court]),
new Map(),
);
res.send(
[...groupedAvailableBookings].map(group => ({
date: group[0],
courts: group[1],
})),
);
});
app.listen(8388, () => {
console.log('Server started on port 8388');
});