From 94dc2655e7b81f7f8686eab1739f99210f142946 Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 25 Jun 2019 21:30:40 +0100 Subject: [PATCH] merge bitly code back in to shorturl.py as we use it for default shortener --- modules/bitly.py | 22 ---------------------- modules/shorturl.py | 26 ++++++++++++++++++-------- 2 files changed, 18 insertions(+), 30 deletions(-) delete mode 100644 modules/bitly.py diff --git a/modules/bitly.py b/modules/bitly.py deleted file mode 100644 index 52703fe9..00000000 --- a/modules/bitly.py +++ /dev/null @@ -1,22 +0,0 @@ -#--depends-on commands -#--require-config bitly-api-key - -import re -from src import ModuleManager, utils - -URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten" - -class Module(ModuleManager.BaseModule): - def on_load(self): - self.exports.add("shorturl-s-bitly", self._shorturl) - def _shorturl(self, url): - if len(url) < 22: - return None - - page = utils.http.request(URL_BITLYSHORTEN, get_params={ - "access_token": self.bot.config["bitly-api-key"], - "longUrl": url}, json=True) - - if page and page.data["data"]: - return page.data["data"]["url"] - return None diff --git a/modules/shorturl.py b/modules/shorturl.py index 6cb7073b..fa8bbecd 100644 --- a/modules/shorturl.py +++ b/modules/shorturl.py @@ -1,6 +1,8 @@ import re from src import ModuleManager, utils +URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten" + @utils.export("serverset", {"setting": "url-shortener", "help": "Set URL shortener service", "example": "bitly"}) @utils.export("botset", {"setting": "url-shortener", @@ -10,6 +12,8 @@ class Module(ModuleManager.BaseModule): self.exports.add("shorturl", self._shorturl) self.exports.add("shorturl-any", self._shorturl_any) + self.exports.add("shorturl-s-bitly", self._bitly) + def _get_shortener(self, name): return self.exports.get_one("shorturl-s-%s" % name, None) def _call_shortener(self, shortener_name, url): @@ -22,14 +26,7 @@ class Module(ModuleManager.BaseModule): return short_url def _shorturl_any(self, url): - global_shortener_name = self.bot.get_setting("url-shortener", "bitly") - if global_shortener_name: - return self._call_shortener(global_shortener_name, url) or url - - shortener_name = self.exports.find_one("shorturl-s-", None) - if shortener_name == None: - return url - return self._call_shortener(shortener_name, url) or url + return self._call_shortener("bitly", url) or url def _shorturl(self, server, url): shortener_name = server.get_setting("url-shortener", "bitly") @@ -37,6 +34,19 @@ class Module(ModuleManager.BaseModule): return url return self._call_shortener(shortener_name, url) or url + 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 + @utils.hook("received.command.shorten") def shorten(self, event): """