update big NGC
This commit is contained in:
parent
675bf1b2b9
commit
870e3125ad
122 changed files with 2945 additions and 768 deletions
|
@ -1,83 +1,49 @@
|
|||
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
|
||||
import random
|
||||
import urllib.request
|
||||
from utils.util import *
|
||||
from PyQt5 import QtNetwork, QtCore
|
||||
import json
|
||||
from PyQt5 import QtNetwork
|
||||
from PyQt5 import QtCore
|
||||
try:
|
||||
import requests
|
||||
except ImportError:
|
||||
requests = None
|
||||
try:
|
||||
import pycurl
|
||||
import certifi
|
||||
from io import BytesIO
|
||||
except ImportError:
|
||||
pycurl = None
|
||||
|
||||
from user_data.settings import get_user_config_path
|
||||
from tests.support_testing import download_url, _get_nodes_path
|
||||
|
||||
DEFAULT_NODES_COUNT = 4
|
||||
global LOG
|
||||
import logging
|
||||
LOG = logging.getLogger('app.'+'bootstrap')
|
||||
|
||||
|
||||
class Node:
|
||||
|
||||
def __init__(self, node):
|
||||
self._ip, self._port, self._tox_key = node['ipv4'], node['port'], node['public_key']
|
||||
self._priority = random.randint(1, 1000000) if node['status_tcp'] and node['status_udp'] else 0
|
||||
|
||||
def get_priority(self):
|
||||
return self._priority
|
||||
|
||||
priority = property(get_priority)
|
||||
|
||||
def get_data(self):
|
||||
return self._ip, self._port, self._tox_key
|
||||
|
||||
|
||||
def generate_nodes(nodes_count=DEFAULT_NODES_COUNT):
|
||||
with open(_get_nodes_path(), 'rt') as fl:
|
||||
json_nodes = json.loads(fl.read())['nodes']
|
||||
nodes = map(lambda json_node: Node(json_node), json_nodes)
|
||||
nodes = filter(lambda n: n.priority > 0, nodes)
|
||||
sorted_nodes = sorted(nodes, key=lambda x: x.priority)
|
||||
if nodes_count is not None:
|
||||
sorted_nodes = sorted_nodes[-DEFAULT_NODES_COUNT:]
|
||||
for node in sorted_nodes:
|
||||
yield node.get_data()
|
||||
|
||||
|
||||
def download_nodes_list(settings):
|
||||
url = 'https://nodes.tox.chat/json'
|
||||
def download_nodes_list(settings, oArgs):
|
||||
if not settings['download_nodes_list']:
|
||||
return
|
||||
return ''
|
||||
url = settings['download_nodes_url']
|
||||
path = _get_nodes_path(oArgs=oArgs)
|
||||
# dont download blindly so we can edit the file and not block on startup
|
||||
if os.path.isfile(path):
|
||||
with open(path, 'rt') as fl:
|
||||
result = fl.read()
|
||||
return result
|
||||
LOG.debug("downloading list of nodes")
|
||||
result = download_url(url, settings._app)
|
||||
if not result:
|
||||
LOG.warn("failed downloading list of nodes")
|
||||
return ''
|
||||
LOG.info("downloaded list of nodes")
|
||||
_save_nodes(result, settings._app)
|
||||
return result
|
||||
|
||||
if not settings['proxy_type']: # no proxy
|
||||
try:
|
||||
req = urllib.request.Request(url)
|
||||
req.add_header('Content-Type', 'application/json')
|
||||
response = urllib.request.urlopen(req)
|
||||
result = response.read()
|
||||
_save_nodes(result)
|
||||
except Exception as ex:
|
||||
log('TOX nodes loading error: ' + str(ex))
|
||||
else: # proxy
|
||||
netman = QtNetwork.QNetworkAccessManager()
|
||||
proxy = QtNetwork.QNetworkProxy()
|
||||
proxy.setType(
|
||||
QtNetwork.QNetworkProxy.Socks5Proxy if settings['proxy_type'] == 2 else QtNetwork.QNetworkProxy.HttpProxy)
|
||||
proxy.setHostName(settings['proxy_host'])
|
||||
proxy.setPort(settings['proxy_port'])
|
||||
netman.setProxy(proxy)
|
||||
try:
|
||||
request = QtNetwork.QNetworkRequest()
|
||||
request.setUrl(QtCore.QUrl(url))
|
||||
reply = netman.get(request)
|
||||
|
||||
while not reply.isFinished():
|
||||
QtCore.QThread.msleep(1)
|
||||
QtCore.QCoreApplication.processEvents()
|
||||
data = bytes(reply.readAll().data())
|
||||
_save_nodes(data)
|
||||
except Exception as ex:
|
||||
log('TOX nodes loading error: ' + str(ex))
|
||||
|
||||
|
||||
def _get_nodes_path():
|
||||
return join_path(curr_directory(__file__), 'nodes.json')
|
||||
|
||||
|
||||
def _save_nodes(nodes):
|
||||
def _save_nodes(nodes, app):
|
||||
if not nodes:
|
||||
return
|
||||
print('Saving nodes...')
|
||||
with open(_get_nodes_path(), 'wb') as fl:
|
||||
with open(_get_nodes_path(oArgs=app._args), 'wb') as fl:
|
||||
LOG.info("Saving nodes to " +_get_nodes_path())
|
||||
fl.write(nodes)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue