From 8ff4d8c16cd2c2065b361b6fdf0cb31449824cd1 Mon Sep 17 00:00:00 2001 From: kiriharu Date: Wed, 13 Jan 2021 23:49:23 +0300 Subject: [PATCH] cover many domain zones with whois_vu package --- apps/tgbot/pyproject.toml | 1 + apps/tgbot/tgbot/handlers/default/whois.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/tgbot/pyproject.toml b/apps/tgbot/pyproject.toml index 02f38a5..d0d67bc 100644 --- a/apps/tgbot/pyproject.toml +++ b/apps/tgbot/pyproject.toml @@ -12,6 +12,7 @@ python-whois = "^0.7.3" core = {path = "../core"} aioinflux = "^0.9.0" loguru = "^0.5.3" +whois-vu = "^0.2.0" [tool.poetry.dev-dependencies] diff --git a/apps/tgbot/tgbot/handlers/default/whois.py b/apps/tgbot/tgbot/handlers/default/whois.py index 0c07925..0977667 100644 --- a/apps/tgbot/tgbot/handlers/default/whois.py +++ b/apps/tgbot/tgbot/handlers/default/whois.py @@ -3,7 +3,7 @@ from typing import Optional from whois import whois, parser from aiogram.types import Message from dataclasses import dataclass -from aiogram.utils.markdown import quote_html +from whois_vu.api import WhoisSource from tgbot.handlers.base import SimpleCommandHandler from tgbot.handlers.errors import NotEnoughArgs, LocalhostForbidden @@ -29,7 +29,7 @@ class DomainAttrClass: attr: str -# DOMAIN_ATTR_CLASSES order have matter! +# DOMAIN_ATTR_CLASSES order matters! DOMAIN_ATTR_CLASSES = [ DomainAttrClass("👤", "Регистратор", "registrar"), DomainAttrClass("📅", "Дата создания", "creation_date"), @@ -46,21 +46,26 @@ DOMAIN_ATTR_CLASSES = [ ] +def whois_request(domain: str) -> parser.WhoisEntry: + domain_info = whois(domain) + if domain_info.get("domain_name") is None: + ws = WhoisSource().get(domain) + domain_info = parser.WhoisEntry.load(domain, ws.whois) + return domain_info + + def create_whois_message(domain: str) -> str: try: - domain_info = whois(domain) + domain_info = whois_request(domain) except parser.PywhoisError as e: return f"❗ Домен {domain} свободен или не был найден." domain_name = domain_info.get("domain_name") - if domain_name is None: - return no_domain_text if isinstance(domain_name, list): domain_name = domain_name[0] - message = f"\n📝 Информация о домене {domain_name.lower()}:" for i, domain_attr in enumerate(DOMAIN_ATTR_CLASSES): - # for pretty printing, DOMAIN_ATTR_CLASSES order have matter! + # for pretty printing, DOMAIN_ATTR_CLASSES order matters! if i in [2, 10]: message += "\n" resp = format_domain_item( @@ -76,6 +81,7 @@ def format_domain_item(icon, item_name, items) -> Optional[str]: if not items: return if isinstance(items, list): + items = map(str, items) # fix datetime bug message = f"\n{icon} {item_name}:\n" message += str.join("\n", [f" * {ns}" for ns in list(set(map(str.lower, items)))]) else: