sopel-plugins/urbandict.py

35 lines
1.2 KiB
Python

#!/usr/bin/python3
# coding=utf-8
#urbandict.py - urban dictionary module
#author: mutantmonkey <mutantmonkey@mutantmonkey.in>
# https://pypi.org/project/wikiquote/
# https://pypi.org/project/sopel/
# modified by xmszkn(at)disroot.org
# GPLv3 license
import requests
from sopel import plugin
@plugin.command('urb', 'urban')
@plugin.example('.urb word')
def urbandict(bot, trigger):
word = trigger.group(2)
if not word:
return bot.say("napisz .urb słowo")
try:
data = requests.get("http://api.urbandictionary.com/v0/define?term={0}".format(requests.utils.quote(word))).json()
except:
return bot.say("Error connecting to urban dictionary")
if not data['list']:
return bot.say("No results found for {0}".format(word))
try:
result = list(filter(lambda x: x['word'].lower() == word.lower(), data['list']))[0]
except:
return bot.say("No results found for {0}".format(word))
url = 'http://{}.urbanup.com'.format(word.replace(' ','-'))
maxdesc = 420 - len(url)
response = "{0} - {1}".format((result['definition'].replace("[","").replace("]","").strip()[:maxdesc])+"...", url)
bot.say(response)