chatgpt/pastedb/pastedb01/upload.py

47 lines
1.3 KiB
Python
Raw Normal View History

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}")