aboutsummaryrefslogtreecommitdiff
path: root/scripts/updateDonators.js
blob: f35048454372fa6add370b2699f17c6a64a5e088 (plain)
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
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' }
)