2016-03-30 18:31:23 +00:00
|
|
|
#--require-config wordnik-api-key
|
|
|
|
|
2018-09-23 08:52:53 +00:00
|
|
|
import time
|
2018-09-24 14:13:27 +00:00
|
|
|
from src import Utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-23 08:52:53 +00:00
|
|
|
URL_WORDNIK = "https://api.wordnik.com/v4/word.json/%s/definitions"
|
|
|
|
URL_WORDNIK_RANDOM = "https://api.wordnik.com/v4/words.json/randomWord"
|
|
|
|
|
|
|
|
RANDOM_DELAY_SECONDS = 3
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-09-24 12:24:36 +00:00
|
|
|
self._last_called = 0
|
2018-09-23 08:52:53 +00:00
|
|
|
|
2018-09-24 12:23:59 +00:00
|
|
|
def _get_definition(self, word):
|
2018-09-23 08:52:53 +00:00
|
|
|
word = event["args"] if "args" in event else event
|
|
|
|
|
|
|
|
page = Utils.get_url(URL_WORDNIK % word, get_params={
|
|
|
|
"useCanonical": "true", "limit": 1,
|
|
|
|
"sourceDictionaries": "wiktionary", "api_key": self.bot.config[
|
2018-09-24 12:23:59 +00:00
|
|
|
"wordnik-api-key"]}, json=True)
|
2018-09-23 08:52:53 +00:00
|
|
|
|
|
|
|
return page
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.define", usage="<phrase>")
|
2016-03-29 11:56:58 +00:00
|
|
|
def define(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Define a provided term
|
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["args"]:
|
|
|
|
word = event["args"]
|
|
|
|
else:
|
2018-08-28 11:23:57 +00:00
|
|
|
word = event["buffer"].get(from_self=False)
|
2018-09-24 12:23:59 +00:00
|
|
|
|
|
|
|
page = self._get_definition(word)
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
|
|
|
if len(page):
|
|
|
|
event["stdout"].write("%s: %s" % (page[0]["word"],
|
|
|
|
page[0]["text"]))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No definitions found")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|
2018-09-23 08:52:53 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("received.command.randomword")
|
2018-09-23 08:52:53 +00:00
|
|
|
def random_word(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Define a random word
|
|
|
|
"""
|
2018-09-24 12:24:36 +00:00
|
|
|
if not self._last_called or (time.time()-self._last_called >=
|
2018-09-24 12:23:59 +00:00
|
|
|
RANDOM_DELAY_SECONDS):
|
2018-09-24 12:24:36 +00:00
|
|
|
self._last_called = time.time()
|
2018-09-23 08:52:53 +00:00
|
|
|
|
|
|
|
page = Utils.get_url(URL_WORDNIK_RANDOM, get_params={
|
|
|
|
"api_key":self.bot.config["wordnik-api-key"],
|
|
|
|
"min_dictionary_count":1},json=True)
|
2018-09-24 12:23:59 +00:00
|
|
|
if page and len(page):
|
|
|
|
definition = self._get_definition(page["word"])
|
|
|
|
if len(definition):
|
|
|
|
definition = definition[0]
|
2018-09-23 08:52:53 +00:00
|
|
|
else:
|
2018-09-24 12:23:59 +00:00
|
|
|
event["stderr"].write("Try again in a couple of "
|
|
|
|
"seconds")
|
|
|
|
return
|
|
|
|
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
|
|
|
page["word"], definition["text"]))
|
2018-09-23 08:52:53 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|
|
|
|
else:
|
2018-09-24 12:23:59 +00:00
|
|
|
event["stderr"].write("Try again in a couple of seconds")
|