mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-08-09 08:13:00 +02:00
use nostr.build api to check for file limits
This commit is contained in:
@@ -142,7 +142,7 @@ class TextToSpeech(DVMTaskInterface):
|
|||||||
fs, x = ffmpegio.audio.read("outputs/output.wav", sample_fmt='dbl', ac=1)
|
fs, x = ffmpegio.audio.read("outputs/output.wav", sample_fmt='dbl', ac=1)
|
||||||
ffmpegio.audio.write(final_filename, fs, x, overwrite=True)
|
ffmpegio.audio.write(final_filename, fs, x, overwrite=True)
|
||||||
|
|
||||||
result = await upload_media_to_hoster(final_filename, dvm_config=self.dvm_config)
|
result = await upload_media_to_hoster(final_filename, key_hex=self.dvm_config.PRIVATE_KEY)
|
||||||
print(result)
|
print(result)
|
||||||
return result
|
return result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@@ -7,12 +7,15 @@ def sha256sum(filename):
|
|||||||
with open(filename, 'rb', buffering=0) as f:
|
with open(filename, 'rb', buffering=0) as f:
|
||||||
return hashlib.file_digest(f, 'sha256').hexdigest()
|
return hashlib.file_digest(f, 'sha256').hexdigest()
|
||||||
|
|
||||||
async def generate_nip98_header(filepath, dvm_config):
|
async def generate_nip98_header(pkeys_hex, url="", kind="POST", filepath=""):
|
||||||
keys = Keys.parse(dvm_config.NIP89.PK)
|
keys = Keys.parse(pkeys_hex)
|
||||||
utag = Tag.parse(["u", "https://nostr.build/api/v2/upload/files"])
|
utag = Tag.parse(["u", url])
|
||||||
methodtag = Tag.parse(["method", "POST"])
|
methodtag = Tag.parse(["method", kind])
|
||||||
|
tags = [utag, methodtag]
|
||||||
|
if kind == "POST":
|
||||||
payloadtag = Tag.parse(["payload", sha256sum(filepath)])
|
payloadtag = Tag.parse(["payload", sha256sum(filepath)])
|
||||||
event = EventBuilder(Kind(27235), "", [utag, methodtag, payloadtag]).to_event(keys)
|
tags.append(payloadtag)
|
||||||
|
event = EventBuilder(Kind(27235), "", tags).to_event(keys)
|
||||||
|
|
||||||
encoded_nip98_event = base64.b64encode(event.as_json().encode('utf-8')).decode('utf-8')
|
encoded_nip98_event = base64.b64encode(event.as_json().encode('utf-8')).decode('utf-8')
|
||||||
|
|
||||||
|
@@ -149,26 +149,62 @@ Will probably need to switch to another system in the future.
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
async def upload_media_to_hoster(filepath: str, dvm_config=None, usealternativeforlargefiles=True):
|
async def upload_media_to_hoster(filepath: str, key_hex=None, fallback=True):
|
||||||
|
if key_hex is None:
|
||||||
|
# If we don't pass a key we use the key from .env
|
||||||
|
if os.getenv("NOSTR_BUILD_ACCOUNT_PK"):
|
||||||
|
key_hex = Keys.parse(os.getenv("NOSTR_BUILD_ACCOUNT_PK")).secret_key().to_hex()
|
||||||
|
else:
|
||||||
|
print("No Hex key set, using catbox")
|
||||||
|
uploader = CatboxUploader(filepath)
|
||||||
|
result = uploader.execute()
|
||||||
|
return result
|
||||||
|
|
||||||
if dvm_config is None:
|
print("Uploading Media: " + filepath)
|
||||||
dvm_config = DVMConfig()
|
|
||||||
dvm_config.NIP89.PK = Keys.parse(os.getenv("NOSTR_BUILD_ACCOUNT_PK")).secret_key().to_hex()
|
|
||||||
|
|
||||||
print("Uploading image: " + filepath)
|
|
||||||
try:
|
try:
|
||||||
files = {'file': open(filepath, 'rb')}
|
files = {'file': open(filepath, 'rb')}
|
||||||
file_stats = os.stat(filepath)
|
file_stats = os.stat(filepath)
|
||||||
sizeinmb = file_stats.st_size / (1024 * 1024)
|
sizeinmb = file_stats.st_size / (1024 * 1024)
|
||||||
print("Filesize of Uploaded media: " + str(sizeinmb) + " Mb.")
|
print("Filesize of Uploaded media: " + str(sizeinmb) + " Mb.")
|
||||||
if sizeinmb > 25 and usealternativeforlargefiles:
|
|
||||||
print("Filesize over Nostr.build limited, using catbox")
|
limitinmb = await request_nostr_build_limit(key_hex)
|
||||||
|
|
||||||
|
if sizeinmb > limitinmb:
|
||||||
|
if fallback:
|
||||||
|
print("Filesize over Nostr.build limit, using paid account")
|
||||||
|
key_hex = Keys.parse(os.getenv("NOSTR_BUILD_ACCOUNT_PK")).secret_key().to_hex()
|
||||||
|
limitinmb = await request_nostr_build_limit(key_hex)
|
||||||
|
if sizeinmb > limitinmb:
|
||||||
|
return await upload_nostr_build(key_hex, files, filepath)
|
||||||
|
else:
|
||||||
|
print("Filesize over paid Nostr.build limit, using catbox")
|
||||||
|
uploader = CatboxUploader(filepath)
|
||||||
|
result = uploader.execute()
|
||||||
|
return result
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("Filesize over Nostr.build limit, using catbox")
|
||||||
uploader = CatboxUploader(filepath)
|
uploader = CatboxUploader(filepath)
|
||||||
result = uploader.execute()
|
result = uploader.execute()
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
|
return await upload_nostr_build(key_hex, files, filepath)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
try:
|
||||||
|
# on error we fallback to catbox nevertheless
|
||||||
|
uploader = CatboxUploader(filepath)
|
||||||
|
result = uploader.execute()
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
except:
|
||||||
|
raise Exception("Upload not possible, all hosters didn't work or couldn't generate output")
|
||||||
|
|
||||||
|
|
||||||
|
async def upload_nostr_build(pkey, files, filepath):
|
||||||
url = 'https://nostr.build/api/v2/upload/files'
|
url = 'https://nostr.build/api/v2/upload/files'
|
||||||
auth = await generate_nip98_header(filepath, dvm_config)
|
auth = await generate_nip98_header(pkey, url, "POST", filepath)
|
||||||
headers = {'authorization': auth}
|
headers = {'authorization': auth}
|
||||||
|
|
||||||
response = requests.post(url, files=files, headers=headers)
|
response = requests.post(url, files=files, headers=headers)
|
||||||
@@ -176,15 +212,18 @@ async def upload_media_to_hoster(filepath: str, dvm_config=None, usealternativef
|
|||||||
json_object = json.loads(response.text)
|
json_object = json.loads(response.text)
|
||||||
result = json_object["data"][0]["url"]
|
result = json_object["data"][0]["url"]
|
||||||
return result
|
return result
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
try:
|
async def request_nostr_build_limit(pkey):
|
||||||
uploader = CatboxUploader(filepath)
|
url = 'https://nostr.build/api/v2/upload/limit'
|
||||||
result = uploader.execute()
|
auth = await generate_nip98_header(pkey, url, "GET")
|
||||||
print(result)
|
headers = {'authorization': auth}
|
||||||
return result
|
response = requests.get(url, headers=headers)
|
||||||
except:
|
json_object = json.loads(response.text)
|
||||||
raise Exception("Upload not possible, all hosters didn't work or couldn't generate output")
|
limit = float(json_object["data"]["limit"])
|
||||||
|
limitinmb = limit / (1024 * 1024)
|
||||||
|
print("Limit: " + str(limitinmb) + " MB")
|
||||||
|
return limitinmb
|
||||||
|
|
||||||
|
|
||||||
def build_status_reaction(status, task, amount, content, dvm_config):
|
def build_status_reaction(status, task, amount, content, dvm_config):
|
||||||
|
2
setup.py
2
setup.py
@@ -1,6 +1,6 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
VERSION = '0.6.30'
|
VERSION = '0.6.31'
|
||||||
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
|
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
|
||||||
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')
|
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')
|
||||||
|
|
||||||
|
@@ -17,5 +17,5 @@ if __name__ == '__main__':
|
|||||||
raise FileNotFoundError(f'.env file not found at {env_path} ')
|
raise FileNotFoundError(f'.env file not found at {env_path} ')
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(upload_media_to_hoster("tests/output.wav"))
|
asyncio.run(upload_media_to_hoster("tests/output.wav", "key", True))
|
||||||
# asyncio.run(upload_media_to_hoster("tests/test.jpeg", dvm_config))
|
# asyncio.run(upload_media_to_hoster("tests/test.jpeg", dvm_config))
|
||||||
|
Reference in New Issue
Block a user