mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-28 18:41:43 +01:00
use nostr.build api to check for file limits
This commit is contained in:
parent
b8ebdddcb3
commit
365e7de6aa
@ -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])
|
||||||
payloadtag = Tag.parse(["payload", sha256sum(filepath)])
|
tags = [utag, methodtag]
|
||||||
event = EventBuilder(Kind(27235), "", [utag, methodtag, payloadtag]).to_event(keys)
|
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')
|
encoded_nip98_event = base64.b64encode(event.as_json().encode('utf-8')).decode('utf-8')
|
||||||
|
|
||||||
|
@ -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:
|
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")
|
|
||||||
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)
|
limitinmb = await request_nostr_build_limit(key_hex)
|
||||||
print(response.text)
|
|
||||||
json_object = json.loads(response.text)
|
if sizeinmb > limitinmb:
|
||||||
result = json_object["data"][0]["url"]
|
if fallback:
|
||||||
return result
|
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:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
try:
|
try:
|
||||||
|
# on error we fallback to catbox nevertheless
|
||||||
uploader = CatboxUploader(filepath)
|
uploader = CatboxUploader(filepath)
|
||||||
result = uploader.execute()
|
result = uploader.execute()
|
||||||
print(result)
|
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")
|
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):
|
def build_status_reaction(status, task, amount, content, dvm_config):
|
||||||
alt_description = "This is a reaction to a NIP90 DVM AI task. "
|
alt_description = "This is a reaction to a NIP90 DVM AI task. "
|
||||||
|
|
||||||
|
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))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user