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: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 35
      pbincli/api.py

35
pbincli/api.py

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

Loading…
Cancel
Save