[wip] url shortener support (#19)

Signed-off-by: r4sas <r4sas@i2pmail.org>
This commit is contained in:
r4sas
2019-09-17 08:55:59 +00:00
parent 6b6c33e545
commit 635c87dabd
6 changed files with 75 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
import requests
from pbincli.utils import PBinCLIError
class PrivateBin:
def __init__(self, settings=None):
@@ -27,8 +28,7 @@ class PrivateBin:
try:
return result.json()
except ValueError:
print("ERROR: Unable parse response as json. Received (size = {}):\n{}".format(len(result.text), result.text))
exit(1)
PBinCLIError("Unable parse response as json. Received (size = {}):\n{}".format(len(result.text), result.text))
def get(self, request):
@@ -56,11 +56,9 @@ class PrivateBin:
if not result['status']:
print("Paste successfully deleted!")
elif result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message']))
exit(1)
PBinCLIError("Something went wrong...\nError:\t\t{}".format(result['message']))
else:
print("Something went wrong...\nError: Empty response.")
exit(1)
PBinCLIError("Something went wrong...\nError: Empty response.")
def getVersion(self):
@@ -74,9 +72,24 @@ class PrivateBin:
else 1
class Shortener:
def __init__(self, settings=None):
self.server = settings['server']
self.headers = {'X-Requested-With': 'JSONHttpRequest'}
"""Some parts of this class was taken from
python-yourls (https://github.com/tflink/python-yourls/) library
"""
def __init__(self, apiurl, username=None, password=None, token=None, settings=None):
# we checking which service is used, because some services doesn't require
# any authentication, or have only one domain on which it working
if settings['api'] == 'yourls':
if not apiurl:
PBinCLIError("YOURLS: An API URL is required")
self.apiurl = apiurl
if not username or not password:
if not self.token:
PBinCLIError("YOURLS: username and password or token are required")
else:
self.auth_args = {'signature': token}
else:
self.auth_args = {'username': self.username, 'password': self.password}
if settings['proxy']:
self.proxy = {settings['proxy'].split('://')[0]: settings['proxy']}
@@ -90,3 +103,33 @@ class Shortener:
self.session = requests.Session()
self.session.verify = settings['nocheckcert']
def yourls(self, url):
data_format = 'json'
args = {'action': 'shorturl', 'format': data_format, 'url': url}
reqest = dict(args.items() + self.auth_args.items())
result = self.session.post(
url = self.apiurl,
proxies = self.proxy,
data = request)
try:
result.json()
if result['status'] == 'fail' and result['code'] == 'error:keyword':
PBinCLIError("YOURLS: Received error from API: {}".format(result['message']))
if not 'shorturl' in result:
PBinCLIError("YOURLS: Unknown error: {}".format(result['message']))
print("Paste short URL: {}".format(result['shorturl']))
except ValueError:
PBinCLIError("YOURLS: Unable parse response. Received (size = {}):\n{}".format(len(result.text), result.text))
def clck(self, url):
# from urllib.parse import quote_plus
request = {'url': url}
result = self.session.post(
url = "https://clck.ru/--",
proxies = self.proxy,
data = request)
print("Paste short URL: {}".format(result))