-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-relays.ts
42 lines (36 loc) · 1.26 KB
/
cache-relays.ts
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
/**
* This script downloads the latest list of relay nodes from the awesome-algorand repository,
* translates them to GeoJSON and caches it to the src/layers/relays.geo.json file
*/
import fs from 'fs'
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import * as turf from '@turf/turf'
const __dirname = dirname(fileURLToPath(import.meta.url));
const RELAY_URL = 'https://raw.githubusercontent.com/awesome-algorand/awesome-algorand/stats/.github/scripts/relay_nodes.json'
const RELAY_CACHE = resolve(__dirname, '../src/layers/relays.geo.json')
type RelayRecord = {
"lat": number,
"lon": number,
"countryCode": string,
"country": string,
"city": string,
"domain": string,
"ip": string
}
let data
if( fs.existsSync(RELAY_CACHE) && process.env.FORCE_DOWNLOAD !== 'true' ) {
data = JSON.parse(fs.readFileSync(RELAY_CACHE, 'utf8'))
} else {
data = await fetch(RELAY_URL).then(res => res.json())
data = turf.featureCollection(data.map((relay: RelayRecord)=>{
return turf.point([relay.lon, relay.lat], {
countryCode: relay.countryCode,
name: relay.city,
country: relay.country,
domain: relay.domain,
ip: relay.ip
})
}))
fs.writeFileSync(RELAY_CACHE, JSON.stringify(data, null, 2))
}