From 3e8b9574888db796a3172322a49418f81bbc5b6c Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 1 Mar 2022 22:30:21 -0500 Subject: [PATCH] add polr shortener support --- polr.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 polr.py diff --git a/polr.py b/polr.py new file mode 100644 index 0000000..9fc3653 --- /dev/null +++ b/polr.py @@ -0,0 +1,51 @@ +from src import ModuleManager, utils + +URL = "https://{}/api/v2/action/shorten" + + +class Module(ModuleManager.BaseModule): + def on_load(self): + domain_setting = utils.Setting("polr-baseurl", "Base URL for polr installation") + self.exports.add("channelset", domain_setting) + self.exports.add("serverset", domain_setting) + self.exports.add("botset", domain_setting) + + apikey_setting = utils.SensitiveSetting("polr-apikey", "Set polr API key") + self.exports.add("channelset", apikey_setting) + self.exports.add("serverset", apikey_setting) + self.exports.add("botset", apikey_setting) + + self.exports.add("shorturl-x-polr", self._shorturl) + + def _shorturl(self, server, context, url): + if len(url) < 18: + return None + + if context: + baseurl = context.get_setting( + "polr-baseurl", + server.get_setting( + "polr-baseurl", self.bot.get_setting("polr-baseurl") + ), + ) + + apikey = context.get_setting( + "polr-apikey", + server.get_setting("polr-apikey", self.bot.get_setting("polr-apikey")), + ) + + if baseurl and apikey: + page = utils.http.request( + URL.format(baseurl), + method="POST", + post_data={ + "url": url, + "key": apikey, + "response_type": "plain_text", + }, + ) + + if page and page.data: + return page.decode("utf8") + + return None