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-07 15:37:19 +00:00
from urllib . request import urlopen
2012-11-04 14:32:39 +00:00
2014-08-12 18:10:19 +00:00
from hooks import hook
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
2012-11-04 14:32:39 +00:00
2014-08-12 18:10:19 +00:00
def help_full ( ) :
2014-07-25 14:38:01 +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 ( )
def gen_response ( res , msg , srv ) :
if res is None :
2014-07-25 14:38:01 +00:00
raise IRCException ( " la situation est embarassante, il semblerait que YCC soit down :( " )
2012-11-04 14:32:39 +00:00
elif isinstance ( res , str ) :
return Response ( msg . sender , " URL pour %s : %s " % ( srv , res ) , msg . channel )
else :
2014-07-25 14:38:01 +00:00
raise IRCException ( " mauvaise URL : %s " % srv )
2012-11-04 14:32:39 +00:00
2014-08-12 18:10:19 +00:00
@hook ( " cmd_hook " , " ycc " , help = " !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
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 :
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
2014-07-25 14:38:01 +00:00
if len ( msg . cmds ) > 5 :
raise IRCException ( " je ne peux pas réduire autant d ' URL d ' un seul coup. " )
res = list ( )
for url in msg . cmds [ 1 : ] :
o = urlparse ( url , " http " )
if o . scheme != " " :
snd_url = " http://ycc.fr/redirection/create/ " + quote ( url , " /: % @&=? " )
print_debug ( snd_url )
raw = urlopen ( snd_url , timeout = 10 )
if o . netloc == " " :
res . append ( gen_response ( raw . read ( ) . decode ( ) , msg , o . scheme ) )
2012-11-04 14:32:39 +00:00
else :
2014-07-25 14:38:01 +00:00
res . append ( gen_response ( raw . read ( ) . decode ( ) , msg , o . netloc ) )
else :
res . append ( gen_response ( False , msg , url ) )
return res
2012-11-04 14:32:39 +00:00
2014-08-28 16:05:21 +00:00
@hook ( " msg_default " )
2012-11-04 14:32:39 +00:00
def parselisten ( msg ) :
global LAST_URLS
2014-05-01 21:36:21 +00:00
try :
2014-08-29 14:33:45 +00:00
urls = re . findall ( " ([a-zA-Z0-9+.-]+:(?://)?[^ ]+) " , msg . text )
2014-05-01 21:36:21 +00:00
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 ( )
2014-07-25 14:38:01 +00:00
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
2012-11-06 03:14:11 +00:00
2014-08-12 18:10:19 +00:00
@hook ( " all_post " )
2012-11-04 14:32:39 +00:00
def parseresponse ( res ) :
2014-09-01 17:21:54 +00:00
return parselisten ( res )