From a4c57fc40ff9f896ceefe86c13cbe155e6d40075 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sun, 14 Apr 2013 20:57:26 -0500 Subject: [PATCH 1/3] A new UrbanDictionary plugin to obtain correct definitions of words. --- commands/dictionary.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 commands/dictionary.py diff --git a/commands/dictionary.py b/commands/dictionary.py new file mode 100644 index 0000000..961c41e --- /dev/null +++ b/commands/dictionary.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# +# Public domain, 2013 Legoktm +# + +from json import loads +from urllib import quote +from urllib2 import urlopen + +from earwigbot.commands import Command + +class UrbanDictionary(Command): + """Get the real definition of a word.""" + name = "urban" + commands = ["dictt", "definee", "defne", "dct", "ud"] + + def process(self, data): + if not data.args: + return + q = ' '.join(data.args) + + url = "http://api.urbandictionary.com/v0/define?term={}" + q = quote(q, safe="") + query = urlopen(url.format(q)).read() + res = loads(query) + if res['result_type'] == 'exact': + defin = res['list'][0] + msg = 'Definition: {definition}; example: {example}'.format(**defin) + self.reply(data, msg) + elif res['result_type'] == 'fulltext': + l = [i['word'] for i in res['list']] + msg = 'Here are some close matches...: {}'.format(', '.join(l)) + self.reply(data, msg) + else: + self.reply(data, 'Sorry, no results found :(') + From b57fd37f4995013207644d6fb470bc8c81df14dd Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sun, 14 Apr 2013 21:02:22 -0500 Subject: [PATCH 2/3] Python2.6 compatability --- commands/dictionary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/dictionary.py b/commands/dictionary.py index 961c41e..6b18672 100644 --- a/commands/dictionary.py +++ b/commands/dictionary.py @@ -29,7 +29,7 @@ class UrbanDictionary(Command): self.reply(data, msg) elif res['result_type'] == 'fulltext': l = [i['word'] for i in res['list']] - msg = 'Here are some close matches...: {}'.format(', '.join(l)) + msg = 'Here are some close matches...: {0}'.format(', '.join(l)) self.reply(data, msg) else: self.reply(data, 'Sorry, no results found :(') From 3c66a3d98bab8b6b99970274674dfddc2097f808 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sun, 14 Apr 2013 21:03:06 -0500 Subject: [PATCH 3/3] Fix the other one too --- commands/dictionary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/dictionary.py b/commands/dictionary.py index 6b18672..47f5b74 100644 --- a/commands/dictionary.py +++ b/commands/dictionary.py @@ -19,7 +19,7 @@ class UrbanDictionary(Command): return q = ' '.join(data.args) - url = "http://api.urbandictionary.com/v0/define?term={}" + url = "http://api.urbandictionary.com/v0/define?term={0}" q = quote(q, safe="") query = urlopen(url.format(q)).read() res = loads(query)