fix errors, more error handling

This commit is contained in:
kiriharu 2021-01-14 00:37:57 +03:00
parent 8a89e58840
commit f6aff5a9ef
2 changed files with 21 additions and 3 deletions

View File

@ -12,7 +12,7 @@ python-whois = "^0.7.3"
core = {path = "../core"} core = {path = "../core"}
aioinflux = "^0.9.0" aioinflux = "^0.9.0"
loguru = "^0.5.3" loguru = "^0.5.3"
whois-vu = "^0.2.0" whois-vu = "^0.3.0"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]

View File

@ -4,7 +4,9 @@ from whois import whois, parser
from aiogram.types import Message from aiogram.types import Message
from dataclasses import dataclass from dataclasses import dataclass
from whois_vu.api import WhoisSource from whois_vu.api import WhoisSource
from whois_vu.errors import IncorrectZone, QueryNotMatchRegexp
from tgbot.handlers.whois_zones import ZONES
from tgbot.handlers.base import SimpleCommandHandler from tgbot.handlers.base import SimpleCommandHandler
from tgbot.handlers.errors import NotEnoughArgs, LocalhostForbidden from tgbot.handlers.errors import NotEnoughArgs, LocalhostForbidden
from tgbot.middlewares.throttling import rate_limit from tgbot.middlewares.throttling import rate_limit
@ -21,6 +23,12 @@ no_domain_text = """
Напишите /whois чтобы посмотреть справку. Напишите /whois чтобы посмотреть справку.
""" """
incorrect_domain = "❗ Домен {domain} не поддерживается в текущей реализации /whois или его попросту не " \
"существует.\n\n" \
"📌 Если вы считаете что это какая-то ошибка, " \
"то вы можете рассказать " \
"нам о ней удобным для вас способом. Контакты указаны в /start."
@dataclass @dataclass
class DomainAttrClass: class DomainAttrClass:
@ -49,17 +57,27 @@ DOMAIN_ATTR_CLASSES = [
def whois_request(domain: str) -> parser.WhoisEntry: def whois_request(domain: str) -> parser.WhoisEntry:
domain_info = whois(domain) domain_info = whois(domain)
if domain_info.get("domain_name") is None: if domain_info.get("domain_name") is None:
splitted = domain.split(".")
ws = WhoisSource().get(domain) ws = WhoisSource().get(domain)
domain_info = parser.WhoisEntry.load(domain, ws.whois) if zone_class := ZONES.get(splitted[-1], None):
domain_info = zone_class(domain, ws.whois)
else:
domain_info = parser.WhoisEntry.load(domain, ws.whois)
return domain_info return domain_info
def create_whois_message(domain: str) -> str: def create_whois_message(domain: str) -> str:
try: try:
domain_info = whois_request(domain) domain_info = whois_request(domain)
except parser.PywhoisError as e: except parser.PywhoisError:
return f"❗ Домен {domain} свободен или не был найден." return f"❗ Домен {domain} свободен или не был найден."
except IncorrectZone:
return incorrect_domain.format(domain=domain)
except QueryNotMatchRegexp:
return incorrect_domain.format(domain=domain)
domain_name = domain_info.get("domain_name") domain_name = domain_info.get("domain_name")
if not domain_name:
return incorrect_domain.format(domain=domain)
if isinstance(domain_name, list): if isinstance(domain_name, list):
domain_name = domain_name[0] domain_name = domain_name[0]
message = f"\n📝 Информация о домене {domain_name.lower()}:" message = f"\n📝 Информация о домене {domain_name.lower()}:"