implement custom shorter url support (#28)

Usage: set "--short-api" to "custom", provide url with "--short-url" to shorter
service which returns short link in text form. For paste url mask "{{url}}" must
be used. Link queried with GET request.

Example for Shlink service:
https://doma.in/rest/v2/short-urls/shorten?apiKey=YOURKEY&longUrl={{url}}&format=txt

Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
R4SAS
2022-01-10 02:00:25 +03:00
parent 5589ba0437
commit 82ca95f01a
3 changed files with 19 additions and 2 deletions

View File

@@ -95,6 +95,8 @@ class Shortener:
self._yourls_init(settings)
elif self.api == 'isgd' or self.api == 'vgd':
self._gd_init()
elif self.api == 'custom':
self.apiurl == settings['short_url']
self.session, self.proxy = _config_requests(settings)
@@ -142,6 +144,7 @@ class Shortener:
'isgd': self._gd,
'vgd': self._gd,
'cuttly': self._cuttly
'custom': self._custom
}
# run function selected by choosen API
servicesList[self.api](url)
@@ -250,3 +253,17 @@ class Shortener:
print("Short Link:\t{}".format(result.text))
except Exception as ex:
PBinCLIError("cutt.ly: unexcepted behavior: {}".format(ex))
def _custom(self, url):
from urllib.parse import quote
qUrl = quote(url, safe="") # urlencoded paste url
rUrl = self.apiurl.replace("{{url}}", qUrl)
try:
result = self.session.get(
url = rUrl,
proxies = self.proxy)
print("Short Link:\t{}".format(result.text))
except Exception as ex:
PBinCLIError("Shorter: unexcepted behavior: {}".format(ex))