DDG module: new !urbandictionnary command that display results from Urban Dictionnary website; closes #29

This commit is contained in:
nemunaire 2014-04-22 17:19:10 +02:00
commit 08f3a31e88
2 changed files with 47 additions and 0 deletions

View file

@ -0,0 +1,27 @@
# coding=utf-8
import json
from urllib.parse import quote
from urllib.request import urlopen
class UrbanDictionnary:
def __init__(self, terms):
self.terms = terms
raw = urlopen("http://api.urbandictionary.com/v0/define?term=%s" % quote(terms), timeout=10)
self.udres = json.loads(raw.read().decode())
@property
def result_type(self):
if self.udres and "result_type" in self.udres:
return self.udres["result_type"]
else:
return ""
@property
def definitions(self):
if self.udres and "list" in self.udres:
for d in self.udres["list"]:
yield d["definition"]
else:
yield "Sorry, no definition found for %s" % self.terms