From 45d854e5904da91163a749fd5e77c49a9a869a6b Mon Sep 17 00:00:00 2001 From: r4sas Date: Wed, 18 Sep 2019 21:09:16 +0000 Subject: [PATCH] [shortener] add is.gd, v.gd and cutt.ly services support Start realisation of bitly support. Signed-off-by: r4sas --- pbincli/api.py | 111 ++++++++++++++++++++++++++++++++++++++++++++----- pbincli/cli.py | 2 +- 2 files changed, 102 insertions(+), 11 deletions(-) diff --git a/pbincli/api.py b/pbincli/api.py index 9bdf936..619ade3 100644 --- a/pbincli/api.py +++ b/pbincli/api.py @@ -100,6 +100,14 @@ class Shortener: self.auth_args = {} else: PBinCLIError("YOURLS: either username and password or token are required. Otherwise set to default (None)") + elif self.api == 'isgd' or self.api == 'vgd': + 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/)' + self.session, self.proxy = _config_requests(settings) @@ -137,16 +145,99 @@ class Shortener: elif self.api == 'clckru': request = {'url': url} - result = self.session.post( - url = "https://clck.ru/--", - proxies = self.proxy, - data = request) - print("Short Link:\t{}".format(result.text)) + + try: + result = self.session.post( + url = "https://clck.ru/--", + proxies = self.proxy, + data = request) + print("Short Link:\t{}".format(result.text)) + except Exception as ex: + PBinCLIError("clck.ru: unexcepted behavior: {}".format(ex)) elif self.api == 'tinyurl': request = {'url': url} - result = self.session.post( - url = "https://tinyurl.com/api-create.php", - proxies = self.proxy, - data = request) - print("Short Link:\t{}".format(result.text)) + + try: + result = self.session.post( + url = "https://tinyurl.com/api-create.php", + proxies = self.proxy, + data = request) + print("Short Link:\t{}".format(result.text)) + except Exception as ex: + PBinCLIError("TinyURL: unexcepted behavior: {}".format(ex)) + + elif self.api == 'isgd' or self.api == 'vgd': + request = { + 'format': 'json', + 'url': url, + 'logstats': 0 # we don't want use any statistics + } + headers = { 'User-Agent': self.useragent} + + try: + result = self.session.post( + url = self.apiurl + "create.php", + headers = headers, + proxies = self.proxy, + data = request) + + response = result.json() + + if 'shorturl' in response: + print("Short Link:\t{}".format(response['shorturl'])) + else: + PBinCLIError("{}: got error {} from API: {}".format( + "is.gd" if self.api == 'isgd' else 'v.gd', + response['errorcode'], + response['errormessage'])) + + except Exception as ex: + PBinCLIError("{}: unexcepted behavior: {}".format( + "is.gd" if self.api == 'isgd' else 'v.gd', + ex)) + + elif self.api == 'cuttly': + request = { + 'url': url, + 'domain': 0 + } + + try: + result = self.session.post( + url = "https://cutt.ly/scripts/shortenUrl.php", + proxies = self.proxy, + data = request) + print("Short Link:\t{}".format(result.text)) + 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': + request = {'url': url} + headers = {'X-Requested-With': 'XMLHttpRequest'} + + try: + result = self.session.post( + url = "https://bitly.com/", + headers = headers, + proxies = self.proxy, + data = request) + response = result.json() + if response['data'] and response['status_code'] == 200: + print("Short Link:\t{}".format(response['data']['anon_shorten']['link'])) + elif response['status_txt']: + errcode = response['status_txt'] or 'DEFAULT' + friendlyError = { + 'RATE_LIMIT_EXCEEDED': 'Whoa - you\'ve exceeded your quota. Create a free account to keep shortening.', + 'INVALID_ARG_URL': 'Unable to shorten that link. It is not a valid url.', + 'INVALID_ARG_LONGURL': 'Unable to shorten that link. It is not a valid url.', + 'ALREADY_A_BITLY_LINK': 'That is already a Bitly link', + 'UNKNOWN_ERROR': 'Woops. Something went wrong. Please try again.', + 'DEFAULT': 'An error occurred' + } + PBinCLIError("bitly: got error from API: {}".format(friendlyError[errcode])) + except Exception as ex: + PBinCLIError("bitly: unexcepted behavior: {}".format(ex)) + ''' diff --git a/pbincli/cli.py b/pbincli/cli.py index ff8739e..7b7057b 100755 --- a/pbincli/cli.py +++ b/pbincli/cli.py @@ -38,7 +38,7 @@ def main(): choices=["zlib", "none"], help="set compression for paste (default: zlib). Note: works only on v2 paste format") # URL shortener send_parser.add_argument("-S", "--short", default=False, action="store_true", help="use URL shortener") - send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store", choices=["clckru", "tinyurl", "yourls"], help="API used by shortener service") + send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store", choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls"], help="API used by shortener service") send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API") send_parser.add_argument("--short-user", default=argparse.SUPPRESS, help="Shortener username") send_parser.add_argument("--short-pass", default=argparse.SUPPRESS, help="Shortener password")