Browse Source

[shortener] separate services related code in functions

Signed-off-by: r4sas <r4sas@i2pmail.org>
dependabot/add-v2-config-file
R4SAS 5 years ago
parent
commit
ebfe0c48a0
Signed by untrusted user: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 35
      pbincli/api.py

35
pbincli/api.py

@ -111,8 +111,22 @@ class Shortener: @@ -111,8 +111,22 @@ class Shortener:
self.session, self.proxy = _config_requests(settings)
def getlink(self, url):
if self.api == 'yourls':
# that is api -> function mapper for running service-related function when getlink() used
servicesList = {
'yourls': self._yourls,
'clckru': self._clckru,
'tinyurl': self._tinyurl,
'isgd': self._gd,
'vgd': self._gd,
'cuttly': self._cuttly
}
# run function selected by choosen API
servicesList[self.api](url)
def _yourls(self,url):
request = {'action': 'shorturl', 'format': 'json', 'url': url}
request.update(self.auth_args)
@ -143,7 +157,8 @@ class Shortener: @@ -143,7 +157,8 @@ class Shortener:
else:
PBinCLIError("YOURLS: No status, statusCode or message fields in response! Received:\n{}".format(response))
elif self.api == 'clckru':
def _clckru(self, url):
request = {'url': url}
try:
@ -155,7 +170,8 @@ class Shortener: @@ -155,7 +170,8 @@ class Shortener:
except Exception as ex:
PBinCLIError("clck.ru: unexcepted behavior: {}".format(ex))
elif self.api == 'tinyurl':
def _tinyurl(self, url):
request = {'url': url}
try:
@ -167,7 +183,8 @@ class Shortener: @@ -167,7 +183,8 @@ class Shortener:
except Exception as ex:
PBinCLIError("TinyURL: unexcepted behavior: {}".format(ex))
elif self.api == 'isgd' or self.api == 'vgd':
def _gd(self, url):
request = {
'format': 'json',
'url': url,
@ -197,7 +214,8 @@ class Shortener: @@ -197,7 +214,8 @@ class Shortener:
"is.gd" if self.api == 'isgd' else 'v.gd',
ex))
elif self.api == 'cuttly':
def _cuttly(self, url):
request = {
'url': url,
'domain': 0
@ -212,9 +230,9 @@ class Shortener: @@ -212,9 +230,9 @@ class Shortener:
except Exception as ex:
PBinCLIError("cutt.ly: unexcepted behavior: {}".format(ex))
'''
# That code needs testing. API requires username and apiKey or accessToken to work.
elif self.api == 'bitly':
# [WIP] That code needs testing. API requires username and apiKey or accessToken to work.
def _bitly(self, url):
request = {'url': url}
headers = {'X-Requested-With': 'XMLHttpRequest'}
@ -240,4 +258,3 @@ class Shortener: @@ -240,4 +258,3 @@ class Shortener:
PBinCLIError("bitly: got error from API: {}".format(friendlyError[errcode]))
except Exception as ex:
PBinCLIError("bitly: unexcepted behavior: {}".format(ex))
'''

Loading…
Cancel
Save