From ed57ac7c17ace0906970ddbb0035df7f0f8fb356 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 1 May 2019 08:17:48 +0100 Subject: Move isgd.py to shorturl.py and switch back to using bit.ly --- modules/isgd.py | 39 --------------------------------------- modules/shorturl.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 39 deletions(-) delete mode 100644 modules/isgd.py create mode 100644 modules/shorturl.py (limited to 'modules') diff --git a/modules/isgd.py b/modules/isgd.py deleted file mode 100644 index cfc94ebb..00000000 --- a/modules/isgd.py +++ /dev/null @@ -1,39 +0,0 @@ -import re -from src import ModuleManager, utils - -ISGD_API_URL = "https://is.gd/create.php" - -class Module(ModuleManager.BaseModule): - def on_load(self): - self.exports.add("shortlink", self._shortlink) - - def _shortlink(self, url): - if not re.match(utils.http.REGEX_URL, url): - url = "http://%s" % url - - page = utils.http.request(ISGD_API_URL, get_params= - {"format": "json", "url": url}, json=True) - - if page and page.data["shorturl"]: - return page.data["shorturl"] - - @utils.hook("received.command.shorten") - def shorten(self, event): - """ - :help: Shorten a given URL using the is.gd service - :usage: - """ - url = None - if len(event["args"]) > 0: - url = event["args_split"][0] - else: - url = event["target"].buffer.find(utils.http.REGEX_URL) - if url: - url = re.search(utils.http.REGEX_URL, url.message).group(0) - if not url: - raise utils.EventError("No URL provided/found.") - - if url: - event["stdout"].write("Shortened URL: %s" % self._shortlink(url)) - else: - event["stderr"].write("Unable to shorten that URL.") diff --git a/modules/shorturl.py b/modules/shorturl.py new file mode 100644 index 00000000..13503725 --- /dev/null +++ b/modules/shorturl.py @@ -0,0 +1,44 @@ +#--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): + _name = "Short" + + def on_load(self): + self.exports.add("shortlink", self._shortlink) + + def _shortlink(self, url): + if not re.match(utils.http.REGEX_URL, url): + url = "http://%s" % url + + 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"] + + @utils.hook("received.command.shorten") + def shorten(self, event): + """ + :help: Shorten a given URL using the is.gd service + :usage: + """ + url = None + if len(event["args"]) > 0: + url = event["args_split"][0] + else: + url = event["target"].buffer.find(utils.http.REGEX_URL) + if url: + url = re.search(utils.http.REGEX_URL, url.message).group(0) + if not url: + raise utils.EventError("No URL provided/found.") + + if url: + event["stdout"].write("Shortened URL: %s" % self._shortlink(url)) + else: + event["stderr"].write("Unable to shorten that URL.") -- cgit v1.3.1-10-gc9f91