From 9d82c727b6ea3d07def66232606aaddf02b38ec1 Mon Sep 17 00:00:00 2001 From: r4sas Date: Fri, 20 Sep 2019 06:12:10 +0000 Subject: [PATCH] [shortener] move service-depend init code to functions Signed-off-by: r4sas --- pbincli/api.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/pbincli/api.py b/pbincli/api.py index fc1da15..3b4bf41 100644 --- a/pbincli/api.py +++ b/pbincli/api.py @@ -88,28 +88,34 @@ class Shortener: # we checking which service is used, because some services doesn't require # any authentication, or have only one domain on which it working if self.api == 'yourls': - if not settings['short_url']: - PBinCLIError("YOURLS: An API URL is required") - self.apiurl = settings['short_url'] - - if settings['short_user'] and settings['short_pass'] and settings['short_token'] is None: - self.auth_args = {'username': settings['short_user'], 'password': settings['short_pass']} - elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token']: - self.auth_args = {'signature': settings['short_token']} - elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token'] is None: - self.auth_args = {} - else: - PBinCLIError("YOURLS: either username and password or token are required. Otherwise set to default (None)") + self._yourls_init(settings) elif self.api == 'isgd' or self.api == 'vgd': - if self.api == 'isgd': - self.apiurl = 'https://is.gd/' - else: - self.apiurl = 'https://v.gd/' + self._gd_init() - self.useragent = 'Mozilla/5.0 (compatible; pbincli - https://github.com/r4sas/pbincli/)' + self.session, self.proxy = _config_requests(settings) - self.session, self.proxy = _config_requests(settings) + def _yourls_init(self, settings): + if not settings['short_url']: + PBinCLIError("YOURLS: An API URL is required") + self.apiurl = settings['short_url'] + + if settings['short_user'] and settings['short_pass'] and settings['short_token'] is None: + self.auth_args = {'username': settings['short_user'], 'password': settings['short_pass']} + elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token']: + self.auth_args = {'signature': settings['short_token']} + elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token'] is None: + self.auth_args = {} + else: + PBinCLIError("YOURLS: either username and password or token are required. Otherwise set to default (None)") + + + def _gd_init(self): + if self.api == 'isgd': + self.apiurl = 'https://is.gd/' + else: + self.apiurl = 'https://v.gd/' + self.useragent = 'Mozilla/5.0 (compatible; pbincli - https://github.com/r4sas/pbincli/)' def getlink(self, url):