From 843d9fb77f10ae0d0a6d7368479379656d2f39b2 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 8 Mar 2020 23:34:54 +0000 Subject: [PATCH] add (as yet untested) Your Own URL Shortener support in yourls.py --- modules/yourls.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 modules/yourls.py diff --git a/modules/yourls.py b/modules/yourls.py new file mode 100644 index 00000000..9543c4e2 --- /dev/null +++ b/modules/yourls.py @@ -0,0 +1,36 @@ +from src import ModuleManager, utils + +def _parse(s): + parsed = urllib.parse.urlpare(s) + return urllib.parse.urljoin(s, parsed.path), parsed.query + +SETTING = utils.FunctionSetting(_parse, "yourls", + "Set YOURLS server (and token) to use for URL shortening", + example="https://bitbot.dev/yourls-api.php?1002a612b4", + format=utils.sensitive_format) + +@utils.export("botset", SETTING) +@utils.export("serverset", SETTING) +@utils.export("channelset", SETTING) +class Module(ModuleManager.BaseModule): + @utils.export("shorturl-x-yourls") + def _shorturl(self, server, context, url): + if len(url) < 20: + return None + + setting = server.get_setting("yourls", + self.bot.get_setting("yourls", None)) + if context: + setting = context.get_setting("yourls", setting) + + if not setting == None: + url, token = setting + + page = utils.http.request(URL, post_data={ + "signature": token, + "action": "shorturl", + "url": url, + "format": "json"}).json() + if page: + return page["shorturl"] + return None