aboutsummaryrefslogtreecommitdiff
path: root/modules/urbandictionary.py
diff options
context:
space:
mode:
authorGravatar jesopo2016-03-29 12:56:58 +0100
committerGravatar jesopo2016-03-29 12:56:58 +0100
commitf943d63098a50746f4e470e403a991a4d9713030 (patch)
treedeeb98058917d0155227211d72576f0cbab28d3f /modules/urbandictionary.py
parentInitial commit (diff)
first commit.
Diffstat (limited to 'modules/urbandictionary.py')
-rw-r--r--modules/urbandictionary.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/modules/urbandictionary.py b/modules/urbandictionary.py
new file mode 100644
index 00000000..a9815617
--- /dev/null
+++ b/modules/urbandictionary.py
@@ -0,0 +1,34 @@
+import json, re
+import Utils
+
+URL_URBANDICTIONARY = "http://api.urbandictionary.com/v0/define"
+REGEX_DEFNUMBER = re.compile("-n(\d+) \S+")
+
+class Module(object):
+ def __init__(self, bot):
+ bot.events.on("received").on("command").on("urbandictionary", "ud"
+ ).hook(self.ud, min_args=1,
+ help="Get the definition of a provided term")
+
+ def ud(self, event):
+ term = event["args"]
+ number = 1
+ match = re.match(REGEX_DEFNUMBER, term)
+ if match:
+ number = int(match.group(1))
+ term = term.split(" ", 1)[1]
+ page = Utils.get_url(URL_URBANDICTIONARY, get_params={"term": term},
+ json=True)
+ if page:
+ if len(page["list"]):
+ if number > 0 and len(page["list"]) > number-1:
+ definition = page["list"][number-1]
+ 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:
+ event["stderr"].write("Failed to load results")