52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
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
|