Skip to content

Instantly share code, notes, and snippets.

@Jxck-S
Created May 9, 2023 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jxck-S/2a95655bf42cc344885709d647c717ab to your computer and use it in GitHub Desktop.
Save Jxck-S/2a95655bf42cc344885709d647c717ab to your computer and use it in GitHub Desktop.
Upload image/video to various nostr focused sharing platforms
def nostr_build_upload(file_name):
"""Uploads an image/video to nostr build and returns the URL of it."""
import requests
url = 'https://nostr.build/api/upload/ios.php'
with open(file_name, 'rb') as f:
files = {'fileToUpload': f}
data = {'submit': 'Upload'}
response = requests.post(url, files=files, data=data)
response.raise_for_status
if response.status_code == 200:
return response.text.replace("\\", "").strip("\"")
def void_cat_upload(file_name):
"""Uploads an image/video to void.cat and returns the URL of it."""
import requests
import mimetypes
mime_type, _ = mimetypes.guess_type(file_name)
with open(file_name, "rb") as f:
data = f.read()
headers = {
"V-Content-Type": mime_type,
"V-Filename": file_name,
}
url = "https://void.cat/upload?cli=true"
response = requests.post(url, headers=headers, data=data)
response.raise_for_status
return response.text
def upload_on_any(file_name):
"""Uploads an image/video to nostr build or void cat, depending on if the first fails."""
from requests.exceptions import HTTPError
try:
url = nostr_build_upload(file_name)
except HTTPError:
pass
else:
return url
try:
url = void_cat_upload(file_name)
except HTTPError:
return None
else:
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment