-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservers.js
49 lines (38 loc) · 1.17 KB
/
servers.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
import { log } from 'console'
import https from 'https'
import { write, getRows } from './util.js'
export const fetchServers = () => new Promise(resolve => {
const req = https.request('https://api.invidious.io/instances.json')
req.on('error', async e => {
log(` + error fetching servers (${e.message || e}).`)
process.exit(1)
})
req.on('response', res => {
// TODO string util [1]
let str = ''
res.on('data', d => (str += d.toString('utf8')))
res.on('end', async () => {
const data = JSON.parse(str)
const hosts = data
.filter(i =>
i[1].monitor?.statusClass === 'success' // only include servers that are accessible
)
.map(i => `https://${i[0]}`)
const excluded = data
.map(i => `https://${i[0]}`)
.filter(i => !hosts.includes(i))
write(
`accessible servers: ${hosts.length}/${data.length}
\rexcluded servers: ${excluded.length}/${data.length}`,
0,
getRows(2)
)
if (hosts.length) resolve({ hosts })
else {
log(' + error fetching servers (empty response).')
process.exit(1)
}
})
})
req.end()
})