-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload-sessions.js
55 lines (52 loc) · 2.04 KB
/
download-sessions.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
async function GetSessionsForRustServer(serverId, startTime, stopTime) {
const url = `https://api.battlemetrics.com/servers/${serverId}/relationships/sessions?start=${startTime.toISOString()}&stop=${stopTime.toISOString()}`;
let response;
try {
await Sleep(200);
response = await FetchUrl(url);
} catch (e) {
console.log('Error while downloading sessions for server', serverId);
return [];
}
let sessions = response.data;
if (sessions.length > 4000) {
console.log('Splitting download because too many sessions.');
const mid = new Date((startTime.getTime() + stopTime.getTime()) / 2);
await GetSessionsForRustServer(serverId, startTime, mid);
await GetSessionsForRustServer(serverId, mid, stopTime);
} else {
for (const session of sessions) {
const startTimeUnix = new Date(session.attributes.start).getTime();
const stopTimeUnix = new Date(session.attributes.stop).getTime();
const values = [
session.id,
session.relationships.server.data.id,
session.attributes.name,
startTimeUnix,
stopTimeUnix,
stopTimeUnix - startTimeUnix,
];
db.run(updateSessionSql, values, DatabaseCallback);
}
console.log('Wrote', sessions.length, 'session records.');
}
}
async function main() {
const servers = await GetAllRustServers();
// Uncomment the next line to crawl the servers from the smallest to largest.
//servers.reverse();
//const startTime = new Date(dateString + 'T00:00:00.000Z');
//const stopTime = new Date(startTime.getTime() + 86400 * 1000); // Add 24h.
const startTime = new Date('2022-04-16T00:00:00.000Z');
const stopTime = new Date('2022-07-12T00:00:00.000Z');
// Crawl the servers from smallest to largest.
console.log('Number of Rust servers found', servers.length);
let serverCount = 0;
for (const server of servers) {
serverCount++;
console.log(serverCount, 'of', servers.length, server.id, server.attributes.name);
await GetSessionsForRustServer(server.id, startTime, stopTime);
}
db.close();
}
main();