2012-11-04 14:32:39 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""URL reducer module"""
|
|
|
|
|
2012-11-04 14:32:39 +00:00
|
|
|
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-04 14:32:39 +00:00
|
|
|
|
2015-01-04 22:57:09 +00:00
|
|
|
from nemubot.exception import IRCException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
2015-04-06 07:50:13 +00:00
|
|
|
from nemubot.message import Text
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.tools import web
|
2014-08-12 18:10:19 +00:00
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2012-11-04 14:32:39 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2014-11-13 01:51:49 +00:00
|
|
|
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.")
|
2012-11-04 14:32:39 +00:00
|
|
|
|
|
|
|
LAST_URLS = dict()
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-12-18 12:02:19 +00:00
|
|
|
def reduce(url):
|
|
|
|
"""Ask YCC website to reduce given URL
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
url -- the URL to reduce
|
|
|
|
"""
|
|
|
|
|
|
|
|
snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%@&=?")
|
|
|
|
return web.getURLContent(snd_url)
|
|
|
|
|
|
|
|
|
2012-11-04 14:32:39 +00:00
|
|
|
def gen_response(res, msg, srv):
|
|
|
|
if res is None:
|
2014-12-18 12:02:19 +00:00
|
|
|
raise IRCException("mauvaise URL : %s" % srv)
|
|
|
|
else:
|
2015-04-06 07:50:13 +00:00
|
|
|
return Text("URL pour %s : %s" % (srv, res), server=None,
|
|
|
|
to=msg.to_response)
|
2012-11-04 14:32:39 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-09-11 19:20:56 +00:00
|
|
|
@hook("cmd_hook", "ycc")
|
2012-11-04 14:32:39 +00:00
|
|
|
def cmd_ycc(msg):
|
2014-10-05 16:19:20 +00:00
|
|
|
minify = list()
|
|
|
|
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2012-11-04 14:32:39 +00:00
|
|
|
global LAST_URLS
|
|
|
|
if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0:
|
2014-10-05 16:19:20 +00:00
|
|
|
minify.append(LAST_URLS[msg.channel].pop())
|
2012-11-04 14:32:39 +00:00
|
|
|
else:
|
2014-07-25 14:38:01 +00:00
|
|
|
raise IRCException("je n'ai pas d'autre URL à réduire.")
|
2012-11-04 14:32:39 +00:00
|
|
|
|
2015-07-10 21:09:54 +00:00
|
|
|
if len(msg.args) > 4:
|
2014-07-25 14:38:01 +00:00
|
|
|
raise IRCException("je ne peux pas réduire autant d'URL d'un seul coup.")
|
2014-10-05 16:19:20 +00:00
|
|
|
else:
|
2015-07-10 21:09:54 +00:00
|
|
|
minify += msg.args
|
2014-07-25 14:38:01 +00:00
|
|
|
|
|
|
|
res = list()
|
2014-10-05 16:19:20 +00:00
|
|
|
for url in minify:
|
2014-07-25 14:38:01 +00:00
|
|
|
o = urlparse(url, "http")
|
|
|
|
if o.scheme != "":
|
2014-12-18 12:02:19 +00:00
|
|
|
minief_url = reduce(url)
|
2014-07-25 14:38:01 +00:00
|
|
|
if o.netloc == "":
|
2014-12-18 12:02:19 +00:00
|
|
|
res.append(gen_response(minief_url, msg, o.scheme))
|
2012-11-04 14:32:39 +00:00
|
|
|
else:
|
2014-12-18 12:02:19 +00:00
|
|
|
res.append(gen_response(minief_url, msg, o.netloc))
|
2014-07-25 14:38:01 +00:00
|
|
|
else:
|
2014-12-18 12:02:19 +00:00
|
|
|
res.append(gen_response(None, msg, url))
|
2014-07-25 14:38:01 +00:00
|
|
|
return res
|
2012-11-04 14:32:39 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-08-28 16:05:21 +00:00
|
|
|
@hook("msg_default")
|
2012-11-04 14:32:39 +00:00
|
|
|
def parselisten(msg):
|
2014-09-03 17:06:26 +00:00
|
|
|
parseresponse(msg)
|
|
|
|
return None
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2014-09-03 17:06:26 +00:00
|
|
|
@hook("all_post")
|
|
|
|
def parseresponse(msg):
|
2012-11-04 14:32:39 +00:00
|
|
|
global LAST_URLS
|
2014-05-01 21:36:21 +00:00
|
|
|
try:
|
2014-11-13 01:51:49 +00:00
|
|
|
urls = re.findall("([a-zA-Z0-9+.-]+:(?://)?[^ ]+)", msg.text)
|
|
|
|
for url in urls:
|
|
|
|
o = urlparse(url)
|
|
|
|
if o.scheme != "":
|
|
|
|
if o.netloc == "ycc.fr" or (o.netloc == "" and
|
|
|
|
len(o.path) < 10):
|
|
|
|
continue
|
|
|
|
if msg.channel not in LAST_URLS:
|
|
|
|
LAST_URLS[msg.channel] = list()
|
|
|
|
LAST_URLS[msg.channel].append(url)
|
2014-05-01 21:36:21 +00:00
|
|
|
except:
|
2014-07-25 14:38:01 +00:00
|
|
|
pass
|
2014-09-01 17:21:54 +00:00
|
|
|
return msg
|