tox-weechat/misc/getnodes.py
2017-02-18 22:29:04 -08:00

55 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Håvard Pettersson <mail@haavard.me>.
#
# This file is part of Tox-WeeChat.
#
# Tox-WeeChat is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tox-WeeChat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
#
import re
import sys
import json
import datetime
import textwrap
import urllib.request
if __name__ == '__main__':
with urllib.request.urlopen('https://nodes.tox.chat/json') as response:
data = json.load(response)
# get online nodes that are defined by IP address
nodes = [node for node in data['nodes']
if (node['status_tcp'] or node['status_udp'])
and re.match(r'^\d+\.\d+\.\d+\.\d+$', node['ipv4'])]
# extract relevant values from node dictionaries
addresses = [node['ipv4'] for node in nodes]
ports = [node['port'] for node in nodes]
keys = [node['public_key'] for node in nodes]
comments = ['Maintainer: {}, location: {}'.format(
node['maintainer'], node['location']) for node in nodes]
# emit C code
print('/* bootstrap nodes generated by', __file__)
print(' * last generated', datetime.datetime.now().isoformat(), '*/')
print('static struct t_twc_bootstrap_node const twc_bootstrap_nodes[] = {')
for key, address, port, comment in zip(keys, addresses, ports, comments):
print(' /* {} */'.format(comment))
print(' {{"{}",'.format(key))
print(' "{}", {}}},'.format(address, port))
print('};')