made some work for pastedb / pastecache..
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"name": "Joe Chuck", "age": 222, "city": "New Jersey"}
|
||||
Binary file not shown.
+161
@@ -0,0 +1,161 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import getopt
|
||||
import http.cookiejar
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from http.cookies import SimpleCookie
|
||||
from json import loads as json_loads
|
||||
from os import environ
|
||||
|
||||
_headers = {"Referer": 'https://rentry.co'}
|
||||
|
||||
|
||||
class UrllibClient:
|
||||
"""Simple HTTP Session Client, keeps cookies."""
|
||||
|
||||
def __init__(self):
|
||||
self.cookie_jar = http.cookiejar.CookieJar()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie_jar))
|
||||
urllib.request.install_opener(self.opener)
|
||||
|
||||
def get(self, url, headers={}):
|
||||
request = urllib.request.Request(url, headers=headers)
|
||||
return self._request(request)
|
||||
|
||||
def post(self, url, data=None, headers={}):
|
||||
postdata = urllib.parse.urlencode(data).encode()
|
||||
request = urllib.request.Request(url, postdata, headers)
|
||||
return self._request(request)
|
||||
|
||||
def _request(self, request):
|
||||
response = self.opener.open(request)
|
||||
response.status_code = response.getcode()
|
||||
response.data = response.read().decode('utf-8')
|
||||
return response
|
||||
|
||||
|
||||
def raw(url):
|
||||
client = UrllibClient()
|
||||
return json_loads(client.get('https://rentry.co/api/raw/{}'.format(url)).data)
|
||||
|
||||
|
||||
def new(url, edit_code, text):
|
||||
client, cookie = UrllibClient(), SimpleCookie()
|
||||
|
||||
cookie.load(vars(client.get('https://rentry.co'))['headers']['Set-Cookie'])
|
||||
csrftoken = cookie['csrftoken'].value
|
||||
|
||||
payload = {
|
||||
'csrfmiddlewaretoken': csrftoken,
|
||||
'url': url,
|
||||
'edit_code': edit_code,
|
||||
'text': text
|
||||
}
|
||||
|
||||
return json_loads(client.post('https://rentry.co/api/new', payload, headers=_headers).data)
|
||||
|
||||
|
||||
def edit(url, edit_code, text):
|
||||
client, cookie = UrllibClient(), SimpleCookie()
|
||||
|
||||
cookie.load(vars(client.get('https://rentry.co'))['headers']['Set-Cookie'])
|
||||
csrftoken = cookie['csrftoken'].value
|
||||
|
||||
payload = {
|
||||
'csrfmiddlewaretoken': csrftoken,
|
||||
'edit_code': edit_code,
|
||||
'text': text
|
||||
}
|
||||
|
||||
return json_loads(client.post('https://rentry.co/api/edit/{}'.format(url), payload, headers=_headers).data)
|
||||
|
||||
|
||||
def usage():
|
||||
print('''
|
||||
Usage: rentry {new | edit | raw} {-h | --help} {-u | --url} {-p | --edit-code} text
|
||||
|
||||
Commands:
|
||||
new create a new entry
|
||||
edit edit an existing entry
|
||||
raw get raw markdown text of an existing entry
|
||||
|
||||
Options:
|
||||
-h, --help show this help message and exit
|
||||
-u, --url URL url for the entry, random if not specified
|
||||
-p, --edit-code EDIT-CODE edit code for the entry, random if not specified
|
||||
|
||||
Examples:
|
||||
rentry new 'markdown text' # new entry with random url and edit code
|
||||
rentry new -p pw -u example 'text' # with custom edit code and url
|
||||
rentry edit -p pw -u example 'text' # edit the example entry
|
||||
cat FILE | rentry new # read from FILE and paste it to rentry
|
||||
cat FILE | rentry edit -p pw -u example # read from FILE and edit the example entry
|
||||
rentry raw -u example # get raw markdown text
|
||||
rentry raw -u https://rentry.co/example # -u accepts absolute and relative urls
|
||||
''')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
environ.pop('POSIXLY_CORRECT', None)
|
||||
opts, args = getopt.gnu_getopt(sys.argv[1:], "hu:p:", ["help", "url=", "edit-code="])
|
||||
except getopt.GetoptError as e:
|
||||
sys.exit("error: {}".format(e))
|
||||
|
||||
command, url, edit_code, text = None, '', '', None
|
||||
|
||||
for o, a in opts:
|
||||
if o in ("-h", "--help"):
|
||||
usage()
|
||||
sys.exit()
|
||||
elif o in ("-u", "--url"):
|
||||
url = urllib.parse.urlparse(a).path.strip('/')
|
||||
elif o in ("-p", "--edit-code"):
|
||||
edit_code = a
|
||||
|
||||
command = (args[0:1] or [None])[0]
|
||||
command or sys.exit(usage())
|
||||
command in ['new', 'edit', 'raw'] or sys.exit('error: command must be new, edit or raw')
|
||||
|
||||
text = (args[1:2] or [None])[0]
|
||||
if not text and command != 'raw':
|
||||
text = sys.stdin.read().strip()
|
||||
text or sys.exit('error: text is required')
|
||||
|
||||
if command == 'new':
|
||||
response = new(url, edit_code, text)
|
||||
if response['status'] != '200':
|
||||
print('error: {}'.format(response['content']))
|
||||
try:
|
||||
for i in response['errors'].split('.'):
|
||||
i and print(i)
|
||||
sys.exit(1)
|
||||
except:
|
||||
sys.exit(1)
|
||||
else:
|
||||
print('Url: {}\nEdit code: {}'.format(response['url'], response['edit_code']))
|
||||
|
||||
elif command == 'edit':
|
||||
url or sys.exit('error: url is required')
|
||||
edit_code or sys.exit('error: edit code is required')
|
||||
|
||||
response = edit(url, edit_code, text)
|
||||
if response['status'] != '200':
|
||||
print('error: {}'.format(response['content']))
|
||||
try:
|
||||
for i in response['errors'].split('.'):
|
||||
i and print(i)
|
||||
sys.exit(1)
|
||||
except:
|
||||
sys.exit(1)
|
||||
else:
|
||||
print('Ok')
|
||||
|
||||
elif command == 'raw':
|
||||
url or sys.exit('error: url is required')
|
||||
response = raw(url)
|
||||
if response['status'] != '200':
|
||||
sys.exit('error: {}'.format(response['content']))
|
||||
print(response['content'])
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
{
|
||||
"name": "1680551932-b74d72",
|
||||
"pastie_blzoeg": {
|
||||
"service": "pastie",
|
||||
"key": "https://pastie.io/blzoeg",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
},
|
||||
"rentry_QDmcP3gr": {
|
||||
"service": "Rentry",
|
||||
"key": "https://rentry.co/hqcohp",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
},
|
||||
"dpaste_9W5AB4Y2V": {
|
||||
"service": "dpaste",
|
||||
"key": "https://dpaste.com/9W5AB4Y2V",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
},
|
||||
"sprunge_j9uXbd_9fd30": {
|
||||
"service": "sprunge",
|
||||
"key": "http://sprunge.us/j9uXbd",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
},
|
||||
"defau_9fd30": {
|
||||
"service": "p.defau.lt",
|
||||
"key": "https://p.defau.lt/?ExQfLnJ_aDEKYIqkpwv6cQ",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
},
|
||||
"opendev_819477_9fd30": {
|
||||
"service": "opendev",
|
||||
"key": "https://paste.opendev.org/json/",
|
||||
"md5sum": "9fd3002da661e6ca38a2e8a49daafb1b"
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
def serviceTag():
|
||||
return 'p.defau.lt'
|
||||
|
||||
def upload(data):
|
||||
json_data = json.dumps(data)
|
||||
md5sum = hashlib.md5(json_data.encode('utf-8')).hexdigest()
|
||||
|
||||
url = 'https://p.defau.lt/submit.php'
|
||||
response = requests.post(url, data={'code': json_data})
|
||||
|
||||
if response.status_code == 200:
|
||||
key = response.url
|
||||
name = 'defau_' + md5sum[:5]
|
||||
return {'name': name, 'service': 'p.defau.lt', 'key': key, 'md5sum': md5sum}
|
||||
else:
|
||||
return None
|
||||
|
||||
def get(trace):
|
||||
url = trace[key]
|
||||
response = requests.request.get(url)
|
||||
return response.content
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import hashlib
|
||||
import json
|
||||
import requests
|
||||
|
||||
def upload(data):
|
||||
try:
|
||||
content = json.dumps(data)
|
||||
syntax = 'json'
|
||||
expiry_days = ''
|
||||
|
||||
r = requests.post('https://dpaste.com/api/v2/',
|
||||
data={'content': content,
|
||||
'syntax': syntax,
|
||||
'expiry_days': expiry_days},
|
||||
headers={'User-Agent': 'My Python Project'})
|
||||
|
||||
if r.status_code == 201:
|
||||
dpaste_url = r.headers['Location']
|
||||
#print(f'JSON object uploaded to dpaste.com: {dpaste_url}')
|
||||
md5sum = hashlib.md5(content.encode('utf-8')).hexdigest()
|
||||
|
||||
return {
|
||||
'service': 'dpaste',
|
||||
'key': dpaste_url,
|
||||
'md5sum': md5sum,
|
||||
'name': 'dpaste_' + dpaste_url.rsplit('/', 1)[-1]
|
||||
}
|
||||
else:
|
||||
return None
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import json
|
||||
import hashlib
|
||||
import http.cookiejar
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from http.cookies import SimpleCookie
|
||||
|
||||
_headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
|
||||
|
||||
class UrllibClient:
|
||||
"""Simple HTTP Session Client, keeps cookies."""
|
||||
|
||||
def __init__(self):
|
||||
self.cookie_jar = http.cookiejar.CookieJar()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie_jar))
|
||||
urllib.request.install_opener(self.opener)
|
||||
|
||||
def get(self, url, headers={}):
|
||||
request = urllib.request.Request(url, headers=headers)
|
||||
return self._request(request)
|
||||
|
||||
def post(self, url, data=None, headers={}):
|
||||
postdata = urllib.parse.urlencode(data).encode()
|
||||
request = urllib.request.Request(url, postdata, headers)
|
||||
return self._request(request)
|
||||
|
||||
def _request(self, request):
|
||||
response = self.opener.open(request)
|
||||
response.status_code = response.getcode()
|
||||
response.data = response.read().decode('utf-8')
|
||||
return response
|
||||
|
||||
|
||||
def json_loads(string):
|
||||
try:
|
||||
return json.loads(string)
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def upload(data):
|
||||
client, cookie = UrllibClient(), SimpleCookie()
|
||||
|
||||
cookie.load(vars(client.get('https://rentry.co'))['headers']['Set-Cookie'])
|
||||
csrftoken = cookie['csrftoken'].value
|
||||
|
||||
json_data = json.dumps(data)
|
||||
md5sum = hashlib.md5(json_data.encode('utf-8')).hexdigest()
|
||||
|
||||
payload = {
|
||||
'csrfmiddlewaretoken': csrftoken,
|
||||
'url': md5sum,
|
||||
'edit_code': '',
|
||||
'text': json_data
|
||||
}
|
||||
|
||||
response = client.post('https://rentry.co/api/new', payload, headers=_headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
json_response = json_loads(response.data)
|
||||
return {
|
||||
"service": "rentry",
|
||||
"name": json_response["slug"],
|
||||
"key": f"https://rentry.co/{json_response['slug']}",
|
||||
"md5sum": md5sum
|
||||
}
|
||||
else:
|
||||
return None
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
url = 'https://paste.opendev.org/json/'
|
||||
|
||||
def upload(data):
|
||||
try:
|
||||
content = json.dumps(data)
|
||||
|
||||
# create JSON payload for new paste
|
||||
payload = {
|
||||
'language': 'text',
|
||||
'code': content,
|
||||
'private': False
|
||||
#'expire': '1day'
|
||||
}
|
||||
|
||||
# send request to create new paste
|
||||
response = requests.post(url + '?method=pastes.newPaste', json=payload)
|
||||
|
||||
status = response.status_code
|
||||
paste_id = response.json()['data']
|
||||
|
||||
if status == 200:
|
||||
#print(f'JSON object uploaded to dpaste.com: {dpaste_url}')
|
||||
md5sum = hashlib.md5(content.encode('utf-8')).hexdigest()
|
||||
|
||||
return {
|
||||
'service': "opendev",
|
||||
'key': url,
|
||||
'md5sum': md5sum,
|
||||
'name': 'opendev_' + paste_id + '_' + md5sum[:5]
|
||||
}
|
||||
else:
|
||||
return None
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
# we can get the paste back by:
|
||||
## $ > curl -d '{"paste_id":819463}' -H 'Content-Type: application/json' https://paste.opendev.org/json/?method=pastes.getPaste |jq .data.code
|
||||
@@ -0,0 +1,20 @@
|
||||
import requests
|
||||
|
||||
def upload(data):
|
||||
url = 'https://paste2.org/'
|
||||
|
||||
response = requests.post(url, data={'data': data})
|
||||
response.raise_for_status()
|
||||
|
||||
# Extract the URL of the uploaded paste from the response
|
||||
paste_url = None
|
||||
for line in response.text.splitlines():
|
||||
if line.startswith('<input type="text" id="paste-url" value="'):
|
||||
paste_url = line.split('"')[3]
|
||||
break
|
||||
|
||||
if paste_url is None:
|
||||
raise ValueError('Could not extract paste URL from response')
|
||||
|
||||
return paste_url
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
def upload(data):
|
||||
try:
|
||||
json_data = json.dumps(data)
|
||||
md5sum = hashlib.md5(json_data.encode('utf-8')).hexdigest()
|
||||
response = requests.post('https://pastie.io/documents', data=json_data)
|
||||
if response.status_code == 200:
|
||||
key = response.json()['key']
|
||||
pastie_url = f'https://pastie.io/{key}'
|
||||
# print(f'JSON object uploaded to Pastie: {pastie_url}')
|
||||
|
||||
return {
|
||||
"service": "pastie",
|
||||
"name": 'pastie_' + key,
|
||||
"key": pastie_url,
|
||||
"md5sum": md5sum
|
||||
}
|
||||
else:
|
||||
return None
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import subprocess
|
||||
import json
|
||||
import hashlib
|
||||
import re
|
||||
|
||||
|
||||
def upload(data):
|
||||
md5sum = hashlib.md5(json.dumps(data).encode('utf-8')).hexdigest()
|
||||
command = ['./external/rentry', 'new', json.dumps(data)]
|
||||
output = subprocess.check_output(command, universal_newlines=True)
|
||||
#print(output)
|
||||
lines = output.strip().split('\n')
|
||||
url = re.search("(?P<url>https?://[^\s]+)", lines[0]).group("url")
|
||||
edit_code = lines[1].split(':')[-1].strip()
|
||||
return {'name': 'rentry_' + edit_code, 'service': 'Rentry', 'key': url, 'md5sum': md5sum}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
import re
|
||||
|
||||
url = 'http://sprunge.us'
|
||||
|
||||
def upload(data):
|
||||
try:
|
||||
json_data = json.dumps(data)
|
||||
md5sum = hashlib.md5(json_data.encode('utf-8')).hexdigest()
|
||||
|
||||
# Send the HTTP POST request to the Sprunge API
|
||||
response = requests.post(url, data={'sprunge': json_data})
|
||||
if response.status_code == 200:
|
||||
|
||||
# Get the URL of the uploaded text from the response body
|
||||
sprunge_url = response.text.strip()
|
||||
|
||||
#print('Uploaded to:', sprunge_url)
|
||||
|
||||
# Use a regular expression to extract the random ID from the URL
|
||||
match = re.match(r'^http://sprunge\.us/(\w+)$', sprunge_url)
|
||||
if match:
|
||||
random_id = match.group(1)
|
||||
#print('Random ID:', random_id)
|
||||
key = "sprunge_" + random_id + '_' + md5sum[:5]
|
||||
else:
|
||||
print('Invalid Sprunge URL:', sprunge_url)
|
||||
|
||||
return {
|
||||
"service": "sprunge",
|
||||
"name": key,
|
||||
"key": sprunge_url,
|
||||
"md5sum": md5sum
|
||||
}
|
||||
else:
|
||||
return None
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
@@ -0,0 +1,26 @@
|
||||
import subprocess
|
||||
import hashlib
|
||||
import time
|
||||
import random
|
||||
import string
|
||||
|
||||
def generate_name():
|
||||
"""Generate a random name for the paste"""
|
||||
ts = int(time.time())
|
||||
rand_str = ''.join(random.choices(string.ascii_lowercase, k=5))
|
||||
name = f"termbin-{ts}-{rand_str}"
|
||||
return name
|
||||
|
||||
def upload(data):
|
||||
"""Upload the data to termbin.com"""
|
||||
name = generate_name()
|
||||
try:
|
||||
cmd = f"echo '{data}' | nc termbin.com 9999"
|
||||
response = subprocess.check_output(cmd, shell=True, timeout=5).decode()
|
||||
url = f"https://termbin.com/{name}"
|
||||
md5sum = hashlib.md5(data.encode('utf-8')).hexdigest()
|
||||
return {'service': 'termbin', 'key': url, 'md5sum': md5sum}
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
|
||||
print(f"Upload failed with error: {e}")
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import json
|
||||
import time
|
||||
import hashlib
|
||||
from services import pastie, dpaste, rentry, defau, sprunge, opendev
|
||||
|
||||
# Load the JSON object from file
|
||||
with open('data.json') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Generate a unique name for the upload
|
||||
name = f"{time.time():.0f}-{hashlib.sha256(str(data).encode()).hexdigest()[:6]}"
|
||||
|
||||
# Upload to the available services
|
||||
paste_dict = {'name': name}
|
||||
successes = []
|
||||
failures = []
|
||||
for service in [pastie, rentry, dpaste, sprunge, defau, opendev]:
|
||||
try:
|
||||
result = service.upload(data)
|
||||
paste_dict[result['name']] = {
|
||||
'service': result['service'],
|
||||
'key': result['key'],
|
||||
'md5sum': result['md5sum'],
|
||||
}
|
||||
successes.append(result['name'])
|
||||
except Exception as e:
|
||||
failures.append(f"{service.__name__}: {str(e)}")
|
||||
|
||||
# Update the paste_dict file
|
||||
with open('paste_dict.json', 'r+') as f:
|
||||
try:
|
||||
paste_dict.update(json.load(f))
|
||||
f.seek(0)
|
||||
except json.decoder.JSONDecodeError:
|
||||
pass # ignore error if file is empty
|
||||
json.dump(paste_dict, f, indent=2)
|
||||
|
||||
# Print upload results
|
||||
print(f"Upload successful to {len(successes)}/{len(successes)+len(failures)} services:")
|
||||
for name in successes:
|
||||
print(f"- {name}")
|
||||
if failures:
|
||||
print("Upload failed to the following services:")
|
||||
for error in failures:
|
||||
print(f"- {error}")
|
||||
|
||||
Reference in New Issue
Block a user