-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Detailed report #69
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,14 +44,16 @@ def __init__(self, host: str, delay: int = 1, deep_scan: bool = False): | |||||
# Shallow scan of foreign url | ||||||
self.deep_scan = deep_scan | ||||||
|
||||||
# Will represent the list of URL to check | ||||||
self.url_to_check = [host] | ||||||
|
||||||
# Will represent the list of checked URL | ||||||
self.checked_url = [] | ||||||
self.urls = { | ||||||
host: { | ||||||
'parent': '', | ||||||
'url': None, | ||||||
'result': None, | ||||||
'check_time': None | ||||||
|
||||||
# Will represent the list of broken URL | ||||||
self.broken_url = {} | ||||||
} | ||||||
} | ||||||
|
||||||
# Will represent the previous webpage content | ||||||
self.prev_data = '' | ||||||
|
@@ -101,33 +103,31 @@ def check(self, url: str) -> requests.Response: | |||||
:url represent the URL to check | ||||||
""" | ||||||
# We verify the URL is already checked | ||||||
if url in self.checked_url: | ||||||
if self.urls[url]['result']: | ||||||
return None | ||||||
|
||||||
self.logging.info('Checking of %s...' % url) | ||||||
|
||||||
# We mark the URL checked | ||||||
self.checked_url.append(url) | ||||||
|
||||||
# We make a connection | ||||||
try: | ||||||
if self.is_same_host(url): | ||||||
response = self.conn.get(url, timeout=2, stream=True) | ||||||
else: | ||||||
response = self.conn.head(url, timeout=2) | ||||||
except requests.exceptions.ReadTimeout: | ||||||
self.broken_url[url] = "Timeout!" | ||||||
self.urls[url]['result'] = False, None, "Timeout!" | ||||||
except requests.exceptions.ConnectionError: | ||||||
self.broken_url[url] = "Connection aborted!" | ||||||
self.urls[url]['result'] = False, None, "Connection aborted!" | ||||||
except requests.exceptions.TooManyRedirects: | ||||||
self.broken_url[url] = "Too many redirection!" | ||||||
self.urls[url]['result'] = False, None, "Too many redirection!" | ||||||
else: | ||||||
# We verify the response status | ||||||
# 2xx stand for request was successfully completed | ||||||
if response.ok: | ||||||
self.urls[url]['result'] = True, response.status_code, response.reason | ||||||
return response if self.is_same_host(url) else None | ||||||
else: | ||||||
self.broken_url[url] = response.reason | ||||||
self.urls[url]['result'] = False, response.status_code, response.reason | ||||||
|
||||||
self.logging.warning( | ||||||
'%s maybe broken because status code: %i' % | ||||||
|
@@ -177,6 +177,8 @@ def update_list(self, response: requests.Response) -> None: | |||||
else: | ||||||
continue | ||||||
|
||||||
origin_url = url | ||||||
|
||||||
# 1.1 and 1.2 | ||||||
if self.is_same_host(url): | ||||||
# 1.2 | ||||||
|
@@ -207,11 +209,15 @@ def update_list(self, response: requests.Response) -> None: | |||||
# Except if the deep_scan is enable | ||||||
# At this point, the URL belongs to the HOST | ||||||
# We verify that the URL is neither already added nor checked | ||||||
if url not in self.url_to_check \ | ||||||
and url not in self.checked_url \ | ||||||
and url != response.url: | ||||||
if url not in self.urls and url != response.url: | ||||||
self.logging.debug('Add the URL %s' % url) | ||||||
self.url_to_check.append(url) | ||||||
self.urls[url] = { | ||||||
'parent': response.url, | ||||||
'url': origin_url, | ||||||
'result': None, | ||||||
'check_time': None | ||||||
|
||||||
} | ||||||
else: | ||||||
continue | ||||||
|
||||||
|
@@ -226,8 +232,18 @@ def update_list(self, response: requests.Response) -> None: | |||||
def run(self) -> None: | ||||||
"""Run the checker.""" | ||||||
# We check while we have an URL unchecked | ||||||
while (self.url_to_check): | ||||||
response = self.check(self.url_to_check.pop(0)) | ||||||
if response: | ||||||
self.update_list(response) | ||||||
time.sleep(self.delay) | ||||||
while 1: | ||||||
url_to_check = [u for u in self.urls if not self.urls[u]['result']] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
to prevent a KeyError raising here ! |
||||||
|
||||||
if not url_to_check: | ||||||
break | ||||||
|
||||||
while url_to_check: | ||||||
url = url_to_check.pop(0) | ||||||
self.urls[url]['check_time'] = time.time() | ||||||
response = self.check(url) | ||||||
if response: | ||||||
self.update_list(response) | ||||||
self.urls[url]['check_time'] = time.time() - self.urls[url]['check_time'] | ||||||
time.sleep(self.delay) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.