Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/whois/__init__.py

78 lines
3 KiB
Python
Raw Normal View History

2023-08-23 18:16:14 +00:00
import datetime
2023-08-23 17:30:09 +00:00
from whois import whois
from core.builtins import Bot, Plain, Image
from core.component import module
from core.utils.image import msgchain2image
2023-08-23 19:05:24 +00:00
def process(input):
2023-08-24 05:09:07 +00:00
if isinstance(input, list) and len(input) > 0:
return input[0]
else:
return input
def get_value(dict, key):
if key in dict:
return dict[key]
else:
return None
2023-08-23 19:10:48 +00:00
2023-08-23 19:05:24 +00:00
2023-08-23 17:30:09 +00:00
w = module('whois', desc='{whois.help.desc}',
developers=['DoroWolf'])
@w.handle('<domain>')
async def _(msg: Bot.MessageSession, domain: str):
res = await get_whois(msg, domain)
output = await msg.finish(res)
img = await msgchain2image([Plain(output)])
await msg.finish([Image(img)])
2023-08-23 18:44:49 +00:00
2023-08-23 17:30:09 +00:00
async def get_whois(msg, domain):
try:
info = whois(domain)
except:
2023-08-24 05:21:52 +00:00
await msg.finish(msg.locale.t("whois.message.get_failed"))
2023-08-23 17:30:09 +00:00
2023-08-24 05:13:12 +00:00
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')
2023-08-23 17:30:09 +00:00
2023-08-24 05:21:52 +00:00
if not domain_name:
await msg.finish(msg.locale.t("whois.message.get_failed"))
2023-08-24 05:18:29 +00:00
if name_servers:
name_servers_list = list(set([i.lower() for i in name_servers]))
else:
name_servers_list = []
2023-08-23 17:30:09 +00:00
return f'''\
2023-08-24 05:15:43 +00:00
{msg.locale.t('whois.message.domain_name')}{process(domain_name).lower()}{f"""
2023-08-24 05:09:07 +00:00
{msg.locale.t('whois.message.registrar')}{registrar}""" if registrar else ''}{f"""
2023-08-24 05:10:30 +00:00
{msg.locale.t('whois.message.whois_server')}{whois_server}""" if whois_server else ''}{f"""
2023-08-24 05:09:07 +00:00
{msg.locale.t('whois.message.updated_date')}{str(process(updated_date))}""" if updated_date else ''}{f"""
{msg.locale.t('whois.message.creation_date')}{str(process(creation_date))}""" if creation_date else ''}{f"""
{msg.locale.t('whois.message.expiration_date')}{str(process(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.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"""
{msg.locale.t('ip.message.location')}{f"{process(address)}, " if address else ''}{f"{process(city)}, " if city else ''}{f"{process(state)}, " if state else ''}{process(country)}""" if country else ''}{f"""
{msg.locale.t('whois.message.postal_code')}{process(registrant_postal_code)}""" if registrant_postal_code else ''}'''