-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add excluded_urls #9
Open
jimpriest
wants to merge
4
commits into
bartdag:master
Choose a base branch
from
jimpriest:excluded-urls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from optparse import OptionParser, OptionGroup | ||
|
||
from pylinkvalidator.compat import get_safe_str | ||
from pylinkvalidator.urlutil import get_clean_url_split | ||
from pylinkvalidator.urlutil import get_clean_url_split, re | ||
|
||
|
||
DEFAULT_TYPES = ['a', 'img', 'script', 'link'] | ||
|
@@ -148,6 +148,7 @@ def __init__(self): | |
self.worker_config = None | ||
self.accepted_hosts = [] | ||
self.ignored_prefixes = [] | ||
self.excluded_urls = [] | ||
self.worker_size = 0 | ||
|
||
def should_crawl(self, url_split, depth): | ||
|
@@ -160,15 +161,21 @@ def is_local(self, url_split): | |
return url_split.netloc in self.accepted_hosts | ||
|
||
def should_download(self, url_split): | ||
"""Returns True if the url does not start with an ignored prefix and if | ||
it is local or outside links are allowed.""" | ||
"""Returns True if the url does not start with | ||
* an ignored prefix | ||
* it does not match excluded url regex | ||
* if it is local or outside links are allowed.""" | ||
local = self.is_local(url_split) | ||
|
||
if not self.options.test_outside and not local: | ||
return False | ||
|
||
url = url_split.geturl() | ||
|
||
for exclude_url in self.excluded_urls: | ||
if re.search(exclude_url, url): | ||
return False | ||
|
||
for ignored_prefix in self.ignored_prefixes: | ||
if url.startswith(ignored_prefix): | ||
return False | ||
|
@@ -207,6 +214,9 @@ def _parse_config(self): | |
if self.options.ignored_prefixes: | ||
self.ignored_prefixes = self.options.ignored_prefixes.split(',') | ||
|
||
if self.options.excluded_urls: | ||
self.excluded_urls = self.options.excluded_urls.split(',') | ||
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. slight optimization to prevent compiling the regex for every url: self.excluded_urls = [re.compile(pattern) for pattern in self.options.excluded_urls.split(',')] |
||
|
||
if self.options.workers: | ||
self.worker_size = self.options.workers | ||
else: | ||
|
@@ -274,6 +284,11 @@ def _build_parser(self): | |
dest="accepted_hosts", action="store", default=None, | ||
help="comma-separated list of additional hosts to crawl (e.g., " | ||
"example.com,subdomain.another.com)") | ||
crawler_group.add_option( | ||
"-x", "--exclude", dest="excluded_urls", | ||
action="store", default=None, | ||
help="URLs matching the regular expression will be ignored" | ||
"(e.g., /private/)") | ||
crawler_group.add_option( | ||
"-i", "--ignore", dest="ignored_prefixes", | ||
action="store", default=None, | ||
|
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.
if you go with the optimization noted below: