Kunal Mehta před 11 roky
rodič
revize
9ab16a8d6a
1 změnil soubory, kde provedl 36 přidání a 0 odebrání
  1. +36
    -0
      commands/dictionary.py

+ 36
- 0
commands/dictionary.py Zobrazit soubor

@@ -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={0}"
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...: {0}'.format(', '.join(l))
self.reply(data, msg)
else:
self.reply(data, 'Sorry, no results found :(')


Načítá se…
Zrušit
Uložit