-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_dmoj.js
80 lines (71 loc) · 2.38 KB
/
grab_dmoj.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
var Discord = require('discord.js');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var auth = require('/users/jason/logins.json');
var dmoj = require('./dmoj.json');
async function get_http(url) {
console.log('Getting: '.concat(url));
await new Promise(resolve => setTimeout(resolve, 1000));
var response = [];
var xhttp = new XMLHttpRequest();
xhttp.open('GET', url, false);
xhttp.setRequestHeader("Authorization", `Bearer ${auth.dmoj_token}`);
xhttp.onload = function() {
response = JSON.parse(xhttp.responseText);
};
xhttp.onerror = function() {
message.channel.send(`\`\`\`Something went wrong :(\`\`\``);
reponse = null;
};
xhttp.send();
return response;
}
async function asyncForEach(arr, func) {
for (var i in arr) {
await func(arr[i]);
}
}
async function main() {
console.log('Grabbing DMOJ');
// Grab Problem Names
await asyncForEach(dmoj.problems, async problem => {
var response = await get_http(`https://dmoj.ca/api/v2/problem/${problem.id}`);
if (response !== undefined && 'data' in response) {
problem.name = response.data.object.name;
problem.pp = response.data.object.points;
}
});
// Grab Leaderboard and Problem Board
dmoj.leaderboard = [];
dmoj.problem_board = [];
await asyncForEach(dmoj.users, async user => {
var response = await get_http(`https://dmoj.ca/api/v2/user/${user}`);
if (response !== undefined && 'data' in response) {
dmoj.leaderboard.push({"user": user, "pp": parseFloat(response.data.object.performance_points)});
dmoj.problem_board.push({'user': user, 'pp': 0});
await asyncForEach(dmoj.problems, async problem => {
if (response.data.object.solved_problems.includes(problem.id)) {
dmoj.problem_board[dmoj.problem_board.length - 1].pp += problem.pp;
}
});
}
});
dmoj.leaderboard.sort(function(a, b) {
if (a.pp < b.pp) return 1;
if (a.pp > b.pp) return -1;
return 0;
});
dmoj.problem_board.sort(function(a, b) {
if (a.pp < b.pp) return 1;
if (a.pp > b.pp) return -1;
return 0;
});
require('fs').readFile('dmoj.json', 'utf8', (err, data) => {
new_users = JSON.parse(data).users;
dmoj.users = new_users;
require('fs').writeFile('dmoj.json', JSON.stringify(dmoj, null, 4), function writeJSON(err) {
if (err) return console.log(err);
console.log('Saving DMOJ');
});
});
}
main();