[ddg] Split the module in two: ddg for search and urbandict for urbandictionnary

This commit is contained in:
nemunaire 2015-10-24 14:44:16 +02:00
commit 2b96c32063
5 changed files with 175 additions and 171 deletions

37
modules/urbandict.py Normal file
View file

@ -0,0 +1,37 @@
"""Search definition from urbandictionnary"""
# PYTHON STUFFS #######################################################
from urllib.parse import quote
from nemubot.exception import IRCException
from nemubot.hooks import hook
from nemubot.tools import web
from more import Response
# MODULE CORE #########################################################
def search(terms):
return web.getJSON(
"http://api.urbandictionary.com/v0/define?term=%s"
% quote(' '.join(terms)))
# MODULE INTERFACE ####################################################
@hook("cmd_hook", "urbandictionnary")
def udsearch(msg):
if not len(msg.args):
raise IRCException("Indicate a term to search")
s = search(msg.args)
res = Response(channel=msg.channel, nomore="No more results",
count=" (%d more definitions)")
for i in s["list"]:
res.append_message(i["definition"].replace("\n", " "),
title=i["word"])
return res