chatgpt/pastedb/lodgeit_python3.py

70 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
LodgeIt!
~~~~~~~~
A script that pastes stuff into the lodgeit pastebin.
"""
import os
import sys
import argparse
import urllib.parse
import urllib.request
VERSION = '0.3'
SERVER_NAME = 'http://paste.openstack.org/'
def upload_paste(paste_content, title=None, language=None, private=None):
"""
Uploads a paste to LodgeIt!
:param paste_content: the content of the paste to upload
:param title: the title of the paste (optional)
:param language: the language of the paste (optional)
:param private: whether the paste should be private (optional)
:return: the URL of the uploaded paste
"""
# build the POST data
data = {
'content': paste_content.encode('utf-8'),
'format': 'text',
}
if title is not None:
data['name'] = title
if language is not None:
data['language'] = language
if private is not None:
data['private'] = private
# make the request
url = urllib.parse.urljoin(SERVER_NAME, '/pastes')
request = urllib.request.Request(url, data=urllib.parse.urlencode(data).encode('utf-8'))
response = urllib.request.urlopen(request)
# parse the response and return the URL of the new paste
location = response.getheader('Location')
if location is None:
raise ValueError('Could not find the URL of the new paste')
return location
def main():
# parse the command-line arguments
parser = argparse.ArgumentParser(description='Upload a paste to LodgeIt!')
parser.add_argument('filename', help='the name of the file to upload')
parser.add_argument('--title', help='the title of the paste')
parser.add_argument('--language', help='the language of the paste')
parser.add_argument('--private', action='store_true', help='make the paste private')
args = parser.parse_args()
# read the content of the file to upload
with open(args.filename, 'r') as f:
paste_content = f.read()
# upload the paste and print the URL of the new paste
url = upload_paste(paste_content, args.title, args.language, args.private)
print(url)
if __name__ == '__main__':
main()