2012-11-04 14:32:39 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
import re
|
2012-11-07 15:37:19 +00:00
|
|
|
from urllib.parse import urlparse
|
2012-12-07 00:13:11 +00:00
|
|
|
from urllib.parse import quote
|
2012-11-07 15:37:19 +00:00
|
|
|
from urllib.request import urlopen
|
2012-11-04 14:32:39 +00:00
|
|
|
|
|
|
|
nemubotversion = 3.3
|
|
|
|
|
|
|
|
def help_tiny ():
|
|
|
|
"""Line inserted in the response to the command !help"""
|
|
|
|
return "Gets YCC urls"
|
|
|
|
|
|
|
|
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."
|
|
|
|
|
|
|
|
def load(context):
|
|
|
|
from hooks import Hook
|
|
|
|
add_hook("cmd_hook", Hook(cmd_ycc, "ycc"))
|
|
|
|
add_hook("all_post", Hook(parseresponse))
|
|
|
|
|
|
|
|
LAST_URLS = dict()
|
|
|
|
|
|
|
|
def gen_response(res, msg, srv):
|
|
|
|
if res is None:
|
|
|
|
return Response(msg.sender, "La situation est embarassante, il semblerait que YCC soit down :(", msg.channel)
|
|
|
|
elif isinstance(res, str):
|
|
|
|
return Response(msg.sender, "URL pour %s : %s" % (srv, res), msg.channel)
|
|
|
|
else:
|
|
|
|
return Response(msg.sender, "Mauvaise URL : %s" % srv, msg.channel)
|
|
|
|
|
|
|
|
def cmd_ycc(msg):
|
|
|
|
if len(msg.cmds) == 1:
|
|
|
|
global LAST_URLS
|
|
|
|
if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0:
|
|
|
|
msg.cmds.append(LAST_URLS[msg.channel].pop())
|
|
|
|
else:
|
|
|
|
return Response(msg.sender, "Je n'ai pas d'autre URL à réduire.", msg.channel)
|
|
|
|
|
|
|
|
if len(msg.cmds) < 6:
|
|
|
|
res = list()
|
|
|
|
for url in msg.cmds[1:]:
|
2012-11-07 15:37:19 +00:00
|
|
|
o = urlparse(url, "http")
|
|
|
|
if o.scheme != "":
|
2012-12-07 00:13:11 +00:00
|
|
|
snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%#@&=?")
|
|
|
|
print_debug(snd_url)
|
|
|
|
raw = urlopen(snd_url, timeout=10)
|
2012-11-07 15:37:19 +00:00
|
|
|
if o.netloc == "":
|
|
|
|
res.append(gen_response(raw.read().decode(), msg, o.scheme))
|
|
|
|
else:
|
|
|
|
res.append(gen_response(raw.read().decode(), msg, o.netloc))
|
2012-11-04 14:32:39 +00:00
|
|
|
else:
|
|
|
|
res.append(gen_response(False, msg, url))
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return Response(msg.sender, "je ne peux pas réduire autant d'URL "
|
|
|
|
"d'un seul coup.", msg.channel, nick=msg.nick)
|
|
|
|
|
|
|
|
def parselisten(msg):
|
|
|
|
global LAST_URLS
|
2012-11-28 14:00:01 +00:00
|
|
|
urls = re.findall("([a-zA-Z0-9+.-]+:(//)?[^ ]+)", msg.content)
|
|
|
|
for (url, osef) in urls:
|
2012-11-07 15:37:19 +00:00
|
|
|
o = urlparse(url)
|
|
|
|
if o.scheme != "":
|
2012-11-07 16:19:08 +00:00
|
|
|
if o.netloc == "ycc.fr" or (o.netloc == "" and len(o.path) < 10):
|
2012-11-28 14:00:01 +00:00
|
|
|
continue
|
2012-11-06 03:14:11 +00:00
|
|
|
if msg.channel not in LAST_URLS:
|
|
|
|
LAST_URLS[msg.channel] = list()
|
2012-11-07 15:37:19 +00:00
|
|
|
LAST_URLS[msg.channel].append(o.geturl())
|
2012-11-04 14:32:39 +00:00
|
|
|
return False
|
2012-11-06 03:14:11 +00:00
|
|
|
|
2012-11-04 14:32:39 +00:00
|
|
|
def parseresponse(res):
|
|
|
|
parselisten(res)
|
|
|
|
return True
|