chatgpt/pastedb/example_post_sprunge.py

40 lines
1.2 KiB
Python

import requests
import json
import hashlib
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