diff --git a/nostr_dvm/tasks/texttospeech.py b/nostr_dvm/tasks/texttospeech.py index 6484b35..2f82584 100644 --- a/nostr_dvm/tasks/texttospeech.py +++ b/nostr_dvm/tasks/texttospeech.py @@ -142,7 +142,7 @@ class TextToSpeech(DVMTaskInterface): fs, x = ffmpegio.audio.read("outputs/output.wav", sample_fmt='dbl', ac=1) 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) return result except Exception as e: diff --git a/nostr_dvm/utils/nip98_utils.py b/nostr_dvm/utils/nip98_utils.py index 0ef4866..b83b34a 100644 --- a/nostr_dvm/utils/nip98_utils.py +++ b/nostr_dvm/utils/nip98_utils.py @@ -7,12 +7,15 @@ def sha256sum(filename): with open(filename, 'rb', buffering=0) as f: return hashlib.file_digest(f, 'sha256').hexdigest() -async def generate_nip98_header(filepath, dvm_config): - keys = Keys.parse(dvm_config.NIP89.PK) - utag = Tag.parse(["u", "https://nostr.build/api/v2/upload/files"]) - methodtag = Tag.parse(["method", "POST"]) - payloadtag = Tag.parse(["payload", sha256sum(filepath)]) - event = EventBuilder(Kind(27235), "", [utag, methodtag, payloadtag]).to_event(keys) +async def generate_nip98_header(pkeys_hex, url="", kind="POST", filepath=""): + keys = Keys.parse(pkeys_hex) + utag = Tag.parse(["u", url]) + methodtag = Tag.parse(["method", kind]) + tags = [utag, methodtag] + if kind == "POST": + payloadtag = Tag.parse(["payload", sha256sum(filepath)]) + 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') diff --git a/nostr_dvm/utils/output_utils.py b/nostr_dvm/utils/output_utils.py index 201e310..5e327ae 100644 --- a/nostr_dvm/utils/output_utils.py +++ b/nostr_dvm/utils/output_utils.py @@ -149,36 +149,51 @@ 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: - dvm_config = DVMConfig() - dvm_config.NIP89.PK = Keys.parse(os.getenv("NOSTR_BUILD_ACCOUNT_PK")).secret_key().to_hex() - - print("Uploading image: " + filepath) + print("Uploading Media: " + filepath) try: files = {'file': open(filepath, 'rb')} file_stats = os.stat(filepath) sizeinmb = file_stats.st_size / (1024 * 1024) print("Filesize of Uploaded media: " + str(sizeinmb) + " Mb.") - if sizeinmb > 25 and usealternativeforlargefiles: - print("Filesize over Nostr.build limited, using catbox") - uploader = CatboxUploader(filepath) - result = uploader.execute() - return result - else: - url = 'https://nostr.build/api/v2/upload/files' - auth = await generate_nip98_header(filepath, dvm_config) - headers = {'authorization': auth} - response = requests.post(url, files=files, headers=headers) - print(response.text) - json_object = json.loads(response.text) - result = json_object["data"][0]["url"] - return result + 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) + result = uploader.execute() + return result + 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) @@ -187,6 +202,30 @@ async def upload_media_to_hoster(filepath: str, dvm_config=None, usealternativef 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' + auth = await generate_nip98_header(pkey, url, "POST", filepath) + headers = {'authorization': auth} + + response = requests.post(url, files=files, headers=headers) + print(response.text) + json_object = json.loads(response.text) + result = json_object["data"][0]["url"] + return result + + +async def request_nostr_build_limit(pkey): + url = 'https://nostr.build/api/v2/upload/limit' + auth = await generate_nip98_header(pkey, url, "GET") + headers = {'authorization': auth} + response = requests.get(url, headers=headers) + json_object = json.loads(response.text) + 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): alt_description = "This is a reaction to a NIP90 DVM AI task. " diff --git a/setup.py b/setup.py index ff60b85..180abb0 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ 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' LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information') diff --git a/tests/upload_hoster.py b/tests/upload_hoster.py index 15d04f6..60d125e 100644 --- a/tests/upload_hoster.py +++ b/tests/upload_hoster.py @@ -17,5 +17,5 @@ if __name__ == '__main__': 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))