mirror of
https://github.com/r4sas/PBinCLI
synced 2025-09-01 08:32:16 +00:00
[shortener] separate services related code in functions
Signed-off-by: r4sas <r4sas@i2pmail.org>
This commit is contained in:
parent
45d854e590
commit
ebfe0c48a0
255
pbincli/api.py
255
pbincli/api.py
@ -111,133 +111,150 @@ class Shortener:
|
|||||||
|
|
||||||
self.session, self.proxy = _config_requests(settings)
|
self.session, self.proxy = _config_requests(settings)
|
||||||
|
|
||||||
def getlink(self, url):
|
|
||||||
if self.api == 'yourls':
|
|
||||||
request = {'action': 'shorturl', 'format': 'json', 'url': url}
|
|
||||||
request.update(self.auth_args)
|
|
||||||
|
|
||||||
|
def getlink(self, url):
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
result = self.session.post(
|
||||||
|
url = self.apiurl,
|
||||||
|
proxies = self.proxy,
|
||||||
|
data = request)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result.raise_for_status()
|
||||||
|
except HTTPError:
|
||||||
|
try:
|
||||||
|
response = result.json()
|
||||||
|
except ValueError:
|
||||||
|
PBinCLIError("YOURLS: Unable parse response. Received (size = {}):\n{}".format(len(result.text), result.text))
|
||||||
|
else:
|
||||||
|
PBinCLIError("YOURLS: Received error from API: {} with JSON {}".format(result, response))
|
||||||
|
else:
|
||||||
|
response = result.json()
|
||||||
|
|
||||||
|
if {'status', 'statusCode', 'message'} <= set(response.keys()):
|
||||||
|
if response['status'] == 'fail':
|
||||||
|
PBinCLIError("YOURLS: Received error from API: {}".format(response['message']))
|
||||||
|
if not 'shorturl' in response:
|
||||||
|
PBinCLIError("YOURLS: Unknown error: {}".format(response['message']))
|
||||||
|
else:
|
||||||
|
print("Short Link:\t{}".format(response['shorturl']))
|
||||||
|
else:
|
||||||
|
PBinCLIError("YOURLS: No status, statusCode or message fields in response! Received:\n{}".format(response))
|
||||||
|
|
||||||
|
|
||||||
|
def _clckru(self, url):
|
||||||
|
request = {'url': url}
|
||||||
|
|
||||||
|
try:
|
||||||
result = self.session.post(
|
result = self.session.post(
|
||||||
url = self.apiurl,
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
def _tinyurl(self, url):
|
||||||
|
request = {'url': url}
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
def _gd(self, url):
|
||||||
|
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,
|
proxies = self.proxy,
|
||||||
data = request)
|
data = request)
|
||||||
|
|
||||||
try:
|
response = result.json()
|
||||||
result.raise_for_status()
|
|
||||||
except HTTPError:
|
if 'shorturl' in response:
|
||||||
try:
|
print("Short Link:\t{}".format(response['shorturl']))
|
||||||
response = result.json()
|
|
||||||
except ValueError:
|
|
||||||
PBinCLIError("YOURLS: Unable parse response. Received (size = {}):\n{}".format(len(result.text), result.text))
|
|
||||||
else:
|
|
||||||
PBinCLIError("YOURLS: Received error from API: {} with JSON {}".format(result, response))
|
|
||||||
else:
|
else:
|
||||||
response = result.json()
|
PBinCLIError("{}: got error {} from API: {}".format(
|
||||||
|
|
||||||
if {'status', 'statusCode', 'message'} <= set(response.keys()):
|
|
||||||
if response['status'] == 'fail':
|
|
||||||
PBinCLIError("YOURLS: Received error from API: {}".format(response['message']))
|
|
||||||
if not 'shorturl' in response:
|
|
||||||
PBinCLIError("YOURLS: Unknown error: {}".format(response['message']))
|
|
||||||
else:
|
|
||||||
print("Short Link:\t{}".format(response['shorturl']))
|
|
||||||
else:
|
|
||||||
PBinCLIError("YOURLS: No status, statusCode or message fields in response! Received:\n{}".format(response))
|
|
||||||
|
|
||||||
elif self.api == 'clckru':
|
|
||||||
request = {'url': url}
|
|
||||||
|
|
||||||
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}
|
|
||||||
|
|
||||||
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',
|
"is.gd" if self.api == 'isgd' else 'v.gd',
|
||||||
ex))
|
response['errorcode'],
|
||||||
|
response['errormessage']))
|
||||||
|
|
||||||
elif self.api == 'cuttly':
|
except Exception as ex:
|
||||||
request = {
|
PBinCLIError("{}: unexcepted behavior: {}".format(
|
||||||
'url': url,
|
"is.gd" if self.api == 'isgd' else 'v.gd',
|
||||||
'domain': 0
|
ex))
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
'''
|
def _cuttly(self, url):
|
||||||
# That code needs testing. API requires username and apiKey or accessToken to work.
|
request = {
|
||||||
elif self.api == 'bitly':
|
'url': url,
|
||||||
request = {'url': url}
|
'domain': 0
|
||||||
headers = {'X-Requested-With': 'XMLHttpRequest'}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = self.session.post(
|
result = self.session.post(
|
||||||
url = "https://bitly.com/",
|
url = "https://cutt.ly/scripts/shortenUrl.php",
|
||||||
headers = headers,
|
proxies = self.proxy,
|
||||||
proxies = self.proxy,
|
data = request)
|
||||||
data = request)
|
print("Short Link:\t{}".format(result.text))
|
||||||
response = result.json()
|
except Exception as ex:
|
||||||
if response['data'] and response['status_code'] == 200:
|
PBinCLIError("cutt.ly: unexcepted behavior: {}".format(ex))
|
||||||
print("Short Link:\t{}".format(response['data']['anon_shorten']['link']))
|
|
||||||
elif response['status_txt']:
|
|
||||||
errcode = response['status_txt'] or 'DEFAULT'
|
# [WIP] That code needs testing. API requires username and apiKey or accessToken to work.
|
||||||
friendlyError = {
|
def _bitly(self, url):
|
||||||
'RATE_LIMIT_EXCEEDED': 'Whoa - you\'ve exceeded your quota. Create a free account to keep shortening.',
|
request = {'url': url}
|
||||||
'INVALID_ARG_URL': 'Unable to shorten that link. It is not a valid url.',
|
headers = {'X-Requested-With': 'XMLHttpRequest'}
|
||||||
'INVALID_ARG_LONGURL': 'Unable to shorten that link. It is not a valid url.',
|
|
||||||
'ALREADY_A_BITLY_LINK': 'That is already a Bitly link',
|
try:
|
||||||
'UNKNOWN_ERROR': 'Woops. Something went wrong. Please try again.',
|
result = self.session.post(
|
||||||
'DEFAULT': 'An error occurred'
|
url = "https://bitly.com/",
|
||||||
}
|
headers = headers,
|
||||||
PBinCLIError("bitly: got error from API: {}".format(friendlyError[errcode]))
|
proxies = self.proxy,
|
||||||
except Exception as ex:
|
data = request)
|
||||||
PBinCLIError("bitly: unexcepted behavior: {}".format(ex))
|
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))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user