YCC module: dusting & anchors are correctly passed; fixes #59

This commit is contained in:
nemunaire 2014-07-25 16:38:01 +02:00
parent d16f57f8d5
commit 1a3912cc4f

View File

@ -8,11 +8,11 @@ from urllib.request import urlopen
nemubotversion = 3.3 nemubotversion = 3.3
def help_tiny (): def help_tiny ():
"""Line inserted in the response to the command !help""" """Line inserted in the response to the command !help"""
return "Gets YCC urls" return "Gets YCC urls"
def help_full (): def help_full ():
return "!ycc [<url>]: with an argument, reduce the given <url> thanks to ycc.fr; without argument, reduce the last URL said on the current channel." return "!ycc [<url>]: with an argument, reduce the given <url> thanks to ycc.fr; without argument, reduce the last URL said on the current channel."
def load(context): def load(context):
from hooks import Hook from hooks import Hook
@ -23,11 +23,11 @@ LAST_URLS = dict()
def gen_response(res, msg, srv): def gen_response(res, msg, srv):
if res is None: if res is None:
return Response(msg.sender, "La situation est embarassante, il semblerait que YCC soit down :(", msg.channel) raise IRCException("la situation est embarassante, il semblerait que YCC soit down :(")
elif isinstance(res, str): elif isinstance(res, str):
return Response(msg.sender, "URL pour %s : %s" % (srv, res), msg.channel) return Response(msg.sender, "URL pour %s : %s" % (srv, res), msg.channel)
else: else:
return Response(msg.sender, "Mauvaise URL : %s" % srv, msg.channel) raise IRCException("mauvaise URL : %s" % srv)
def cmd_ycc(msg): def cmd_ycc(msg):
if len(msg.cmds) == 1: if len(msg.cmds) == 1:
@ -35,26 +35,25 @@ def cmd_ycc(msg):
if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0: if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0:
msg.cmds.append(LAST_URLS[msg.channel].pop()) msg.cmds.append(LAST_URLS[msg.channel].pop())
else: else:
return Response(msg.sender, "Je n'ai pas d'autre URL à réduire.", msg.channel) raise IRCException("je n'ai pas d'autre URL à réduire.")
if len(msg.cmds) < 6: if len(msg.cmds) > 5:
res = list() raise IRCException("je ne peux pas réduire autant d'URL d'un seul coup.")
for url in msg.cmds[1:]:
o = urlparse(url, "http") res = list()
if o.scheme != "": for url in msg.cmds[1:]:
snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%#@&=?") o = urlparse(url, "http")
print_debug(snd_url) if o.scheme != "":
raw = urlopen(snd_url, timeout=10) snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%@&=?")
if o.netloc == "": print_debug(snd_url)
res.append(gen_response(raw.read().decode(), msg, o.scheme)) raw = urlopen(snd_url, timeout=10)
else: if o.netloc == "":
res.append(gen_response(raw.read().decode(), msg, o.netloc)) res.append(gen_response(raw.read().decode(), msg, o.scheme))
else: else:
res.append(gen_response(False, msg, url)) res.append(gen_response(raw.read().decode(), msg, o.netloc))
return res else:
else: res.append(gen_response(False, msg, url))
return Response(msg.sender, "je ne peux pas réduire autant d'URL " return res
"d'un seul coup.", msg.channel, nick=msg.nick)
def parselisten(msg): def parselisten(msg):
global LAST_URLS global LAST_URLS
@ -67,9 +66,9 @@ def parselisten(msg):
continue continue
if msg.channel not in LAST_URLS: if msg.channel not in LAST_URLS:
LAST_URLS[msg.channel] = list() LAST_URLS[msg.channel] = list()
LAST_URLS[msg.channel].append(o.geturl()) LAST_URLS[msg.channel].append(url)
except: except:
pass pass
return False return False
def parseresponse(res): def parseresponse(res):