Skip to content

Commit

Permalink
Update whois
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf authored Mar 14, 2024
1 parent f7612ae commit e9d7710
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
71 changes: 33 additions & 38 deletions modules/whois/__init__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,52 @@
from datetime import datetime

from whois import whois

from config import Config
from core.builtins import Bot, Plain, Image
from core.builtins import Bot
from core.component import module
from core.logger import Logger
from core.utils.text import parse_time_string


def process(input_):
if isinstance(input_, list) and len(input_) > 0:
return input_[0]
if isinstance(input_, list):
input_list = list(set([i.lower() for i in input_]))
return ', '.join(input_list)
else:
return input_


def get_value(dict, key):
if key in dict:
return dict[key]
else:
return None


w = module('whois', developers=['DoroWolf'])


@w.handle('<domain> {{whois.help}}')
async def _(msg: Bot.MessageSession, domain: str):
res = await get_whois(msg, domain)
output = await msg.finish(res)
await msg.finish(res)


async def get_whois(msg, domain):
try:
info = whois(domain)
Logger.debug(str(info))
Logger.info(str(info))
except BaseException:
await msg.finish(msg.locale.t("whois.message.get_failed"))

domain_name = get_value(info, 'domain_name')
registrar = get_value(info, 'registrar')
whois_server = get_value(info, 'whois_server')
updated_date = get_value(info, 'updated_date')
creation_date = get_value(info, 'creation_date')
expiration_date = get_value(info, 'expiration_date')
name_servers = get_value(info, 'name_servers')
dnssec = get_value(info, 'dnssec')
name = get_value(info, 'name')
org = get_value(info, 'org')
address = get_value(info, 'address')
city = get_value(info, 'city')
state = get_value(info, 'state')
country = get_value(info, 'country')
registrant_postal_code = get_value(info, 'registrant_postal_code')
domain_name = info.get('domain_name')
registrar = info.get('registrar')
whois_server = info.get('whois_server')
updated_date = info.get('updated_date')
creation_date = info.get('creation_date')
expiration_date = info.get('expiration_date')
name_servers = info.get('name_servers')
emails = info.get('emails')
dnssec = info.get('dnssec')
name = info.get('name')
org = info.get('org')
address = info.get('address')
city = info.get('city')
state = info.get('state')
country = info.get('country')
registrant_postal_code = info.get('registrant_postal_code')

if not domain_name:
await msg.finish(msg.locale.t("whois.message.get_failed"))
Expand All @@ -62,18 +55,19 @@ async def get_whois(msg, domain):
whois_server = whois_server.lower()

if updated_date: # 此时间为UTC时间
updated_date = (process(updated_date) + parse_time_string(Config('timezone_offset', '+8'))).timestamp()
if isinstance(updated_date, list):
updated_date = updated_date[0]
updated_date = (updated_date + parse_time_string(Config('timezone_offset', '+8'))).timestamp()

if creation_date: # 此时间为UTC时间
creation_date = (process(creation_date) + parse_time_string(Config('timezone_offset', '+8'))).timestamp()
if isinstance(creation_date, list):
creation_date = creation_date[0]
creation_date = (creation_date + parse_time_string(Config('timezone_offset', '+8'))).timestamp()

if expiration_date: # 此时间为UTC时间
expiration_date = (process(expiration_date) + parse_time_string(Config('timezone_offset', '+8'))).timestamp()

if name_servers:
name_servers_list = list(set([i.lower() for i in name_servers]))
else:
name_servers_list = []
if isinstance(expiration_date, list):
expiration_date = expiration_date[0]
expiration_date = (expiration_date + parse_time_string(Config('timezone_offset', '+8'))).timestamp()

return f'''\
{msg.locale.t('whois.message.domain_name')}{process(domain_name).lower()}{f"""
Expand All @@ -82,7 +76,8 @@ async def get_whois(msg, domain):
{msg.locale.t('whois.message.updated_date')}{msg.ts2strftime(updated_date)}""" if updated_date else ''}{f"""
{msg.locale.t('whois.message.creation_date')}{msg.ts2strftime(creation_date)}""" if creation_date else ''}{f"""
{msg.locale.t('whois.message.expiration_date')}{msg.ts2strftime(expiration_date)}""" if expiration_date else ''}{f"""
{msg.locale.t('whois.message.name_servers')}{', '.join(name_servers_list)}""" if name_servers else ''}{f"""
{msg.locale.t('whois.message.name_servers')}{process(name_servers)}""" if name_servers else ''}{f"""
{msg.locale.t('whois.message.email')}{process(emails)}""" if emails else ''}{f"""
{msg.locale.t('whois.message.dnssec')}{process(dnssec)}""" if dnssec else ''}{f"""
{msg.locale.t('whois.message.name')}{process(name)}""" if name else ''}{f"""
{msg.locale.t('whois.message.organization')}{process(org)}""" if org else ''}{f"""
Expand Down
1 change: 1 addition & 0 deletions modules/whois/locales/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"whois.message.creation_date": "Registration Date: ",
"whois.message.dnssec": "DNSSEC: ",
"whois.message.domain_name": "Domain Name: ",
"whois.message.email": "Email: ",
"whois.message.expiration_date": "Expiration Date: ",
"whois.message.get_failed": "Failed to fetch WHOIS.",
"whois.message.location": "Location: ",
Expand Down
1 change: 1 addition & 0 deletions modules/whois/locales/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"whois.message.creation_date": "注册日期:",
"whois.message.dnssec": "DNSSEC:",
"whois.message.domain_name": "域名名称:",
"whois.message.email": "邮箱:",
"whois.message.expiration_date": "截止日期:",
"whois.message.get_failed": "获取 WHOIS 失败。",
"whois.message.location": "位置:",
Expand Down
1 change: 1 addition & 0 deletions modules/whois/locales/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"whois.message.creation_date": "註冊日期:",
"whois.message.dnssec": "DNSSEC:",
"whois.message.domain_name": "網域名稱:",
"whois.message.email": "電郵:",
"whois.message.expiration_date": "截止日期:",
"whois.message.get_failed": "取得 WHOIS 失敗。",
"whois.message.location": "位置:",
Expand Down

0 comments on commit e9d7710

Please sign in to comment.