DDG module: new !urbandictionnary command that display results from Urban Dictionnary website; closes #29
This commit is contained in:
parent
eefcf96516
commit
08f3a31e88
2 changed files with 47 additions and 0 deletions
27
modules/ddg/UrbanDictionnary.py
Normal file
27
modules/ddg/UrbanDictionnary.py
Normal 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
|
||||
|
|
@ -5,6 +5,7 @@ import imp
|
|||
nemubotversion = 3.3
|
||||
|
||||
from . import DDGSearch
|
||||
from . import UrbanDictionnary
|
||||
from . import WFASearch
|
||||
from . import Wikipedia
|
||||
|
||||
|
|
@ -21,9 +22,11 @@ def load(context):
|
|||
add_hook("cmd_hook", Hook(calculate, "calc"))
|
||||
add_hook("cmd_hook", Hook(wiki, "dico"))
|
||||
add_hook("cmd_hook", Hook(wiki, "wiki"))
|
||||
add_hook("cmd_hook", Hook(udsearch, "urbandictionnary"))
|
||||
|
||||
def reload():
|
||||
imp.reload(DDGSearch)
|
||||
imp.reload(UrbanDictionnary)
|
||||
imp.reload(WFASearch)
|
||||
imp.reload(Wikipedia)
|
||||
|
||||
|
|
@ -65,6 +68,23 @@ def search(msg):
|
|||
return res
|
||||
|
||||
|
||||
def udsearch(msg):
|
||||
if len(msg.cmds) <= 1:
|
||||
return Response(msg.sender,
|
||||
"Indicate a term to search",
|
||||
msg.channel, nick=msg.nick)
|
||||
|
||||
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.cmds[1:]))
|
||||
|
||||
res = Response(msg.sender, channel=msg.channel, nomore="No more results",
|
||||
count=" (%d more definitions)")
|
||||
|
||||
for d in s.definitions:
|
||||
res.append_message(d)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def calculate(msg):
|
||||
if len(msg.cmds) <= 1:
|
||||
return Response(msg.sender,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue