chatgpt/pastedb/example_post_multi_backends.py

53 lines
1.5 KiB
Python

import json
import requests
import hashlib
import time
import random
# generate random name for upload
def generate_name():
timestamp = str(int(time.time()))
rand = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=3))
return timestamp + '-' + rand
# define json object to upload
data = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# add timestamp and md5sum to the json object
data['timestamp'] = int(time.time())
json_str = json.dumps(data)
hash_md5 = hashlib.md5(json_str.encode())
data['md5sum'] = hash_md5.hexdigest()
# upload to pastie
pastie_url = 'https://www.pastie.io/documents'
pastie_resp = requests.post(pastie_url, data=json_str.encode(), headers={'Content-Type': 'application/json'})
pastie_key = pastie_resp.json()['key']
pastie_name = 'pastie-' + generate_name()
# store pastie info in dictionary
paste_dict = {}
paste_dict[pastie_name] = {'service': 'pastie', 'key': pastie_key, 'md5sum': data['md5sum']}
# upload to termbin
termbin_url = 'https://termbin.com'
termbin_resp = requests.post(termbin_url, data=json_str.encode(), headers={'Content-Type': 'text/plain'})
termbin_key = termbin_resp.text.strip()
termbin_name = 'termbin-' + generate_name()
# store termbin info in dictionary
paste_dict[termbin_name] = {'service': 'termbin', 'key': termbin_key, 'md5sum': data['md5sum']}
# write paste dictionary to file
with open('paste_dict.json', 'a') as f:
f.write(json.dumps(paste_dict, indent=4))
f.write('\n')
# print out paste dictionary
print(json.dumps(paste_dict, indent=4))