22whois.py
33Provides a command to allow users to look up information on domain names.
44"""
5-
6- import pythonwhois
5+ import sys
76from contextlib import suppress
87
98from cloudbot import hook
109
10+ if sys .version_info < (3 , 7 , 0 ):
11+ import pythonwhois
12+ else :
13+ pythonwhois = None
14+
1115
1216@hook .command
1317def whois (text , reply ):
14- """<domain> -- Does a whois query on <domain>."""
18+ """<domain> - Does a whois query on <domain>."""
19+ if pythonwhois is None :
20+ return "The pythonwhois library does not work on this version of Python."
21+
1522 domain = text .strip ().lower ()
1623
1724 try :
@@ -24,13 +31,16 @@ def whois(text, reply):
2431
2532 # We suppress errors here because different domains provide different data fields
2633 with suppress (KeyError ):
27- info .append (" \x02 Registrar \x02 : {}" . format ( data ["registrar" ][0 ]))
34+ info .append (( "Registrar" , data ["registrar" ][0 ]))
2835
2936 with suppress (KeyError ):
30- info .append (" \x02 Registered \x02 : {}" . format ( data ["creation_date" ][0 ].strftime ("%d-%m-%Y" )))
37+ info .append (( "Registered" , data ["creation_date" ][0 ].strftime ("%d-%m-%Y" )))
3138
3239 with suppress (KeyError ):
33- info .append ("\x02 Expires\x02 : {}" .format (data ["expiration_date" ][0 ].strftime ("%d-%m-%Y" )))
40+ info .append (("Expires" , data ["expiration_date" ][0 ].strftime ("%d-%m-%Y" )))
41+
42+ if not info :
43+ return "No information returned."
3444
35- info_text = ", " .join (info )
45+ info_text = ", " .join (" \x02 {name} \x02 : {info}" . format ( name = name , info = i ) for name , i in info )
3646 return "{} - {}" .format (domain , info_text )
0 commit comments