2019-06-25 20:31:30 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
|
2016-03-29 15:20:50 +00:00
|
|
|
import re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 15:20:50 +00:00
|
|
|
|
2019-06-25 20:30:40 +00:00
|
|
|
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
|
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
@utils.export("serverset", {"setting": "url-shortener",
|
|
|
|
"help": "Set URL shortener service", "example": "bitly"})
|
|
|
|
@utils.export("botset", {"setting": "url-shortener",
|
|
|
|
"help": "Set URL shortener service", "example": "bitly"})
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-02-07 10:07:11 +00:00
|
|
|
def on_load(self):
|
2019-06-25 16:53:00 +00:00
|
|
|
self.exports.add("shorturl", self._shorturl)
|
|
|
|
self.exports.add("shorturl-any", self._shorturl_any)
|
|
|
|
|
2019-06-25 20:30:40 +00:00
|
|
|
self.exports.add("shorturl-s-bitly", self._bitly)
|
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
def _get_shortener(self, name):
|
|
|
|
return self.exports.get_one("shorturl-s-%s" % name, None)
|
|
|
|
def _call_shortener(self, shortener_name, url):
|
|
|
|
shortener = self._get_shortener(shortener_name)
|
|
|
|
if shortener == None:
|
|
|
|
return None
|
|
|
|
short_url = shortener(url)
|
|
|
|
if short_url == None:
|
|
|
|
return None
|
|
|
|
return short_url
|
|
|
|
|
|
|
|
def _shorturl_any(self, url):
|
2019-06-25 20:30:40 +00:00
|
|
|
return self._call_shortener("bitly", url) or url
|
2019-06-25 16:53:00 +00:00
|
|
|
|
|
|
|
def _shorturl(self, server, url):
|
|
|
|
shortener_name = server.get_setting("url-shortener", "bitly")
|
|
|
|
if shortener_name == None:
|
|
|
|
return url
|
|
|
|
return self._call_shortener(shortener_name, url) or url
|
2016-03-29 15:20:50 +00:00
|
|
|
|
2019-06-25 20:30:40 +00:00
|
|
|
def _bitly(self, url):
|
|
|
|
if len(url) < 22:
|
|
|
|
return None
|
|
|
|
|
|
|
|
access_token = self.bot.config.get("bitly-api-key", None)
|
|
|
|
if access_token:
|
|
|
|
page = utils.http.request(URL_BITLYSHORTEN, get_params={
|
|
|
|
"access_token": access_token, "longUrl": url}, json=True)
|
|
|
|
|
|
|
|
if page and page.data["data"]:
|
|
|
|
return page.data["data"]["url"]
|
|
|
|
return None
|
|
|
|
|
2019-04-24 14:36:26 +00:00
|
|
|
@utils.hook("received.command.shorten")
|
2016-03-29 15:20:50 +00:00
|
|
|
def shorten(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-06-25 16:53:00 +00:00
|
|
|
:help: Shorten a given URL
|
2018-09-30 16:29:09 +00:00
|
|
|
:usage: <url>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-04-24 14:36:26 +00:00
|
|
|
url = None
|
|
|
|
if len(event["args"]) > 0:
|
|
|
|
url = event["args_split"][0]
|
2019-06-25 16:53:00 +00:00
|
|
|
if not re.match(utils.http.REGEX_URL, url):
|
|
|
|
url = "http://%s" % url
|
2019-04-24 14:36:26 +00:00
|
|
|
else:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = event["target"].buffer.find(utils.http.REGEX_URL)
|
2019-04-24 14:36:26 +00:00
|
|
|
if url:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = re.search(utils.http.REGEX_URL, url.message).group(0)
|
2019-04-24 14:36:26 +00:00
|
|
|
if not url:
|
|
|
|
raise utils.EventError("No URL provided/found.")
|
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
event["stdout"].write("Shortened URL: %s" % self._shorturl(
|
|
|
|
event["server"], url))
|
2019-06-26 17:09:39 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.unshorten", min_args=1)
|
|
|
|
def unshorten(self, event):
|
|
|
|
url = event["args_split"][0]
|
|
|
|
if not re.match(utils.http.REGEX_URL, url):
|
|
|
|
url = "http://%s" % url
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = utils.http.request(url, method="HEAD",
|
|
|
|
allow_redirects=False)
|
|
|
|
except:
|
|
|
|
response = None
|
|
|
|
|
|
|
|
if response and "location" in response.headers:
|
|
|
|
event["stdout"].write("Unshortened: %s" %
|
|
|
|
response.headers["location"])
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to unshorten URL")
|