1
1
mirror of https://github.com/r4sas/PBinCLI synced 2025-01-10 14:58:04 +00:00
PBinCLI/pbincli/api.py

67 lines
2.2 KiB
Python
Raw Normal View History

2017-02-19 10:26:22 +00:00
import requests
2018-05-22 17:44:18 +00:00
class PrivateBin:
2018-05-23 08:14:35 +00:00
def __init__(self, server, proxy=None):
2018-05-22 17:44:18 +00:00
self.server = server
2017-02-19 10:26:22 +00:00
self.headers = {'X-Requested-With': 'JSONHttpRequest'}
2018-05-23 08:14:35 +00:00
if proxy:
2018-05-22 17:44:18 +00:00
self.proxy = {proxy.split('://')[0]: proxy}
2018-02-12 14:28:18 +00:00
else:
2018-05-22 17:44:18 +00:00
self.proxy = {}
2017-02-20 12:13:42 +00:00
2017-02-20 12:13:42 +00:00
def post(self, request):
2019-06-02 14:04:38 +00:00
result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request)
try:
return result.json()
except ValueError as e:
print("ERROR: Unable parse response as json. Received (size = {}):\n".format(len(result.text), result.text))
exit(1)
2017-02-20 12:13:42 +00:00
def get(self, request):
2019-06-02 14:04:38 +00:00
return requests.get(
url = self.server + "?" + request,
headers = self.headers,
proxies = self.proxy).json()
def delete(self, request):
# using try as workaround for versions < 1.3 due to we cant detect
# if server used version 1.2, where auto-deletion is added
try:
2019-06-19 10:58:41 +00:00
result = requests.post(
url = self.server,
headers = self.headers,
proxies = self.proxy,
data = request).json()
2019-06-02 14:04:38 +00:00
except ValueError as e:
# unable parse response as json because it can be empty (1.2), so simulate correct answer
from json import loads as json_loads
2019-06-19 10:58:41 +00:00
result = json_loads('{"status":0}')
if not result['status']:
print("Paste successfully deleted!")
elif result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message']))
exit(1)
else:
print("Something went wrong...\nError: Empty response.")
exit(1)
2017-02-20 12:13:42 +00:00
2019-06-02 14:04:38 +00:00
def getVersion(self):
jsonldSchema = requests.get(
url = self.server + '?jsonld=paste',
proxies = self.proxy).json()
return jsonldSchema['@context']['v']['@value'] \
if ('@context' in jsonldSchema and
'v' in jsonldSchema['@context'] and
'@value' in jsonldSchema['@context']['v']) \
else 1