Changeset - 9e4fba8ebb3b
[Not reviewed]
Dennis Fink - 9 years ago 2016-03-02 19:32:50
dennis.fink@c3l.lu
Use onionpy for querying onionoo
3 files changed with 17 insertions and 24 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/model.py
Show inline comments
 
@@ -24,10 +24,11 @@ from datetime import datetime
 

	
 
import jsonschema
 
import strict_rfc3339
 
import requests
 

	
 
from flask import current_app
 
from pkg_resources import resource_filename
 
from onion_py.manager import Manager
 
from onion_py.caching import OnionSimpleCache
 

	
 
from ..utils import check_ip
 

	
 
@@ -45,6 +46,8 @@ validate = functools.partial(
 
    format_checker=jsonschema.FormatChecker()
 
)
 

	
 
manager = Manager(OnionSimpleCache())
 

	
 

	
 
def calculate_weight(data):
 

	
 
@@ -57,9 +60,9 @@ def calculate_weight(data):
 
        except KeyError:
 
            continue
 

	
 
        factor = subdata['factor']
 
        factor = subdata.factor
 

	
 
        values = [x * factor for x in subdata['values'] if x is not None]
 
        values = [x * factor for x in subdata.values if x is not None]
 

	
 
        if values:
 
            obj[subkey] = statistics.mean(values) * 100
 
@@ -224,29 +227,24 @@ class Server:
 
        if self.type not in ('exit', 'relay'):
 
            raise NotImplementedError
 

	
 
        url = 'https://onionoo.torproject.org/weights?lookup={}'.format(
 
            self.fingerprint
 
        )
 

	
 
        data = requests.get(url)
 
        try:
 
            data = manager.query('weights', lookup=self.fingerprint)
 
        except:
 
            raise NotImplementedError
 

	
 
        try:
 
            data.raise_for_status()
 
        except requests.HTTPError as e:
 
            raise e
 
        else:
 
            data = data.json()['relays'][0]
 
        if data is not None:
 
            data = data.relays[0]
 

	
 
        self.mean_consensus_weight = calculate_weight(data['consensus_weight'])
 
        self.mean_exit_probability = calculate_weight(data['exit_probability'])
 
        self.mean_consensus_weight = calculate_weight(data.consensus_weight)
 
        self.mean_exit_probability = calculate_weight(data.exit_probability)
 
        self.mean_guard_probability = calculate_weight(
 
            data['guard_probability']
 
            data.guard_probability
 
        )
 
        self.mean_middle_probability = calculate_weight(
 
            data['middle_probability']
 
            data.middle_probability
 
        )
 
        self.mean_consensus_weight_fraction = calculate_weight(
 
            data['consensus_weight_fraction']
 
            data.consensus_weight_fraction
 
        )
 

	
 
    def check_status(self):
ennstatus/api/views.py
Show inline comments
 
@@ -26,7 +26,6 @@ from werkzeug.exceptions import BadReque
 

	
 
import strict_rfc3339
 
import pygeoip
 
import requests
 

	
 
from ennstatus import csrf
 
from ennstatus.status.functions import (single_server, all_servers,
 
@@ -120,9 +119,6 @@ def update():
 
        server.update_weights()
 
    except NotImplementedError:
 
        pass
 
    except requests.HTTPError as e:
 
        current_app.logger.error(str(e), exc_info=True)
 
        pass
 

	
 
    try:
 
        server.save()
requirements.in
Show inline comments
 
@@ -9,6 +9,5 @@ Flask==0.10.1
 
jsonschema==2.5.1
 
pygeoip==0.3.2
 
python-gnupg==0.3.7
 
requests==2.7.0
 
strict-rfc3339==0.5
 
OnionPy
0 comments (0 inline, 0 general)