2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
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-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(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
|
|
|
|
2019-07-03 06:59:12 +00:00
|
|
|
if page:
|
|
|
|
if page.code == 200:
|
|
|
|
return True, page.data[0]
|
|
|
|
else:
|
|
|
|
return True, None
|
|
|
|
else:
|
|
|
|
return False, None
|
2018-09-23 08:52:53 +00:00
|
|
|
|
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
|
|
|
|
2019-07-03 06:59:12 +00:00
|
|
|
success, definition = self._get_definition(word)
|
|
|
|
if success:
|
|
|
|
if not definition == None:
|
|
|
|
text = utils.http.strip_html(definition["text"])
|
|
|
|
event["stdout"].write("%s: %s" % (definition["word"], text))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No definitions found")
|
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|
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-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(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-12-11 22:26:38 +00:00
|
|
|
if page and len(page.data):
|
2019-07-03 06:59:12 +00:00
|
|
|
success, definition = self._get_definition(page.data["word"])
|
|
|
|
if not success:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Try again in a couple of seconds")
|
|
|
|
|
2019-06-05 11:05:51 +00:00
|
|
|
text = utils.http.strip_html(definition["text"])
|
2018-09-24 12:23:59 +00:00
|
|
|
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
2019-06-05 11:05:51 +00:00
|
|
|
page.data["word"], text))
|
2018-09-23 08:52:53 +00:00
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|
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")
|