Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed May 30, 2023
1 parent 9742179 commit 1a57f5d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion domain_admin/service/cache_domain_info_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_domain_info(domain: str) -> CacheDomainInfoModel:

# 不存在或者已过期,重新获取
if not row or row.is_expired is True:
domain_whois = whois_util.get_domain_whois(root_domain)
domain_whois = whois_util.get_domain_info(root_domain)

if domain_whois is None:
raise Exception("域名信息获取失败")
Expand Down
29 changes: 29 additions & 0 deletions domain_admin/utils/text_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""
@File : text_util.py
@Date : 2023-05-30
"""
import re


def has_chinese(text: str) -> bool:
"""
判断是否包含中文
:param text:
:return:
"""
result = re.match("[\u4e00-\u9fa5]+", text)
return True if result else False


def extract_chinese(text: str) -> str:
"""
提取包含的中文
:param text:
:return:
"""
result = re.match("[\u4e00-\u9fa5]+", text)
if result:
return result.group(0)
else:
return ""
27 changes: 26 additions & 1 deletion domain_admin/utils/whois_util/whois_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,36 @@
from dateutil import parser

from domain_admin.log import logger
from domain_admin.utils import json_util
from domain_admin.utils import json_util, text_util, domain_util
from domain_admin.utils.whois_util.config import CUSTOM_WHOIS_CONFIGS, DEFAULT_WHOIS_CONFIG
from domain_admin.utils.whois_util.util import parse_whois_raw, get_whois_raw, load_whois_servers

WHOIS_CONFIGS = None


def resolve_domain(domain: str) -> str:
"""
域名转换
:param domain:
:return:
"""
# 解析出域名和顶级后缀
extract_result = domain_util.extract_domain(domain)

root_domain = extract_result.domain
suffix = extract_result.suffix

# 处理包含中文的域名
if text_util.has_chinese(root_domain):
chinese = text_util.extract_chinese(root_domain)
punycode = chinese.encode('punycode').decode()
root_domain = f"xn--{punycode}"

domain_and_suffix = '.'.join([root_domain, suffix])

return domain_and_suffix


def parse_time(time_str, time_format=None):
"""
解析时间字符串为时间对象
Expand Down Expand Up @@ -124,6 +147,8 @@ def get_domain_info(domain: str):
# 处理带端口号的域名
# if ':' in domain:
# domain = domain.split(":")[0]
domain = resolve_domain(domain)
logger.debug("resolve_domain: %s", domain)

res = get_domain_whois(domain)

Expand Down
12 changes: 12 additions & 0 deletions tests/utils/test_text_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
"""
@File : test_text_util.py
@Date : 2023-05-30
"""
from domain_admin.utils import text_util


def test_has_chinese():
assert text_util.has_chinese("I love you") is False
assert text_util.has_chinese("我喜欢你") is True
assert text_util.has_chinese("我 love 你") is True
5 changes: 4 additions & 1 deletion tests/utils/test_whois_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
@Date : 2022-10-22
@Author : Peng Shiyu
"""
import re

from domain_admin.utils import whois_util
from domain_admin.utils import text_util
from domain_admin.utils.whois_util.config import ROOT_SERVER
from domain_admin.utils.whois_util.util import get_whois_raw, parse_whois_raw

Expand All @@ -30,7 +32,8 @@ def test_get_domain_info():
# 'dot.tk'
# 'bilibili.tv'
# 'wowma.jp'
'www.otto.de'
# 'www.otto.de',
'www.米梵家居.com'
]

for domain in domain_list:
Expand Down

0 comments on commit 1a57f5d

Please sign in to comment.