-
Notifications
You must be signed in to change notification settings - Fork 545
/
topology.py
64 lines (54 loc) · 2.04 KB
/
topology.py
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
#!/usr/bin/python3
import requests
import json
import time
import argparse
parser = argparse.ArgumentParser(description='''Generate CSPA relay metadata''')
parser.add_argument('--output', help="output", required=True)
args = parser.parse_args()
API_URL= "http://change.this:3100/graphql"
try:
r = requests.get('https://raw.githubusercontent.com/SinglePoolAlliance/Registration/master/registry.json')
except requests.exceptions.RequestException as e:
raise SystemExit(e)
pools = json.loads(r.text)
spa_relays = []
for pool in pools:
headers = {'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/json', 'Accept': 'application/json', 'Connection': 'keep-alive', 'DNT': '1', 'Origin': 'http://localhost:3100'}
data = {"query":'''{ stakePools ( where:
{ hash:
{ _eq: "''' + pool['poolId'] +'''"}} ) { relays { ipv4 dnsName port} } }'''}
while True:
try:
r = requests.post(API_URL, data=json.dumps(data), headers=headers)
except:
print ("[err] got an exception while querying graphql...retrying")
time.sleep(1)
pass
else:
break
struct_query = json.loads(r.text)
while r.status_code != 200 or "errors" in struct_query:
print ("[err] got an error while querying graphql...retrying")
time.sleep(1)
while True:
try:
r = requests.post(API_URL, data=json.dumps(data), headers=headers)
except:
print ("[err] got an exception while querying graphql...retrying")
time.sleep(5)
pass
else:
break
struct_query = json.loads(r.text)
print("[debug] {0}".format(r.text.strip()))
relays = json.loads( r.text)['data']['stakePools'][0]['relays']
for relay in relays:
if relay['dnsName'] != None:
spa_relays.append({"addr": relay['dnsName'], "port": relay['port'], "valency": 1 })
break
elif relay['ipv4'] != None:
spa_relays.append({"addr": relay['ipv4'], "port": relay['port'], "valency": 1 })
break
with open(args.output, 'w') as f:
json.dump(spa_relays, f, indent=4, sort_keys=True)