[ddg] Dusting + !safeoff handling

This commit is contained in:
nemunaire 2015-07-06 22:10:55 +02:00
parent 787a5fd3da
commit b75c54419f
2 changed files with 23 additions and 20 deletions

View File

@ -8,12 +8,12 @@ from nemubot.tools.xmlparser import parse_string
class DDGSearch:
def __init__(self, terms):
def __init__(self, terms, safeoff=False):
self.terms = terms
self.ddgres = web.getXML(
"https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1" %
quote(terms),
"https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1%s" %
(quote(terms), "&kp=-1" if safeoff else ""),
timeout=10)
@property

View File

@ -5,6 +5,7 @@
import imp
from nemubot import context
from nemubot.exception import IRCException
from nemubot.hooks import hook
nemubotversion = 3.4
@ -16,25 +17,26 @@ from . import UrbanDictionnary
@hook("cmd_hook", "define")
def define(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to define",
msg.channel, nick=msg.nick)
if not len(msg.args):
raise IRCException("Indicate a term to define")
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
s = DDGSearch.DDGSearch(' '.join(msg.args))
res = Response(channel=msg.channel)
return Response(s.definition, channel=msg.channel)
res.append_message(s.definition)
return res
@hook("cmd_hook", "search")
def search(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to search",
msg.channel, nick=msg.nick)
if not len(msg.args):
raise IRCException("Indicate a term to search")
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
if "!safeoff" in msg.args:
msg.args.remove("!safeoff")
safeoff = True
else:
safeoff = False
s = DDGSearch.DDGSearch(' '.join(msg.args), safeoff)
res = Response(channel=msg.channel, nomore="No more results",
count=" (%d more results)")
@ -47,21 +49,22 @@ def search(msg):
for rt in s.relatedTopics:
res.append_message(rt)
res.append_message(s.definition)
return res
@hook("cmd_hook", "urbandictionnary")
def udsearch(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to search",
msg.channel, nick=msg.nick)
if not len(msg.args):
raise IRCException("Indicate a term to search")
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.cmds[1:]))
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.args))
res = Response(channel=msg.channel, nomore="No more results",
count=" (%d more definitions)")
for d in s.definitions:
res.append_message(d)
res.append_message(d.replace("\n", " "))
return res