[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: class DDGSearch:
def __init__(self, terms): def __init__(self, terms, safeoff=False):
self.terms = terms self.terms = terms
self.ddgres = web.getXML( self.ddgres = web.getXML(
"https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1" % "https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1%s" %
quote(terms), (quote(terms), "&kp=-1" if safeoff else ""),
timeout=10) timeout=10)
@property @property

View File

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