2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_URBANDICTIONARY = "http://api.urbandictionary.com/v0/define"
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-04-27 21:27:36 +00:00
|
|
|
_name = "UrbanDictionary"
|
|
|
|
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.ud", alias_of="urbandictionary")
|
|
|
|
@utils.hook("received.command.urbandictionary", min_args=1)
|
2016-03-29 11:56:58 +00:00
|
|
|
def ud(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get the definition of a provided term from Urban Dictionary
|
2019-09-20 13:19:06 +00:00
|
|
|
:usage: <term> [#<number>]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
number = 1
|
2019-09-20 13:11:09 +00:00
|
|
|
term = event["args_split"]
|
|
|
|
if (event["args_split"][-1].startswith("#") and
|
|
|
|
len(event["args_split"]) > 1 and
|
|
|
|
event["args_split"][-1][1:].isdigit()):
|
|
|
|
number = int(event["args_split"][-1][1:])
|
|
|
|
term = term[:-1]
|
|
|
|
term = " ".join(term)
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_URBANDICTIONARY,
|
2018-10-03 12:22:37 +00:00
|
|
|
get_params={"term": term}, json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if len(page.data["list"]):
|
|
|
|
if number > 0 and len(page.data["list"]) > number-1:
|
|
|
|
definition = page.data["list"][number-1]
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write("%s: %s" % (definition["word"],
|
|
|
|
definition["definition"].replace("\n", " ").replace(
|
|
|
|
"\r", "").replace(" ", " ")))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Definition number does not exist")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No results found")
|
|
|
|
else:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|