diff options
author | Jordan <me@jordan.im> | 2019-06-21 22:45:24 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2019-06-21 22:45:24 -0700 |
commit | a18c9d4ea994b3040190a9e40876a4ba429d5363 (patch) | |
tree | 15cbe1c41533b70d4c5e124eac4418805950f7d9 /tor-metrics | |
parent | fc58403a0187e4c707d46a0f5fe9a80f3852e791 (diff) | |
download | allium-a18c9d4ea994b3040190a9e40876a4ba429d5363.tar.gz allium-a18c9d4ea994b3040190a9e40876a4ba429d5363.zip |
trim redundant exception handling
Diffstat (limited to 'tor-metrics')
-rwxr-xr-x | tor-metrics/generate.py | 5 | ||||
-rw-r--r-- | tor-metrics/relays.py | 29 |
2 files changed, 9 insertions, 25 deletions
diff --git a/tor-metrics/generate.py b/tor-metrics/generate.py index 608cbdd..2f573a5 100755 --- a/tor-metrics/generate.py +++ b/tor-metrics/generate.py @@ -21,11 +21,6 @@ def generate_html(relays): 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) - else: - if relays.statuscode == 304: - print('HTTP 304 response, no modification since last fetch') - else: - print('API request could not be completed') def unsorted(relays, filename, is_index): template_env = env.get_template(filename) diff --git a/tor-metrics/relays.py b/tor-metrics/relays.py index 2c88528..423b02e 100644 --- a/tor-metrics/relays.py +++ b/tor-metrics/relays.py @@ -6,7 +6,6 @@ abs_path = os.path.dirname(os.path.abspath(__file__)) class Relays: def __init__(self): - self.statuscode = None self.url = config.CONFIG['onionoo_url'] self.ts_file = os.path.join(abs_path, "timestamp") self.json = self.fetch() @@ -16,26 +15,16 @@ class Relays: if os.path.isfile(self.ts_file): with open(self.ts_file, 'r') as ts_file: prev_timestamp = ts_file.read() - try: - conn = urllib.request.Request(self.url, - headers={"If-Modified-Since": prev_timestamp}) - api_response = urllib.request.urlopen(conn).read() - self.statuscode = urllib.request.urlopen(conn).getcode() - except HTTPError as e: - self.statuscode = e.code - return(None) - except URLError as e: - return(None) + headers = {"If-Modified-Since": prev_timestamp} + conn = urllib.request.Request(self.url, headers=headers) else: - try: - conn = urllib.request.Request(self.url) - api_response = urllib.request.urlopen(conn).read() - self.statuscode = urllib.request.urlopen(conn).getcode() - except HTTPError as e: - self.statuscode = e.code - return(None) - except URLError as e: - return(None) + conn = urllib.request.Request(self.url) + + try: + api_response = urllib.request.urlopen(conn).read() + except Exception as e: + print(e) + return(None) json_data = json.loads(api_response.decode('utf-8')) sorted_json = self.sort_by_bandwidth(json_data) |