bitbot-3.11-fork/modules/thesaurus.py

51 lines
2 KiB
Python
Raw Normal View History

#--depends-on commands
#--require-config bighugethesaurus-api-key
from src import ModuleManager, utils
2016-03-29 11:56:58 +00:00
URL_THESAURUS = "http://words.bighugelabs.com/api/2/%s/%s/json"
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.synonym", min_args=1)
@utils.hook("received.command.antonym", min_args=1)
2016-03-29 11:56:58 +00:00
def thesaurus(self, event):
"""
:help: Get synonyms/antonyms for a provided phrase
:usage: <word> [type]
"""
2016-03-29 11:56:58 +00:00
phrase = event["args_split"][0]
page = utils.http.request(URL_THESAURUS % (
self.bot.config["bighugethesaurus-api-key"], phrase))
2016-03-29 11:56:58 +00:00
syn_ant = event["command"][:3]
if page:
2018-12-16 11:03:36 +00:00
if page.code == 404:
raise utils.EventError("Word not found")
page = page.json()
2018-12-16 11:03:36 +00:00
2016-03-29 11:56:58 +00:00
if not len(event["args_split"]) > 1:
word_types = []
for word_type in page.keys():
if syn_ant in page[word_type]:
2016-03-29 11:56:58 +00:00
word_types.append(word_type)
if word_types:
word_types = sorted(word_types)
event["stdout"].write(
"Available categories for %s: %s" % (
phrase, ", ".join(word_types)))
else:
event["stderr"].write("No categories available")
else:
category = event["args_split"][1].lower()
if category in page:
if syn_ant in page[category]:
2016-03-29 11:56:58 +00:00
event["stdout"].write("%ss for %s: %s" % (
event["command"].title(), phrase, ", ".join(
page[category][syn_ant])))
2016-03-29 11:56:58 +00:00
else:
event["stderr"].write("No %ss for %s" % (
event["command"], phrase))
else:
event["stderr"].write("Category not found")
else:
raise utils.EventResultsError()