mpaste/pastebins/paste_python.py

49 lines
1.4 KiB
Python
Raw Permalink Normal View History

import requests
def publish_to_pinnwand(content: str, expiry: str = "1day", lexer: str = "text", name: str = None, base_url: str = "https://bpa.st") -> str:
"""
Uploads content to pinnwand pastebin and returns the link to the paste.
Args:
- content: The content of the paste.
- expiry: Expiry time for the paste. Defaults to "1day".
- lexer: The lexer to use for syntax highlighting. Defaults to "text".
- name: Optional name for the file.
- base_url: The base URL of the pinnwand instance. Defaults to "https://bpa.st".
Returns:
- The URL of the created paste.
"""
# API endpoint for creating new pastes
api_endpoint = f"{base_url}/api/v1/paste"
# Create the payload
payload = {
"expiry": expiry,
"files": [
{
"lexer": lexer,
"content": content
}
]
}
# Add the name field if provided
if name:
payload["files"][0]["name"] = name
# Send the POST request to the API
response = requests.post(api_endpoint, json=payload)
# Check if the request was successful
if response.status_code == 200:
# Parse the response JSON
response_data = response.json()
paste_url = response_data.get("link")
return paste_url
else:
raise Exception(f"Failed to upload to pinnwand, status code: {response.status_code}, message: {response.text}")