You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB
Python

#!/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]
pubkeys = [node['public_key'] for node in nodes]
comments = ['Maintainer: {}, location: {}'.format(
node['maintainer'], node['location']) for node in nodes]
# format data as C literals
addresses_c = ', '.join('"{}"'.format(address) for address in addresses)
ports_c = ', '.join('{}'.format(port) for port in ports)
pubkeys_c = ',\n '.join('/* {} */\n "{}"'.format(
comment, key) for comment, key in zip(comments, pubkeys))
# word-wrap and indent addresses and ports
addresses_c = textwrap.fill(addresses_c).replace('\n', '\n ')
ports_c = textwrap.fill(ports_c).replace('\n', '\n ')
# emit C code
print('/* bootstrap nodes generated by {}, do not modify'.format(__file__))
print(' * last generated', datetime.datetime.now().isoformat())
print(' */')
print('char *twc_bootstrap_keys[] = {')
print(' '*3, pubkeys_c, ' };\n')
print('char *twc_bootstrap_addresses[] = {')
print(' '*3, addresses_c, ' };\n')
print('uint16_t twc_bootstrap_ports[] = {')
print(' '*3, ports_c, ' };')