2016-03-30 18:31:23 +00:00
|
|
|
#--require-config wordnik-api-key
|
|
|
|
|
2018-09-23 08:52:53 +00:00
|
|
|
import time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, 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
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
_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-10-03 12:22:37 +00:00
|
|
|
page = utils.http.get_url(URL_WORDNIK % word, get_params={
|
2018-09-23 08:52:53 +00:00
|
|
|
"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-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.define")
|
2016-03-29 11:56:58 +00:00
|
|
|
def define(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Define a provided term
|
|
|
|
:usage: <phrase>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["args"]:
|
|
|
|
word = event["args"]
|
|
|
|
else:
|
2018-09-30 10:43:34 +00:00
|
|
|
word = event["target"].buffer.get(from_self=False)
|
2018-10-03 15:45:19 +00:00
|
|
|
word = word.replace(" ", "+")
|
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:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|
2018-09-23 08:52:53 +00:00
|
|
|
|
2018-10-03 12:22:37 +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
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Define a random word
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
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
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
page = utils.http.get_url(URL_WORDNIK_RANDOM, get_params={
|
2018-09-23 08:52:53 +00:00
|
|
|
"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-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Try again in a couple of seconds")
|
|
|
|
|
2018-09-24 12:23:59 +00:00
|
|
|
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
|
|
|
page["word"], definition["text"]))
|
2018-09-23 08:52:53 +00:00
|
|
|
else:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|
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")
|