skip doing the job if addressed to someone else, add media converter

This commit is contained in:
Believethehype
2023-11-30 08:07:30 +01:00
parent 42b3d763f1
commit dd1b69bc0b
19 changed files with 445 additions and 201 deletions

View File

@@ -148,6 +148,7 @@ def get_amount_per_task(task, dvm_config, duration=1):
for dvm in dvm_config.SUPPORTED_DVMS: # this is currently just one
if dvm.TASK == task:
amount = dvm.FIX_COST + (dvm.PER_UNIT_COST * duration)
print("Cost: " + str(amount))
return amount
else:
print("[" + dvm_config.SUPPORTED_DVMS[

View File

@@ -3,34 +3,37 @@ from dataclasses import dataclass
from nostr_sdk import Event
class EventDefinitions:
KIND_DM: int = 4
KIND_ZAP: int = 9735
KIND_ANNOUNCEMENT: int = 31990
KIND_NIP94_METADATA: int = 1063
KIND_FEEDBACK: int = 7000
KIND_DM = 4
KIND_ZAP = 9735
KIND_ANNOUNCEMENT = 31990
KIND_NIP94_METADATA = 1063
KIND_FEEDBACK = 7000
KIND_NIP90_EXTRACT_TEXT = 5000
KIND_NIP90_RESULT_EXTRACT_TEXT = 6000
KIND_NIP90_SUMMARIZE_TEXT = 5001
KIND_NIP90_RESULT_SUMMARIZE_TEXT = 6001
KIND_NIP90_TRANSLATE_TEXT = 5002
KIND_NIP90_RESULT_TRANSLATE_TEXT = 6002
KIND_NIP90_GENERATE_TEXT = 5050
KIND_NIP90_RESULT_GENERATE_TEXT = 6050
KIND_NIP90_GENERATE_IMAGE = 5100
KIND_NIP90_RESULT_GENERATE_IMAGE = 6100
KIND_NIP90_RECOMMEND_NOTES = 65006
KIND_NIP90_RESULT_RECOMMEND_NOTES = 65001
KIND_NIP90_RECOMMEND_USERS = 65007
KIND_NIP90_RESULT_RECOMMEND_USERS = 65001
KIND_NIP90_CONVERT_VIDEO = 5200
KIND_NIP90_RESULT_CONVERT_VIDEO = 6200
KIND_NIP90_CONTENT_DISCOVERY = 5300
KIND_NIP90_RESULT_CONTENT_DISCOVERY = 6300
KIND_NIP90_PEOPLE_DISCOVERY = 5301
KIND_NIP90_GENERIC = 5999
KIND_NIP90_RESULT_GENERIC = 6999
ANY_RESULT = [KIND_NIP90_RESULT_EXTRACT_TEXT,
KIND_NIP90_RESULT_SUMMARIZE_TEXT,
KIND_NIP90_RESULT_TRANSLATE_TEXT,
KIND_NIP90_RESULT_GENERATE_TEXT,
KIND_NIP90_RESULT_GENERATE_IMAGE,
KIND_NIP90_RESULT_RECOMMEND_NOTES,
KIND_NIP90_RESULT_RECOMMEND_USERS,
KIND_NIP90_CONTENT_DISCOVERY,
KIND_NIP90_PEOPLE_DISCOVERY,
KIND_NIP90_RESULT_CONVERT_VIDEO,
KIND_NIP90_RESULT_CONTENT_DISCOVERY,
KIND_NIP90_RESULT_GENERIC]

View File

@@ -9,7 +9,7 @@ from utils.nostr_utils import get_event_by_id
def input_data_file_duration(event, dvm_config, client, start=0, end=0):
print("[" + dvm_config.NIP89.NAME + "] Getting Duration of the Media file..")
#print("[" + dvm_config.NIP89.NAME + "] Getting Duration of the Media file..")
input_value = ""
input_type = "url"
for tag in event.tags():
@@ -32,7 +32,7 @@ def input_data_file_duration(event, dvm_config, client, start=0, end=0):
if input_type == "url":
source_type = check_source_type(input_value)
filename, start, end, type = get_file_start_end_type(input_value, source_type, start, end)
filename, start, end, type = get_file_start_end_type(input_value, source_type, start, end, True)
if type != "audio" and type != "video":
return 1
if filename == "" or filename is None:
@@ -52,7 +52,7 @@ def input_data_file_duration(event, dvm_config, client, start=0, end=0):
return 1
def organize_input_data_to_audio(input_value, input_type, start, end, dvm_config, client) -> str:
def organize_input_media_data(input_value, input_type, start, end, dvm_config, client, process=True, media_format="audio/mp3") -> str:
if input_type == "event": # NIP94 event
evt = get_event_by_id(input_value, client=client, config=dvm_config)
if evt is not None:
@@ -60,9 +60,15 @@ def organize_input_data_to_audio(input_value, input_type, start, end, dvm_config
if input_type == "url":
source_type = check_source_type(input_value)
filename, start, end, type = get_file_start_end_type(input_value, source_type, start, end)
audio_only = True
if media_format.split('/')[0] == "video":
audio_only = False
filename, start, end, type = get_file_start_end_type(input_value, source_type, start, end, audio_only)
if filename == "" or filename is None:
return ""
if type != "audio" and type != "video":
return filename
try:
file_reader = AudioReader(filename, ctx=cpu(0), mono=False)
duration = float(file_reader.duration())
@@ -76,13 +82,23 @@ def organize_input_data_to_audio(input_value, input_type, start, end, dvm_config
print("New Duration of the Media file: " + str(new_duration))
# TODO if already in a working format and time is 0 0, dont convert
print("Converting from " + str(start_time) + " until " + str(end_time))
# for now, we cut and convert all files to mp3
final_filename = '.\\outputs\\audio.mp3'
print(final_filename)
fs, x = ffmpegio.audio.read(filename, ss=start_time, to=end_time, sample_fmt='dbl', ac=1)
ffmpegio.audio.write(final_filename, fs, x, overwrite=True)
return final_filename
if process:
# for now we cut and convert all files to mp3
file = r'processed.' + str(media_format.split('/')[1])
final_filename = os.path.abspath(os.curdir + r'/outputs/' + file)
if media_format.split('/')[0] == "audio":
print("Converting Audio from " + str(start_time) + " until " + str(end_time))
fs, x = ffmpegio.audio.read(filename, ss=start_time, to=end_time, sample_fmt='dbl', ac=1)
ffmpegio.audio.write(final_filename, fs, x, overwrite=True)
elif media_format.split('/')[0] == "video":
print("Converting Video from " + str(start_time) + " until " + str(end_time))
ffmpegio.transcode(filename, final_filename, overwrite=True, show_log=True)
print(final_filename)
return final_filename
else:
return filename
def check_nip94_event_for_media(evt, input_value, input_type):
@@ -112,15 +128,13 @@ def convert_media_length(start: float, end: float, duration: float):
return start_time, end_time, dur
def get_file_start_end_type(url, source_type, start, end) -> (str, str):
def get_file_start_end_type(url, source_type, start, end, audio_only=True) -> (str, str):
# Overcast
if source_type == "overcast":
name, start, end = get_overcast(url, start, end)
return name, start, end, "audio"
# Youtube
elif source_type == "youtube":
audio_only = True
name, start, end = get_youtube(url, start, end, audio_only)
return name, start, end, "audio"
@@ -180,7 +194,7 @@ def check_source_type(url):
def get_overcast(input_value, start, end):
filename = '.\\outputs\\' + ".originalaudio.mp3"
filename = os.path.abspath(os.curdir + r'/outputs/originalaudio.mp3')
print("Found overcast.fm Link.. downloading")
start_time = start
end_time = end
@@ -200,7 +214,7 @@ def get_overcast(input_value, start, end):
def get_TikTok(input_value, start, end):
filepath = '.\\outputs\\'
filepath = os.path.abspath(os.curdir + r'/outputs/')
try:
filename = downloadTikTok(input_value, filepath)
print(filename)
@@ -211,7 +225,7 @@ def get_TikTok(input_value, start, end):
def get_Instagram(input_value, start, end):
filepath = '.\\outputs\\'
filepath = os.path.abspath(os.curdir + r'/outputs/')
try:
filename = downloadInstagram(input_value, filepath)
print(filename)
@@ -222,11 +236,10 @@ def get_Instagram(input_value, start, end):
def get_Twitter(input_value, start, end):
filepath = '.\\outputs\\'
filepath = os.path.abspath(os.curdir) + r'/outputs/'
cleanlink = str(input_value).replace("twitter.com", "x.com")
try:
filename = downloadTwitter(cleanlink, filepath)
print(filename)
except Exception as e:
print(e)
return "", start, end
@@ -267,39 +280,40 @@ def get_media_link(url) -> (str, str):
if content_type == 'audio/x-wav' or str(url).lower().endswith(".wav"):
ext = "wav"
file_type = "audio"
with open('.\\outputs\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
elif content_type == 'audio/mpeg' or str(url).lower().endswith(".mp3"):
ext = "mp3"
file_type = "audio"
with open('.\\outputs\\file.' + '\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
elif content_type == 'audio/ogg' or str(url).lower().endswith(".ogg"):
ext = "ogg"
file_type = "audio"
with open('.\\outputs\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
elif content_type == 'video/mp4' or str(url).lower().endswith(".mp4"):
ext = "mp4"
file_type = "video"
with open('.\\outputs\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
elif content_type == 'video/avi' or str(url).lower().endswith(".avi"):
ext = "avi"
file_type = "video"
with open('.\\outputs\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
elif content_type == 'video/quicktime' or str(url).lower().endswith(".mov"):
ext = "mov"
file_type = "video"
with open('.\\outputs\\file.' + ext, 'wb') as fd:
with open(os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), 'wb') as fd:
fd.write(req.content)
return '.\\outputs\\file.' + ext, file_type
return os.path.abspath(os.curdir + r'/outputs/' + 'file.' + ext), file_type
else:
print(str(url).lower())
@@ -332,5 +346,5 @@ def downloadInstagram(videourl, path):
def downloadYouTube(link, path, audioonly=True):
from utils.scrapper.media_scrapper import YouTubeDownload
result = YouTubeDownload(link, path, audio_only=True)
result = YouTubeDownload(link, path, audio_only=audioonly)
return result

View File

@@ -14,6 +14,14 @@ class NIP89Config:
CONTENT: str = ""
def nip89_create_d_tag(name, pubkey, image):
import hashlib
m = hashlib.md5()
m.update(str(name + image + pubkey).encode("utf-8"))
d_tag = m.hexdigest()[0:16]
return d_tag
def nip89_announce_tasks(dvm_config, client):
k_tag = Tag.parse(["k", str(dvm_config.NIP89.kind)])
d_tag = Tag.parse(["d", dvm_config.NIP89.dtag])

View File

@@ -47,7 +47,9 @@ def send_event(event: Event, client: Client, dvm_config) -> EventId:
for tag in event.tags():
if tag.as_vec()[0] == 'relays':
relays = tag.as_vec()[1].split(',')
for index, param in enumerate(tag.as_vec()):
if index != 0:
relays.append(tag.as_vec()[index])
for relay in relays:
if relay not in dvm_config.RELAY_LIST: