Refactor define.py
This commit is contained in:
parent
967ed65e00
commit
dd0ab3211e
1 changed files with 15 additions and 25 deletions
|
@ -20,10 +20,9 @@ class Module(object):
|
||||||
events.on("received.command.randomword").hook(self.random_word,
|
events.on("received.command.randomword").hook(self.random_word,
|
||||||
help="Generate a random word!")
|
help="Generate a random word!")
|
||||||
|
|
||||||
def get_definition(self, event):
|
def _get_definition(self, word):
|
||||||
word = event["args"] if "args" in event else event
|
word = event["args"] if "args" in event else event
|
||||||
|
|
||||||
|
|
||||||
page = Utils.get_url(URL_WORDNIK % word, get_params={
|
page = Utils.get_url(URL_WORDNIK % word, get_params={
|
||||||
"useCanonical": "true", "limit": 1,
|
"useCanonical": "true", "limit": 1,
|
||||||
"sourceDictionaries": "wiktionary", "api_key": self.bot.config[
|
"sourceDictionaries": "wiktionary", "api_key": self.bot.config[
|
||||||
|
@ -36,7 +35,8 @@ class Module(object):
|
||||||
word = event["args"]
|
word = event["args"]
|
||||||
else:
|
else:
|
||||||
word = event["buffer"].get(from_self=False)
|
word = event["buffer"].get(from_self=False)
|
||||||
page = self.get_definition(event)
|
|
||||||
|
page = self._get_definition(word)
|
||||||
if page:
|
if page:
|
||||||
if len(page):
|
if len(page):
|
||||||
event["stdout"].write("%s: %s" % (page[0]["word"],
|
event["stdout"].write("%s: %s" % (page[0]["word"],
|
||||||
|
@ -49,32 +49,22 @@ class Module(object):
|
||||||
def random_word(self, event):
|
def random_word(self, event):
|
||||||
if not self.last_called or (time.time()-self.last_called >=
|
if not self.last_called or (time.time()-self.last_called >=
|
||||||
RANDOM_DELAY_SECONDS):
|
RANDOM_DELAY_SECONDS):
|
||||||
|
|
||||||
self.last_called = time.time()
|
self.last_called = time.time()
|
||||||
|
|
||||||
page = Utils.get_url(URL_WORDNIK_RANDOM, get_params={
|
page = Utils.get_url(URL_WORDNIK_RANDOM, get_params={
|
||||||
"api_key":self.bot.config["wordnik-api-key"],
|
"api_key":self.bot.config["wordnik-api-key"],
|
||||||
"min_dictionary_count":1},json=True)
|
"min_dictionary_count":1},json=True)
|
||||||
if page:
|
if page and len(page):
|
||||||
if len(page):
|
definition = self._get_definition(page["word"])
|
||||||
definition = self.get_definition(page["word"])
|
|
||||||
|
|
||||||
if len(definition):
|
if len(definition):
|
||||||
definition = definition[0]
|
definition = definition[0]
|
||||||
else:
|
else:
|
||||||
self.events.on("send.stderr").call(module_name="Random",
|
event["stderr"].write("Try again in a couple of "
|
||||||
target=event["target"],
|
"seconds")
|
||||||
message="Try again in a couple of seconds")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
event["stdout"].set_prefix("Random")
|
|
||||||
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
event["stdout"].write("Random Word: %s - Definition: %s" % (
|
||||||
page["word"], definition["text"]))
|
page["word"], definition["text"]))
|
||||||
else:
|
|
||||||
event["stderr"].write("Something has gone terribly wrong")
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Failed to load results")
|
event["stderr"].write("Failed to load results")
|
||||||
else:
|
else:
|
||||||
self.events.on("send.stderr").call(module_name="Random",
|
event["stderr"].write("Try again in a couple of seconds")
|
||||||
target=event["target"],
|
|
||||||
message="Try again in a couple of seconds")
|
|
||||||
|
|
Loading…
Reference in a new issue