add polr shortener support

This commit is contained in:
Ben Harris 2022-03-01 22:30:21 -05:00
parent eaec4ace8e
commit 3e8b957488
1 changed files with 51 additions and 0 deletions

51
polr.py Normal file
View File

@ -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