diff options
Diffstat (limited to 'scripts/updateDonators.js')
-rw-r--r-- | scripts/updateDonators.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/scripts/updateDonators.js b/scripts/updateDonators.js new file mode 100644 index 0000000..f350484 --- /dev/null +++ b/scripts/updateDonators.js @@ -0,0 +1,49 @@ +import fs from 'fs' +import https from 'https' + +const API_URL = 'https://skyblock-api.matdoes.dev/' + +function fetch(url) { + return new Promise((resolve, reject) => { + let data = '' + + const req = https.request(new URL(url), res => { + res.on('data', d => { + data += d + }) + + res.on('end', () => { + resolve(data) + }) + }) + + req.on('error', error => { + reject(error) + }) + + req.end() + }) +} +function shuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + ;[a[i], a[j]] = [a[j], a[i]] + } + return a +} + +// create a donators.json from the donators.txt +const donatorUuidsText = await fs.promises.readFile('src/donators.txt', { + encoding: 'utf8' +}) +const donatorUuids = donatorUuidsText.split('\n').map(u => u.split(' ')[0]).filter(u => u) +const donators = await Promise.all( + donatorUuids.map(u => fetch(`${API_URL}player/${u}`) + .then(r => JSON.parse(r).player) + ) +) +await fs.promises.writeFile( + 'src/_donators.json', + JSON.stringify(shuffle(donators)), + { encoding: 'utf8' } +) |