-
Notifications
You must be signed in to change notification settings - Fork 1
/
gethistorynow.js
160 lines (134 loc) · 4.63 KB
/
gethistorynow.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
const cron = require('node-cron');
const express = require('express');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const fs = require('fs');
const myTeam = 'shp-chess-club';
let mbrs = null;
let mbrHistory = [];
let ratingPromises = [];
let fullData = [];
let delay = 0;
const delayIncrement = 1000;
app = express();
function ndjsonToArray(ret) {
let retArray = ret.split('\n');
ret = [];
for (let mbr of retArray) {
if (mbr.length > 2) ret.push(JSON.parse(mbr));
}
return ret;
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
var getTeamMembers = function (team) {
console.log('getting team members...');
return new Promise((resolve, reject) => {
var xhttp = new XMLHttpRequest();
// console.log("here");
xhttp.open("GET", "https://lichess.org/api/team/" + myTeam + "/users", true);
xhttp.onload = function () {
if (this.readyState == 4 && this.status == 200) {
// console.log(xhttp.responseText);
// let ret = JSON.parse(xhttp.responseText);
let ret = ndjsonToArray(xhttp.responseText);
resolve(ret);
}
};
xhttp.onerror = () => {
console.log(xhttp.status);
console.log(xhttp.statusText);
reject({
status: xhttp.status,
statusTest: xhttp.statusText
});
};
xhttp.send();
});
}
var getMemberRating = (member, idx) => {
return new Promise(async function (resolve) {
// only fetch from lichess API if we don't already have that member's history
if (mbrHistory[member] != null) {
resolve(mbrHistory[member]);
return;
}
console.log('before sleep');
sleep(1000);
// console.log('after sleep');
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://lichess.org/api/user/" + member + "/rating-history", true);
xhttp.onload = function () {
if (this.readyState == 4 && this.status == 200) {
var ret = JSON.parse(xhttp.responseText);
mbrHistory[member] = {
member: member,
history: ret
};
console.log('before resolve');
resolve(mbrHistory[member]);
}
};
xhttp.onerror = () => {
reject({
status: xhttp.status,
statusText: xhttp.statusText
});
};
xhttp.send();
});
}
getTeamMembers().then((users) => {
console.log('updating data...');
for (let i = 0; i < users.length; i++) {
delay = 1000 * i;
// console.log(ret[i]['member']);//getMemberRating(ret[i]['id'], i)
let thismember = users[i]['id'];
// console.log(thismember);
ratingPromises.push(new Promise(async function (resolve) {
await new Promise(res => setTimeout(res, delay));
let result = await new Promise(r => {
if (mbrHistory[thismember] != null) {
resolve(mbrHistory[thismember]);
return;
}
// console.log('before sleep');
sleep(1000);
// console.log('after sleep');
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://lichess.org/api/user/" + thismember + "/rating-history", true);
xhttp.onload = function () {
if (this.readyState == 4 && this.status == 200) {
var rText = JSON.parse(xhttp.responseText);
mbrHistory[thismember] = {
member: thismember,
history: rText
};
console.log('updated: ' + thismember);
resolve(mbrHistory[thismember]);
}
};
xhttp.onerror = () => {
reject({
status: xhttp.status,
statusText: xhttp.statusText
});
};
xhttp.send();
});
resolve(result);
}));
}
Promise.all(ratingPromises).then((e) => {
// console.log(e);
let data = JSON.stringify(e);
fs.writeFileSync('member-data.json', data);
console.log('Done');
});
// console.log(ret);
});
app.listen(3001);