2019-06-25 20:31:30 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
|
2020-03-08 23:58:24 +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
|
|
|
|
2023-01-17 19:46:52 +00:00
|
|
|
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v4/shorten"
|
2019-06-25 20:30:40 +00:00
|
|
|
|
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-08-30 13:40:54 +00:00
|
|
|
setting = utils.OptionsSetting([], "url-shortener",
|
2019-06-29 21:26:28 +00:00
|
|
|
"Set URL shortener service",
|
|
|
|
options_factory=self._shorturl_options_factory)
|
2019-08-05 09:48:16 +00:00
|
|
|
self.exports.add("channelset", setting)
|
2019-06-29 21:26:28 +00:00
|
|
|
self.exports.add("serverset", setting)
|
|
|
|
self.exports.add("botset", setting)
|
|
|
|
|
|
|
|
def _shorturl_options_factory(self):
|
2020-03-08 23:34:22 +00:00
|
|
|
shorteners = set(self.exports.find("shorturl-s-"))
|
|
|
|
shorteners.update(self.exports.find("shorturl-x-"))
|
|
|
|
return sorted(s.split("-", 2)[-1] for s in shorteners)
|
2019-06-29 21:26:28 +00:00
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
def _get_shortener(self, name):
|
2020-03-08 23:34:22 +00:00
|
|
|
extended = self.exports.get("shorturl-x-%s" % name, None)
|
|
|
|
if not extended == None:
|
|
|
|
return True, extended
|
|
|
|
return False, self.exports.get("shorturl-s-%s" % name, None)
|
2020-03-08 23:59:31 +00:00
|
|
|
def _call_shortener(self, server, context, shortener_name, url):
|
2020-03-08 23:34:22 +00:00
|
|
|
extended, shortener = self._get_shortener(shortener_name)
|
2019-06-25 16:53:00 +00:00
|
|
|
if shortener == None:
|
|
|
|
return None
|
2020-03-08 23:34:22 +00:00
|
|
|
|
|
|
|
if extended:
|
|
|
|
short_url = shortener(server, context, url)
|
|
|
|
else:
|
|
|
|
short_url = shortener(url)
|
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
if short_url == None:
|
|
|
|
return None
|
|
|
|
return short_url
|
|
|
|
|
2020-02-19 17:29:10 +00:00
|
|
|
@utils.export("shorturl-any")
|
2019-06-25 16:53:00 +00:00
|
|
|
def _shorturl_any(self, url):
|
2020-10-17 16:46:37 +00:00
|
|
|
return self._call_shortener(None, None, "bitly", url) or url
|
2019-06-25 16:53:00 +00:00
|
|
|
|
2020-02-19 17:29:10 +00:00
|
|
|
@utils.export("shorturl")
|
2019-08-05 09:48:16 +00:00
|
|
|
def _shorturl(self, server, url, context=None):
|
|
|
|
shortener_name = None
|
|
|
|
if context:
|
|
|
|
shortener_name = context.get_setting("url-shortener",
|
2020-07-14 06:48:33 +00:00
|
|
|
server.get_setting("url-shortener",
|
|
|
|
self.bot.get_setting("url-shortener", "bitly")))
|
2019-08-05 09:48:16 +00:00
|
|
|
else:
|
2020-07-14 06:48:33 +00:00
|
|
|
shortener_name = server.get_setting("url-shortener",
|
|
|
|
self.bot.get_setting("url-shortener", "bitly"))
|
2019-08-05 09:48:16 +00:00
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
if shortener_name == None:
|
|
|
|
return url
|
2020-03-08 23:34:22 +00:00
|
|
|
return self._call_shortener(
|
|
|
|
server, context, shortener_name, url) or url
|
2016-03-29 15:20:50 +00:00
|
|
|
|
2020-02-19 17:29:10 +00:00
|
|
|
@utils.export("shorturl-s-bitly")
|
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:
|
2023-01-17 19:46:52 +00:00
|
|
|
resp = utils.http.request(
|
|
|
|
URL_BITLYSHORTEN,
|
|
|
|
method="POST",
|
|
|
|
post_data={"long_url": url},
|
|
|
|
json_body=True,
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"}
|
|
|
|
)
|
|
|
|
|
|
|
|
if resp.code == 200:
|
|
|
|
return resp.json()["link"]
|
2019-06-25 20:30:40 +00:00
|
|
|
return None
|
|
|
|
|
2019-07-23 17:07:47 +00:00
|
|
|
def _find_url(self, target, args):
|
2019-04-24 14:36:26 +00:00
|
|
|
url = None
|
2019-07-23 17:07:47 +00:00
|
|
|
if args:
|
|
|
|
url = args[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-07-23 17:07:47 +00:00
|
|
|
url = target.buffer.find(utils.http.REGEX_URL)
|
2019-04-24 14:36:26 +00:00
|
|
|
if url:
|
2019-08-13 12:48:03 +00:00
|
|
|
url = utils.http.url_sanitise(url.match)
|
2019-04-24 14:36:26 +00:00
|
|
|
if not url:
|
|
|
|
raise utils.EventError("No URL provided/found.")
|
2019-07-23 17:07:47 +00:00
|
|
|
return url
|
|
|
|
|
|
|
|
@utils.hook("received.command.shorten")
|
|
|
|
def shorten(self, event):
|
|
|
|
"""
|
|
|
|
:help: Shorten a given URL
|
|
|
|
:usage: <url>
|
|
|
|
"""
|
|
|
|
url = self._find_url(event["target"], event["args_split"])
|
2019-04-24 14:36:26 +00:00
|
|
|
|
2019-06-25 16:53:00 +00:00
|
|
|
event["stdout"].write("Shortened URL: %s" % self._shorturl(
|
2019-08-05 09:48:16 +00:00
|
|
|
event["server"], url, context=event["target"]))
|
2019-06-26 17:09:39 +00:00
|
|
|
|
2019-07-23 17:07:47 +00:00
|
|
|
@utils.hook("received.command.unshorten")
|
2019-06-26 17:09:39 +00:00
|
|
|
def unshorten(self, event):
|
2019-07-23 17:07:47 +00:00
|
|
|
url = self._find_url(event["target"], event["args_split"])
|
2019-06-26 17:09:39 +00:00
|
|
|
|
|
|
|
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:
|
2023-01-17 19:46:52 +00:00
|
|
|
event["stderr"].write("Failed to unshorten URL")
|