diff --git a/domain_admin/utils/whois_util/config.py b/domain_admin/utils/whois_util/config.py index 5304b5b346..b8850f1436 100644 --- a/domain_admin/utils/whois_util/config.py +++ b/domain_admin/utils/whois_util/config.py @@ -9,9 +9,9 @@ # 默认的配置,适用于大部分域名查询 DEFAULT_WHOIS_CONFIG = { - 'whois_server': '', # whois查询服务器 + 'whois_server': '', # whois查询服务器 # 'error': 'No match', # 错误信息 - 'registry_time': 'Creation Date', # 注册时间 + 'registry_time': 'Creation Date', # 注册时间 'expire_time': 'Registry Expiry Date' # 过期时间 } @@ -22,8 +22,14 @@ CUSTOM_WHOIS_CONFIGS = { 'cn': { 'whois_server': 'whois.cnnic.cn', - # 'error': 'Invalid parameter', 'registry_time': 'Registration Time', 'expire_time': 'Expiration Time', + }, + 'hk': { + 'whois_server': 'whois.hkirc.hk', + 'registry_time': 'Domain Name Commencement Date', + "registry_time_format": '%d-%m-%Y', + 'expire_time': 'Expiry Date', + "expire_time_format": '%d-%m-%Y', } } diff --git a/domain_admin/utils/whois_util/whois-servers.txt b/domain_admin/utils/whois_util/whois-servers.txt index 3fe6fe4dfe..09774afda6 100644 --- a/domain_admin/utils/whois_util/whois-servers.txt +++ b/domain_admin/utils/whois_util/whois-servers.txt @@ -195,7 +195,7 @@ tel whois.nic.tel tf whois.nic.tf th whois.thnic.net tj whois.nic.tj -tk whois.nic.tk +tk whois.dot.tk tl whois.domains.tl tm whois.nic.tm tn whois.ati.tn diff --git a/domain_admin/utils/whois_util/whois_util.py b/domain_admin/utils/whois_util/whois_util.py index b278288b97..51178b4cdd 100644 --- a/domain_admin/utils/whois_util/whois_util.py +++ b/domain_admin/utils/whois_util/whois_util.py @@ -5,6 +5,7 @@ """ import json from copy import deepcopy +from datetime import datetime from dateutil import parser @@ -16,6 +17,21 @@ WHOIS_CONFIGS = None +def parse_time(time_str, time_format=None): + """ + 解析时间字符串为时间对象 + :param time_str: + :param time_format: + :return: + """ + if time_format: + time_parsed = datetime.strptime(time_str, time_format) + else: + time_parsed = parser.parse(time_str).replace(tzinfo=None) + + return time_parsed + + def load_whois_servers_config(): """ 加载whois_servers配置 @@ -69,6 +85,8 @@ def get_domain_whois(domain): # error = whois_config['error'] registry_time = whois_config['registry_time'] expire_time = whois_config['expire_time'] + registry_time_format = whois_config.get('registry_time_format') + expire_time_format = whois_config.get('expire_time_format') raw_data = get_whois_raw(domain, whois_server, timeout=10) logger.debug(raw_data) @@ -83,9 +101,10 @@ def get_domain_whois(domain): expire_time = data.get(expire_time) if start_time: - start_time = parser.parse(start_time).replace(tzinfo=None) + start_time = parse_time(start_time, registry_time_format) + if expire_time: - expire_time = parser.parse(expire_time).replace(tzinfo=None) + expire_time = parse_time(expire_time, expire_time_format) if start_time and expire_time: return { diff --git a/tests/utils/test_whois_util.py b/tests/utils/test_whois_util.py index 2b81c38d04..3b7a9ad0e2 100644 --- a/tests/utils/test_whois_util.py +++ b/tests/utils/test_whois_util.py @@ -25,9 +25,12 @@ def test_get_domain_info(): # '126.net' # biz - 'all.biz' + # 'all.biz' + + 'dot.tk' ] + for domain in domain_list: # print(parse_whois_raw(get_whois_raw(domain, ROOT_SERVER))) print(whois_util.get_domain_info(domain))