-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from Mews/use-settings-classes
- Loading branch information
Showing
7 changed files
with
197 additions
and
81 deletions.
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from tiny_web_crawler.core.spider import Spider | ||
from tiny_web_crawler.core.spider_settings import SpiderSettings |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Optional | ||
|
||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class GeneralSettings: | ||
""" | ||
A simple dataclass to store general settings for the Spider class | ||
Attributes: | ||
root_url (str): The root URL to start crawling from. | ||
max_links (int): The maximum number of links to crawl. (Default: 5) | ||
save_to_file (Optional[str]): The file path to save the crawl results. | ||
max_workers (int): Max count of concurrent workers. (Default: 1) | ||
delay (float): Delay between requests. (Default: 0.5) | ||
verbose (bool): Whether or not to print debug messages (Default: True) | ||
""" | ||
|
||
root_url: str = "" | ||
max_links: int = 5 | ||
save_to_file: Optional[str] = None | ||
max_workers: int = 1 | ||
delay: float = 0.5 | ||
verbose: bool = True | ||
|
||
@dataclass | ||
class CrawlSettings: | ||
""" | ||
A simple dataclass to store crawl settings for the Spider class | ||
Attributes: | ||
url_regex (Optional[str]): A regular expression against which urls will be matched before crawling | ||
include_body (bool): Whether or not to include the crawled page's body in crawl_result (Default: False) | ||
internal_links_only (bool): Whether or not to crawl only internal links (Default: False) | ||
external_links_only (bool): Whether or not to crawl only external links (Default: False) | ||
respect_robots_txt (bool): Whether or not to respect website's robots.txt files (defualt: True) | ||
""" | ||
url_regex: Optional[str] = None | ||
include_body: bool = False | ||
internal_links_only: bool = False | ||
external_links_only: bool = False | ||
respect_robots_txt: bool = True | ||
|
||
@dataclass | ||
class SpiderSettings(GeneralSettings, CrawlSettings): | ||
""" | ||
A simple dataclass that stores all the settings for the Spider class | ||
""" | ||
|
||
def __post_init__(self) -> None: | ||
if self.root_url == "": | ||
raise ValueError("\"root_url\" argument is required") | ||
|
||
if self.internal_links_only and self.external_links_only: | ||
raise ValueError("Only one of internal_links_only and external_links_only can be set to True") |
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
Oops, something went wrong.