diff options
author | Jordan <me@jordan.im> | 2019-11-02 12:47:00 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2019-11-02 12:47:00 -0700 |
commit | ded0c17e1832a7756370fe91369e0b8942c5f7b0 (patch) | |
tree | b9699645ec4448a12688c82a983d214fba9618ba | |
parent | b2b4b404561afb2f55c2b7a15e5338e48c33a174 (diff) | |
download | allium-ded0c17e1832a7756370fe91369e0b8942c5f7b0.tar.gz allium-ded0c17e1832a7756370fe91369e0b8942c5f7b0.zip |
smaller-scope if conditionals, missing exception return
-rwxr-xr-x | tor-metrics/generate.py | 64 | ||||
-rw-r--r-- | tor-metrics/relays.py | 1 |
2 files changed, 34 insertions, 31 deletions
diff --git a/tor-metrics/generate.py b/tor-metrics/generate.py index cad365f..edea6a9 100755 --- a/tor-metrics/generate.py +++ b/tor-metrics/generate.py @@ -29,18 +29,19 @@ def generate_html(relays): :relays: relays class object containing relay set (list of dict) ''' - if relays.json is not None: - pages_by_key(relays, 'as') - pages_by_key(relays, 'country') - pages_by_key(relays, 'platform') - effective_family(relays) - unsorted(relays, 'index.html', is_index=True) - unsorted(relays.json['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') - if not os.path.exists(static_dest_path): - copytree(static_src_path, static_dest_path) + if relays.json is None: + return + pages_by_key(relays, 'as') + pages_by_key(relays, 'country') + pages_by_key(relays, 'platform') + effective_family(relays) + unsorted(relays, 'index.html', is_index=True) + unsorted(relays.json['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') + if not os.path.exists(static_dest_path): + copytree(static_src_path, static_dest_path) def unsorted(relays, filename, is_index): ''' @@ -107,25 +108,26 @@ def pages_by_key(relays, key): for idx, relay in enumerate(relay_list): found_relays = [] bandwidth = 0 # total bandwidth for relay subset - if relay.get(key) and relay[key] not in values_processed: - values_processed.append(relay[key]) - # find relays w/ matching value past outer idx - for p_relay in relay_list[idx:]: - if p_relay.get(key) and p_relay[key] == relay[key]: - found_relays.append(p_relay) - bandwidth += p_relay['observed_bandwidth'] - if not relay[key].isalnum(): - continue - 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, - bandwidth=f_bandwidth, is_index=False, - path_prefix='../../', deactivate=key, - special_countries=countries.THE_PREFIXED) - with open(os.path.join(dir_path, 'index.html'), 'w', - encoding='utf8') as html: - html.write(rendered) + if not relay.get(key) or relay[key] in values_processed: + continue + if not relay[key].isalnum(): + continue + values_processed.append(relay[key]) + # find relays w/ matching value past outer idx + for p_relay in relay_list[idx:]: + if p_relay.get(key) and p_relay[key] == relay[key]: + found_relays.append(p_relay) + bandwidth += p_relay['observed_bandwidth'] + 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, + bandwidth=f_bandwidth, is_index=False, + path_prefix='../../', deactivate=key, + special_countries=countries.THE_PREFIXED) + with open(os.path.join(dir_path, 'index.html'), 'w', + encoding='utf8') as html: + html.write(rendered) def relay_info(relays): ''' diff --git a/tor-metrics/relays.py b/tor-metrics/relays.py index 71afd9f..c8b94ea 100644 --- a/tor-metrics/relays.py +++ b/tor-metrics/relays.py @@ -52,6 +52,7 @@ class Relays: return None except Exception as err: print('Uncaught exception during onionoo fetch: %s' % err) + return None json_data = json.loads(api_response.decode('utf-8')) fixed_bw = self.fix_missing_observed_bandwidth(json_data) |