From 4a47616cc632a4569adee8c8d0c63191e2912d10 Mon Sep 17 00:00:00 2001 From: MoMMde Date: Sat, 26 Mar 2022 12:23:48 +0100 Subject: [PATCH] small refactoring in response evaluation --- sherlock/sherlock.py | 59 ++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 41 deletions(-) diff --git a/sherlock/sherlock.py b/sherlock/sherlock.py index 495e5934..647f0222 100644 --- a/sherlock/sherlock.py +++ b/sherlock/sherlock.py @@ -356,13 +356,12 @@ def sherlock(username, site_data, query_notify, except: response_text = "" + query_status = QueryStatus.UNKNOWN + error_context = None + if error_text is not None: - result = QueryResult(username, - social_network, - url, - QueryStatus.UNKNOWN, - query_time=response_time, - context=error_text) + error_context = error_text + elif error_type == "message": # error_flag True denotes no error found in the HTML # error_flag False denotes error found in the HTML @@ -385,38 +384,18 @@ def sherlock(username, site_data, query_notify, error_flag = False break if error_flag: - result = QueryResult(username, - social_network, - url, - QueryStatus.CLAIMED, - query_time=response_time) + query_status = QueryStatus.CLAIMED else: - result = QueryResult(username, - social_network, - url, - QueryStatus.AVAILABLE, - query_time=response_time) + query_status = QueryStatus.AVAILABLE elif error_type == "status_code": # Checks if the Status Code is equal to the optional "errorCode" given in 'data.json' if error_code == r.status_code: - result = QueryResult(username, - social_network, - url, - QueryStatus.AVAILABLE, - query_time=response_time) + query_status = QueryStatus.AVAILABLE # Checks if the status code of the response is 2XX elif not r.status_code >= 300 or r.status_code < 200: - result = QueryResult(username, - social_network, - url, - QueryStatus.CLAIMED, - query_time=response_time) + query_status = QueryStatus.CLAIMED else: - result = QueryResult(username, - social_network, - url, - QueryStatus.AVAILABLE, - query_time=response_time) + query_status = QueryStatus.AVAILABLE elif error_type == "response_url": # For this detection method, we have turned off the redirect. # So, there is no need to check the response URL: it will always @@ -424,17 +403,9 @@ def sherlock(username, site_data, query_notify, # code indicates that the request was successful (i.e. no 404, or # forward to some odd redirect). if 200 <= r.status_code < 300: - result = QueryResult(username, - social_network, - url, - QueryStatus.CLAIMED, - query_time=response_time) + query_status = QueryStatus.CLAIMED else: - result = QueryResult(username, - social_network, - url, - QueryStatus.AVAILABLE, - query_time=response_time) + query_status = QueryStatus.AVAILABLE else: # It should be impossible to ever get here... raise ValueError(f"Unknown Error Type '{error_type}' for " @@ -442,6 +413,12 @@ def sherlock(username, site_data, query_notify, # Notify caller about results of query. + result = QueryResult(username=username, + site_name=social_network, + site_url_user=url, + status=query_status, + query_time=response_time, + context=error_context) query_notify.update(result) # Save status of request