Skip to content

Instantly share code, notes, and snippets.

@ZaWertun
Created May 4, 2016 11:12
Show Gist options
  • Save ZaWertun/953c4b3d8e95dde87535711f424ba860 to your computer and use it in GitHub Desktop.
Save ZaWertun/953c4b3d8e95dde87535711f424ba860 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import urllib.request
rows = []
max_col_size = {0: 0, 1: 0, 2: 0}
nodes = json.loads(
urllib.request.urlopen("https://nodes.tox.chat/json").read().decode()
)['nodes']
for node in nodes:
key = node['public_key']
tcp = node['status_tcp']
udp = node['status_udp']
port = node['port']
addr_ipv4 = node['ipv4']
if tcp or udp:
rows.append(
(
"address=\"{0}\",".format(addr_ipv4),
"port={0},".format(port),
"public_key=\"{0}\"".format(key)
)
)
for row in rows:
for col in row:
size = len(col)
index = row.index(col)
if size > max_col_size[index]:
max_col_size[index] = size
rows.sort(key = lambda x: x[0])
last = len(rows) - 1
for index, row in enumerate(rows):
print(' {{ {0} {1} {2} }}{3}'.format(
row[0].ljust(max_col_size[0]),
row[1].ljust(max_col_size[1]),
row[2].ljust(max_col_size[2]),
'' if (index == last) else ','
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment