[urlshortner] Add framalink support

This commit is contained in:
Max 2015-10-09 01:47:42 +01:00
parent c55e66dd70
commit ece42ffe47

View File

@ -3,6 +3,7 @@
# PYTHON STUFFS ####################################################### # PYTHON STUFFS #######################################################
import re import re
import json
from urllib.parse import urlparse from urllib.parse import urlparse
from urllib.parse import quote from urllib.parse import quote
@ -12,13 +13,26 @@ from nemubot.message import Text
from nemubot.tools import web from nemubot.tools import web
# MODULE FUCNTIONS ####################################################
def default_reducer(url, data):
snd_url = url + quote(data, "/:%@&=?")
return web.getURLContent(snd_url)
def framalink_reducer(url, data):
json_data = json.loads(web.getURLContent(url, "lsturl="
+ quote(data, "/:%@&=?"),
header={"Content-Type": "application/x-www-form-urlencoded"}))
return json_data['short']
# MODULE VARIABLES #################################################### # MODULE VARIABLES ####################################################
PROVIDERS = { PROVIDERS = {
"tinyurl": "http://tinyurl.com/api-create.php?url=", "tinyurl": (default_reducer, "http://tinyurl.com/api-create.php?url="),
"ycc": "http://ycc.fr/redirection/create/", "ycc": (default_reducer, "http://ycc.fr/redirection/create/"),
"framalink": (framalink_reducer, "https://frama.link/a?format=json")
} }
DEFAULT_PROVIDER = "ycc" DEFAULT_PROVIDER = "framalink"
# LOADING ############################################################# # LOADING #############################################################
@ -35,15 +49,12 @@ def load(context):
# MODULE CORE ######################################################### # MODULE CORE #########################################################
def reduce(url): def reduce(url):
"""Ask YCC website to reduce given URL """Ask the url shortner website to reduce given URL
Argument: Argument:
url -- the URL to reduce url -- the URL to reduce
""" """
return PROVIDERS[DEFAULT_PROVIDER][0](PROVIDERS[DEFAULT_PROVIDER][1], url)
snd_url = PROVIDERS[DEFAULT_PROVIDER] + quote(url, "/:%@&=?")
return web.getURLContent(snd_url)
def gen_response(res, msg, srv): def gen_response(res, msg, srv):
if res is None: if res is None:
@ -72,7 +83,7 @@ def parseresponse(msg):
for url in urls: for url in urls:
o = urlparse(url) o = urlparse(url)
if o.scheme != "": if o.scheme != "":
if o.netloc == "ycc.fr" or o.netloc == "tinyurl.com" or ( if o.netloc == "ycc.fr" or o.netloc == "tinyurl.com" or o.netloc == "frama.link" or (
o.netloc == "" and len(o.path) < 10): o.netloc == "" and len(o.path) < 10):
continue continue
for recv in msg.receivers: for recv in msg.receivers:
@ -86,7 +97,7 @@ def parseresponse(msg):
# MODULE INTERFACE #################################################### # MODULE INTERFACE ####################################################
@hook("cmd_hook", "tinyurl", @hook("cmd_hook", "framalink",
help="Reduce any given URL", help="Reduce any given URL",
help_usage={None: "Reduce the last URL said on the channel", help_usage={None: "Reduce the last URL said on the channel",
"URL [URL ...]": "Reduce the given URL(s)"}) "URL [URL ...]": "Reduce the given URL(s)"})