phenny/modules/urbandict.py

46 lines
1.1 KiB
Python
Raw Permalink Normal View History

2011-12-26 22:16:06 -05:00
#!/usr/bin/python3
"""
urbandict.py - urban dictionary module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
from tools import GrumbleError
2011-12-26 22:16:06 -05:00
import web
import json
2013-06-09 01:27:24 -04:00
2011-12-26 22:16:06 -05:00
def urbandict(phenny, input):
""".urb <word> - Search Urban Dictionary for a definition."""
word = input.group(2)
if not word:
2012-05-14 16:49:41 -04:00
phenny.say(urbandict.__doc__.strip())
2011-12-26 22:16:06 -05:00
return
try:
2013-06-09 01:27:24 -04:00
data = web.get(
"http://api.urbandictionary.com/v0/define?term={0}".format(
web.quote(word)))
2012-06-02 00:34:50 -04:00
data = json.loads(data)
2013-06-09 01:27:24 -04:00
except:
raise GrumbleError(
2013-06-09 01:27:24 -04:00
"Urban Dictionary slemped out on me. Try again in a minute.")
2011-12-26 22:16:06 -05:00
2018-07-30 22:53:37 -04:00
results = data['list']
if not results:
2011-12-26 22:16:06 -05:00
phenny.say("No results found for {0}".format(word))
return
2011-12-26 22:16:06 -05:00
2018-07-30 22:53:37 -04:00
result = results[0]
2013-06-09 01:27:24 -04:00
url = 'http://www.urbandictionary.com/define.php?term={0}'.format(
web.quote(word))
2011-12-26 22:16:06 -05:00
response = "{0} - {1}".format(result['definition'].strip()[:256], url)
2011-12-26 22:16:06 -05:00
phenny.say(response)
2013-11-28 22:23:32 -05:00
urbandict.name = 'urb'
2011-12-26 22:16:06 -05:00
urbandict.rule = (['urb'], r'(.*)')
if __name__ == '__main__':
print(__doc__.strip())