From a2f394c4dd1b63acf6fe710d428c1e6a3261c2ff Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 24 Feb 2020 20:50:17 -0700 Subject: cleanup, sort methods to update a subset list associated w/ relays object --- tor-metrics/generate.py | 18 +++++++++++------- tor-metrics/templates/all.html | 3 ++- tor-metrics/templates/as.html | 8 +++++--- tor-metrics/templates/country.html | 14 ++++++++------ tor-metrics/templates/platform.html | 7 ++++--- tor-metrics/templates/relay-list.html | 4 ++-- tor-metrics/templates/skeleton.html | 7 ++++++- 7 files changed, 38 insertions(+), 23 deletions(-) diff --git a/tor-metrics/generate.py b/tor-metrics/generate.py index dba4e4b..2df9fd8 100755 --- a/tor-metrics/generate.py +++ b/tor-metrics/generate.py @@ -37,7 +37,7 @@ def generate_html(relays): effective_family(relays) pages_by_flag(relays) unsorted(relays, 'index.html', is_index=True) - unsorted(relays.json['relays'], 'all.html', is_index=False) + unsorted(relays, 'all.html', is_index=False) relay_info(relays) static_src_path = os.path.join(ABS_PATH, 'static') static_dest_path = os.path.join(config.CONFIG['output_root'], 'static') @@ -53,6 +53,7 @@ def unsorted(relays, filename, is_index): :is_index: whether the file is an index or not (True/False) ''' template = ENV.get_template(filename) + relays.json['relay_subset'] = relays.json['relays'] template_render = template.render(relays=relays, is_index=is_index) output = os.path.join(config.CONFIG['output_root'], filename) with open(output, 'w', encoding='utf8') as html: @@ -86,7 +87,8 @@ def effective_family(relays): dir_path = os.path.join(output_path, fingerprint) os.makedirs(dir_path) f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s - rendered = template.render(relays=members, bandwidth=f_bandwidth, + relays.json['relay_subset'] = members + rendered = template.render(relays=relays, bandwidth=f_bandwidth, is_index=False, path_prefix='../../', deactivate='family', family=fingerprint) with open(os.path.join(dir_path, 'index.html'), 'w', @@ -122,7 +124,8 @@ def pages_by_key(relays, key): dir_path = os.path.join(output_path, relay[key]) os.makedirs(dir_path) f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s - rendered = template.render(relays=found_relays, + relays.json['relay_subset'] = found_relays + rendered = template.render(relays=relays, bandwidth=f_bandwidth, is_index=False, path_prefix='../../', deactivate=key, special_countries=countries.THE_PREFIXED) @@ -135,7 +138,6 @@ def pages_by_flag(relays): Render and write HTML listings to disk sorted by FLAG :relays: relays class object containing relay set (list of dict) - :flag: onionoo JSON relay flag to sort by, e.g. 'exit' ''' FLAGS = ['Exit','Fast','Guard','HSDir','Running','Stable','V2Dir','Valid', 'Authority'] @@ -156,7 +158,8 @@ def pages_by_flag(relays): bandwidth += relay['observed_bandwidth'] os.makedirs(output_path) f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s - rendered = template.render(relays=found_relays, + relays.json['relay_subset'] = found_relays + rendered = template.render(relays=relays, bandwidth=f_bandwidth, is_index=False, path_prefix='../../', deactivate=flag, special_countries=countries.THE_PREFIXED, @@ -171,16 +174,17 @@ def relay_info(relays): :relays: relays class object containing relay set (list of dict) ''' + relay_list = relays.json['relays'] template = ENV.get_template('relay-info.html') output_path = os.path.join(config.CONFIG['output_root'], 'relay') if os.path.exists(output_path): rmtree(output_path) os.makedirs(output_path) - relay_list = relays.json['relays'] for relay in relay_list: if not relay['fingerprint'].isalnum(): continue - rendered = template.render(relay=relay, path_prefix='../') + rendered = template.render(relay=relay, path_prefix='../', + relays=relays) with open(os.path.join(output_path, '%s.html' % relay['fingerprint']), 'w', encoding='utf8') as html: html.write(rendered) diff --git a/tor-metrics/templates/all.html b/tor-metrics/templates/all.html index d3324cf..ef6c64a 100644 --- a/tor-metrics/templates/all.html +++ b/tor-metrics/templates/all.html @@ -1,5 +1,6 @@ {% extends "relay-list.html" %} +{% set relay_count = relays.json['relays']|length %} {% block title %}Tor Relays :: All{% endblock %} {% block header %}Home :: All Relays{% endblock %} -{% block description %}All known (non-guard) relays, {{ relays|length }} in total. A smaller browser-friendly subset (top 500) is available here.{% endblock %} +{% block description %}All known (non-guard) relays, {{ relay_count }} in total. A smaller browser-friendly subset (top 500) is available here.{% endblock %} diff --git a/tor-metrics/templates/as.html b/tor-metrics/templates/as.html index 8b7c93d..c27c0ac 100644 --- a/tor-metrics/templates/as.html +++ b/tor-metrics/templates/as.html @@ -1,4 +1,6 @@ {% extends "relay-list.html" %} -{% block title %}Tor Relays :: {{ relays[0]['as']|escape }}{% endblock %} -{% block header %}Home :: {{ relays[0]['as']|escape }}{% endblock %} -{% block description %}{{ relays[0]['as']|escape }} ({{ relays[0]['as_name']|escape }}) is responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} +{% set as_number = relays.json['relay_subset'][0]['as']|escape %} +{% set as_name = relays.json['relay_subset'][0]['as_name']|escape %} +{% block title %}Tor Relays :: {{ as_number }}{% endblock %} +{% block header %}Home :: {{ as_number }}{% endblock %} +{% block description %}{{ as_number }} ({{ as_name }}) is responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} diff --git a/tor-metrics/templates/country.html b/tor-metrics/templates/country.html index 8dcb40d..d7b3395 100644 --- a/tor-metrics/templates/country.html +++ b/tor-metrics/templates/country.html @@ -1,9 +1,11 @@ {% extends "relay-list.html" %} -{% if relays[0]['country_name'] in special_countries %} - {% set country_name = 'The ' + relays[0]['country_name']|escape %} +{% set country_orig = relays.json['relay_subset'][0]['country_name']|escape %} +{% set country_abbr = relays.json['relay_subset'][0]['country']|escape %} +{% if country_orig in special_countries %} + {% set country_name = 'The ' + country_orig %} {% else %} - {% set country_name = relays[0]['country_name']|escape %} + {% set country_name = country_orig %} {% endif %} -{% block title %}Tor Relays :: {{ relays[0]['country_name']|escape }}{% endblock %} -{% block header %}Home :: {{ relays[0]['country_name']|escape }}{% endblock %} -{% block description %}{{ country_name }} ({{ relays[0]['country']|escape }}) is responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} +{% block title %}Tor Relays :: {{ country_orig }}{% endblock %} +{% block header %}Home :: {{ country_name }}{% endblock %} +{% block description %}{{ country_name }} ({{ country_abbr }}) is responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} diff --git a/tor-metrics/templates/platform.html b/tor-metrics/templates/platform.html index 8743b76..1f2741e 100644 --- a/tor-metrics/templates/platform.html +++ b/tor-metrics/templates/platform.html @@ -1,4 +1,5 @@ {% extends "relay-list.html" %} -{% block title %}Tor Relays :: {{ relays[0]['platform']|escape }}{% endblock %} -{% block header %}Home :: {{ relays[0]['platform']|escape }}{% endblock %} -{% block description %}{{ relays[0]['platform']|escape }} systems are responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} +{% set platform = relays.json['relay_subset'][0]['platform']|escape %} +{% block title %}Tor Relays :: {{ platform }}{% endblock %} +{% block header %}Home :: {{ platform }}{% endblock %} +{% block description %}{{ platform }} systems are responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} diff --git a/tor-metrics/templates/relay-list.html b/tor-metrics/templates/relay-list.html index 024e658..da6f175 100644 --- a/tor-metrics/templates/relay-list.html +++ b/tor-metrics/templates/relay-list.html @@ -17,8 +17,8 @@ First Seen - {% if is_index %}{% set relay_list = relays.json['relays'][:500] %} - {% else %}{% set relay_list = relays %}{% endif %} + {% if is_index %}{% set relay_list = relays.json['relay_subset'][:500] %} + {% else %}{% set relay_list = relays.json['relay_subset'] %}{% endif %} {% for relay in relay_list %} {% if relay['observed_bandwidth'] > 1000000 %} diff --git a/tor-metrics/templates/skeleton.html b/tor-metrics/templates/skeleton.html index 17dbc8a..2f67bb1 100644 --- a/tor-metrics/templates/skeleton.html +++ b/tor-metrics/templates/skeleton.html @@ -13,9 +13,14 @@
{% block body %} {% endblock %} -

Country flags provided by GoSquared, relay flags by the Tor Project.

{% block footer %} + {% endblock %} -- cgit v1.2.3-54-g00ecf