-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcAPI.js
114 lines (101 loc) · 3.71 KB
/
srcAPI.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
const config = require('./config.json');
async function fetchRunnerID() {
try {
const url = `https://www.speedrun.com/api/v1/profile`;
const response = await fetch(url, {
method: 'GET', headers: {
'Accept': 'application/json', 'X-API-Key': config.SPEEDRUNCOM_API.RUNNER_APIKEY,
},
});
let returnValue = await response.json();
return returnValue.data.id;
} catch (error) {
console.error('Error fetching runner data:', error);
}
}
async function fetchRuns() {
try {
const runnerID = await fetchRunnerID();
if (!runnerID) {
throw new Error('Invalid runner ID received');
}
const url = `https://www.speedrun.com/api/v1/runs?user=${runnerID}`;
const response = await fetch(url, {
method: 'GET', headers: {
'Accept': 'application/json', 'X-API-Key': config.SPEEDRUNCOM_API.RUNNER_APIKEY,
},
});
const value = await response.json();
return value.data;
} catch (error) {
console.error('Error fetching runs:', error);
return [];
}
}
async function processingRuns() {
const runs = await fetchRuns();
let newVerifiedRuns = [];
for (const run of runs) {
if (run.status.status === 'verified') {
const verifiedDate = new Date(run.status['verify-date']);
let currentDate = new Date();
currentDate.setMinutes(currentDate.getMinutes() - 15);
if (verifiedDate >= currentDate) { // If verify date was -15min from current time
const game = await gameById(run.game);
const gameName = game.names.international;
const category = await categoryById(run.category);
const categoryName = category.name;
const time = formatISODuration(run.times.primary);
let currentRun = {
name: gameName, link: run.weblink, categoryName: categoryName, time: time
}
newVerifiedRuns.push(currentRun);
}
}
}
return newVerifiedRuns;
}
async function gameById(id) {
try {
const url = `https://www.speedrun.com/api/v1/games/${id}`;
const response = await fetch(url, {
method: 'GET', headers: {
'Accept': 'application/json', 'X-API-Key': config.SPEEDRUNCOM_API.RUNNER_APIKEY,
},
});
const value = await response.json();
return value.data;
} catch (error) {
console.error('Error fetching game:', error);
}
}
async function categoryById(id) {
try {
const url = `https://www.speedrun.com/api/v1/categories/${id}`;
const response = await fetch(url, {
method: 'GET', headers: {
'Accept': 'application/json', 'X-API-Key': config.SPEEDRUNCOM_API.RUNNER_APIKEY,
},
});
const value = await response.json();
return value.data;
} catch (error) {
console.error('Error fetching category:', error);
}
}
function formatISODuration(duration) {
const regex = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?/;
const matches = duration.match(regex);
const hours = parseInt(matches[1] || 0, 10);
const minutes = parseInt(matches[2] || 0, 10);
const seconds = parseInt(matches[3] || 0, 10);
// Build the formatted string based on available components
let formattedDuration = '';
if (hours > 0) formattedDuration += `${hours}h `;
if (minutes > 0) formattedDuration += `${minutes}min `;
if (seconds > 0) {
formattedDuration += `${seconds}s`;
}
return formattedDuration.trim();
}
module.exports = {processingRuns};