2020-03-08 23:58:24 +00:00
|
|
|
import urllib.parse
|
2020-03-08 23:34:54 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
def _parse(s):
|
2020-03-08 23:58:24 +00:00
|
|
|
parsed = urllib.parse.urlparse(s)
|
2020-03-08 23:34:54 +00:00
|
|
|
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):
|
|
|
|
setting = server.get_setting("yourls",
|
|
|
|
self.bot.get_setting("yourls", None))
|
|
|
|
if context:
|
|
|
|
setting = context.get_setting("yourls", setting)
|
|
|
|
|
|
|
|
if not setting == None:
|
2020-03-09 00:02:45 +00:00
|
|
|
shortener_url, token = setting
|
2020-03-08 23:34:54 +00:00
|
|
|
|
2020-03-09 00:04:08 +00:00
|
|
|
page = utils.http.request(shortener_url, get_params={
|
2020-03-08 23:34:54 +00:00
|
|
|
"signature": token,
|
|
|
|
"action": "shorturl",
|
|
|
|
"url": url,
|
|
|
|
"format": "json"}).json()
|
|
|
|
if page:
|
|
|
|
return page["shorturl"]
|
|
|
|
return None
|