Skip to content

Commit

Permalink
Added Bot.bot() constructor class method and reset __init__()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorell committed Apr 8, 2020
1 parent 736e0e0 commit b6000c2
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions se_bot_checker/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,10 @@ class Bot:
request_ip = None
request_user_agent = None

def __init__(self, name: str, user_agent: str, domains: List[str] = [], use_regex: bool = False,
use_reverse_dns: bool = None):
def __init__(self, use_reverse_dns: bool = None):
"""
The bot class constructor method.
:param name: The name of the new bot to return if valid.
:type name: str
:param user_agent: The user agent signature to match ``request_user_agent``
strings against.
:type user_agent: str
:param domains: A list of valid domains that the reverse DNS host can match.
Defaults to an empty list.
:type domains: List[str]
:param use_regex: ``True`` if you want to use RegEx to match the user agent.
Defaults to ``False``.
:type use_regex: bool
:param use_reverse_dns: ``True`` if DNS requests should be made. This can slow
down the validation. However, for most crawlers this is the only way to
verify the validity of a crawler IP. You can turn this off if you want to
Expand All @@ -62,10 +50,6 @@ def __init__(self, name: str, user_agent: str, domains: List[str] = [], use_rege
"""
if use_reverse_dns is not None:
self.use_reverse_dns = use_reverse_dns
self.name = name
self.user_agent = user_agent
self.domains = domains
self.use_regex = use_regex

def __call__(self, ip: str, user_agent: str) -> Tuple[bool, str]:
"""
Expand All @@ -81,6 +65,37 @@ def __call__(self, ip: str, user_agent: str) -> Tuple[bool, str]:
self.request_user_agent = user_agent
return self.run()

@classmethod
def bot(cls, name: str, user_agent: str, domains: List[str] = [], use_regex: bool = False,
use_reverse_dns: bool = True):
"""
The bot class constructor method.
:param name: The name of the new bot to return if valid.
:type name: str
:param user_agent: The user agent signature to match ``request_user_agent``
strings against.
:type user_agent: str
:param domains: A list of valid domains that the reverse DNS host can match.
Defaults to an empty list.
:type domains: List[str]
:param use_regex: ``True`` if you want to use RegEx to match the user agent.
Defaults to ``False``.
:type use_regex: bool
:param use_reverse_dns: ``True`` if DNS requests should be made. This can slow
down the validation. However, for most crawlers this is the only way to
verify the validity of a crawler IP. You can turn this off if you want to
quickly match against a list of known valid IPs.
:type use_reverse_dns: bool
:return: Bot instance
"""
bot = cls(use_reverse_dns)
bot.name = name
bot.user_agent = user_agent
bot.domains = domains
bot.use_regex = use_regex
return bot

def run(self) -> Tuple[bool, str]:
"""
Run the bot validation.
Expand Down

0 comments on commit b6000c2

Please sign in to comment.