nemubot/modules/ycc.py

98 lines
2.5 KiB
Python
Raw Normal View History

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
from urllib.parse import urlparse
from urllib.parse import quote
2012-11-04 14:32:39 +00:00
from nemubot.exception import IRCException
from nemubot.hooks import hook
from nemubot.message import TextMessage
from nemubot.tools import web
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
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, "/:%@&=?")
print_debug(snd_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:
2014-11-13 01:51:49 +00:00
return TextMessage("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
@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()
2012-11-04 14:32:39 +00:00
if len(msg.cmds) == 1:
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:
raise IRCException("je n'ai pas d'autre URL à réduire.")
2012-11-04 14:32:39 +00:00
if len(msg.cmds) > 5:
raise IRCException("je ne peux pas réduire autant d'URL d'un seul coup.")
2014-10-05 16:19:20 +00:00
else:
minify += msg.cmds[1:]
res = list()
2014-10-05 16:19:20 +00:00
for url in minify:
o = urlparse(url, "http")
if o.scheme != "":
2014-12-18 12:02:19 +00:00
minief_url = reduce(url)
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))
else:
2014-12-18 12:02:19 +00:00
res.append(gen_response(None, msg, url))
return res
2012-11-04 14:32:39 +00:00
2014-11-13 01:51:49 +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
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)
except:
pass
return msg