Skip to content

Commit 9bfdbca

Browse files
authored
Merge pull request CloudBotIRC#128 from linuxdaemon/gonzobot+whois-rewrite
Add error handling to whois.py
2 parents 8857dc7 + eb737ce commit 9bfdbca

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

plugins/whois.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
whois.py
33
Provides a command to allow users to look up information on domain names.
44
"""
5-
6-
import pythonwhois
5+
import sys
76
from contextlib import suppress
87

98
from 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
1317
def 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("\x02Registrar\x02: {}".format(data["registrar"][0]))
34+
info.append(("Registrar", data["registrar"][0]))
2835

2936
with suppress(KeyError):
30-
info.append("\x02Registered\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("\x02Expires\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

Comments
 (0)