chatgpt/pastedb/example_post_pastie.py

47 lines
1.6 KiB
Python

import requests
import json
import os
import time
import hashlib
# Define your JSON object with a unique timestamp and MD5 hash
my_json = {'timestamp': int(time.time()), 'name': 'John', 'age': 30, 'city': 'New York'}
json_str = json.dumps(my_json, sort_keys=True)
md5_hash = hashlib.md5(json_str.encode()).hexdigest()
my_json['md5'] = md5_hash
# Define the API endpoint for Pastie
pastie_url = 'https://pastie.io/documents'
# Upload the JSON object to Pastie and get the URL
response = requests.post(pastie_url, data=json.dumps(my_json))
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}')
# Add the URL and service name to the dictionary for later querying
paste_dict = {}
if os.path.isfile('paste_dict.json'):
with open('paste_dict.json', 'r') as f:
paste_dict = json.load(f)
paste_dict[key] = {'url': pastie_url, 'service': 'Pastie'}
# Write the URL dictionary to a file on disk
with open('paste_dict.json', 'w') as f:
json.dump(paste_dict, f, indent=4)
else:
print('Error uploading JSON object to Pastie')
# Query the dictionary for the URL of a specific paste
if os.path.isfile('paste_dict.json'):
with open('paste_dict.json', 'r') as f:
paste_dict = json.load(f)
key_to_query = key if key in paste_dict else list(paste_dict.keys())[0]
url = paste_dict[key_to_query]['url']
service = paste_dict[key_to_query]['service']
print(f'URL for paste with key {key_to_query} (stored on {service}): {url}')
else:
print('URL dictionary file not found')