From f3a6a7caf0e05668731a3e43a975c878391fc0cd Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:06:08 +0200 Subject: [PATCH] update sdk --- nostr_dvm/dvm.py | 1 - nostr_dvm/subscription.py | 6 +- nostr_dvm/tasks/advanced_search.py | 4 +- ...t_discovery_currently_popular_followers.py | 4 +- ...ent_discovery_currently_popular_gallery.py | 5 +- ...ntent_discovery_currently_popular_mostr.py | 45 +-------------- ...iscovery_currently_popular_nonfollowers.py | 4 +- ...ntent_discovery_latest_one_per_follower.py | 6 +- nostr_dvm/tasks/discovery_bot_farms.py | 5 -- nostr_dvm/tasks/discovery_censor_wot.py | 6 +- nostr_dvm/tasks/discovery_inactive_follows.py | 6 +- nostr_dvm/tasks/discovery_nonfollowers.py | 6 +- .../discovery_trending_notes_gleasonator.py | 4 +- nostr_dvm/tasks/search_users.py | 4 -- nostr_dvm/tasks/translation_libretranslate.py | 2 +- nostr_dvm/utils/database_utils.py | 4 +- nostr_dvm/utils/definitions.py | 6 +- nostr_dvm/utils/nip88_utils.py | 10 ++-- nostr_dvm/utils/nip89_utils.py | 8 +-- nostr_dvm/utils/nostr_utils.py | 57 ++++++++++++------- nostr_dvm/utils/nwc_tools.py | 48 ---------------- nostr_dvm/utils/scrapper/media_scrapper.py | 6 +- nostr_dvm/utils/wot_utils.py | 6 +- setup.py | 4 +- tests/ditto.py | 3 +- tests/generic_dvm_autotopic_feed.py | 47 +++++++++++---- tests/gui/nicegui/nostrAI_search_client.py | 6 +- tests/test_events.py | 5 +- tests/wot.py | 4 +- 29 files changed, 137 insertions(+), 185 deletions(-) diff --git a/nostr_dvm/dvm.py b/nostr_dvm/dvm.py index e33479e..516f52f 100644 --- a/nostr_dvm/dvm.py +++ b/nostr_dvm/dvm.py @@ -850,7 +850,6 @@ class DVM: return - # await self.client.handle_notifications(NotificationHandler) asyncio.create_task(self.client.handle_notifications(NotificationHandler())) while True: diff --git a/nostr_dvm/subscription.py b/nostr_dvm/subscription.py index 624a227..b74e0a4 100644 --- a/nostr_dvm/subscription.py +++ b/nostr_dvm/subscription.py @@ -10,7 +10,7 @@ from nostr_sdk import (Keys, Client, Timestamp, Filter, nip04_decrypt, HandleNot Options, Tag, Event, nip04_encrypt, NostrSigner, EventId, Nip19Event, nip44_decrypt, Kind) from nostr_dvm.utils.database_utils import fetch_user_metadata -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig from nostr_dvm.utils.nip88_utils import nip88_has_active_subscription from nostr_dvm.utils.nip89_utils import NIP89Config @@ -248,7 +248,7 @@ class Subscription: subscriptionfilter = Filter().kind(EventDefinitions.KIND_NIP88_SUBSCRIBE_EVENT).author( PublicKey.parse(subscriber)).limit(1) - evts = await self.client.get_events_of([subscriptionfilter], timedelta(seconds=3)) + evts = await self.client.get_events_of([subscriptionfilter], relay_timeout) if len(evts) > 0: event7001id = evts[0].id().to_hex() print(evts[0].as_json()) @@ -285,7 +285,7 @@ class Subscription: if tier_dtag == "" or len(zaps) == 0: tierfilter = Filter().id(EventId.parse(subscription_event_id)) - evts = await self.client.get_events_of([tierfilter], timedelta(seconds=3)) + evts = await self.client.get_events_of([tierfilter], relay_timeout) if len(evts) > 0: for tag in evts[0].tags(): if tag.as_vec()[0] == "d": diff --git a/nostr_dvm/tasks/advanced_search.py b/nostr_dvm/tasks/advanced_search.py index 685b75d..54be0ba 100644 --- a/nostr_dvm/tasks/advanced_search.py +++ b/nostr_dvm/tasks/advanced_search.py @@ -5,7 +5,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -132,7 +132,7 @@ class AdvancedSearch(DVMTaskInterface): notes_filter = Filter().kind(Kind(1)).authors(userkeys).search(options["search"]).since( search_since).until(search_until).limit(options["max_results"]) - events = await cli.get_events_of([notes_filter], timedelta(seconds=5)) + events = await cli.get_events_of([notes_filter], relay_timeout) result_list = [] if len(events) > 0: diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py index aec9da3..96c5d53 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils import definitions from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config, check_and_set_d_tag_nip88, check_and_set_tiereventid_nip88 from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag, create_amount_tag @@ -113,7 +113,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): user = PublicKey.parse(options["user"]) followers_filter = Filter().author(user).kinds([Kind(3)]) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + followers = await cli.get_events_of([followers_filter], relay_timeout) #print(followers) # Negentropy reconciliation diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_gallery.py b/nostr_dvm/tasks/content_discovery_currently_popular_gallery.py index 858b688..8f23c97 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_gallery.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_gallery.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils import definitions from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config, check_and_set_d_tag_nip88, check_and_set_tiereventid_nip88 from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag, create_amount_tag @@ -172,7 +172,7 @@ class DicoverContentCurrentlyPopularGallery(DVMTaskInterface): filter2 = Filter().ids(ids) - events = await cli.get_events_of([filter2], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + events = await cli.get_events_of([filter2], relay_timeout) print(len(events)) @@ -278,6 +278,7 @@ class DicoverContentCurrentlyPopularGallery(DVMTaskInterface): except Exception as e: print(e) + # We build an example here that we can call by either calling this file directly from the main directory, # or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the # playground or elsewhere diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_mostr.py b/nostr_dvm/tasks/content_discovery_currently_popular_mostr.py index 1e991d8..fb7cec2 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_mostr.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_mostr.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils import definitions from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout_long from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config, check_and_set_d_tag_nip88, check_and_set_tiereventid_nip88 from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag, create_amount_tag @@ -126,50 +126,9 @@ class DicoverContentCurrentlyPopularMostr(DVMTaskInterface): if self.dvm_config.LOGLEVEL.value >= LogLevel.DEBUG.value: print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} - profilestorequest = [] - #for event in events: - # filt = Filter().kinds([EventDefinitions.KIND_PROFILE]).pubkey(event.author()) - # profiles = await database.query([filt]) - # # If the event is not in the DB, fetch it. I will be in the DB afterwards - # if len(profiles) == 0 and event.author() not in profilestorequest: - # profilestorequest.append(event.author()) - - - #if len(profilestorequest) > 0: - # print("requesting " + str(len(profilestorequest)) + " profiles") - # - # for relay in self.dvm_config.RECONCILE_DB_RELAY_LIST: - # await cli.add_relay(relay) - # - # - # await cli.connect() - # - # - # chunks = [profilestorequest[x:x + 200] for x in range(0, len(profilestorequest), 200)] - # index = 1 - # for entry in chunks: - # print("Iteration " + str(index)) - # index += 1 - # filt = Filter().kinds([EventDefinitions.KIND_PROFILE]).authors(entry) - # evts = await cli.get_events_of([filt], None) - # if len(evts) > 0: - # print(len(evts)) - # else: - # print("Couldn't find profiles") - for event in events: - #filt = Filter().kinds([EventDefinitions.KIND_PROFILE]).author(event.author()) - #profiles = await cli.database().query([filt]) - # If the event is not in the DB, fetch it. I will be in the DB afterwards - #if len(profiles) == 0: - # print("No profile found") - # continue - #else: - # profile: Event = profiles[0] - - #if "@mostr.pub" in profile.content() or "@momostr.pink" in profile.content(): if event.created_at().as_secs() > timestamp_since: filt = Filter().kinds( [EventDefinitions.KIND_ZAP, EventDefinitions.KIND_REPOST, @@ -258,7 +217,7 @@ class DicoverContentCurrentlyPopularMostr(DVMTaskInterface): # RECONCOILE NOT POSSIBLE ON THESE RELAYS SO WE FETCH AB BUNCH (will be stored in db) try: - events = await cli.get_events_of([filter1, filter2, filter3], timedelta(20)) + events = await cli.get_events_of([filter1, filter2, filter3], relay_timeout_long) except Exception as e: print(e) # Do not delete profiles diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_nonfollowers.py b/nostr_dvm/tasks/content_discovery_currently_popular_nonfollowers.py index efc1f97..c52b369 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_nonfollowers.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_nonfollowers.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils import definitions from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config, check_and_set_d_tag_nip88, check_and_set_tiereventid_nip88 from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag, create_amount_tag @@ -153,7 +153,7 @@ class DicoverContentCurrentlyPopularNonFollowers(DVMTaskInterface): await cli.connect() user = PublicKey.parse(options["user"]) followers_filter = Filter().author(user).kinds([Kind(3)]) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + followers = await cli.get_events_of([followers_filter], relay_timeout) if len(followers) > 0: newest = 0 best_entry = followers[0] diff --git a/nostr_dvm/tasks/content_discovery_latest_one_per_follower.py b/nostr_dvm/tasks/content_discovery_latest_one_per_follower.py index d96fbe3..2b54178 100644 --- a/nostr_dvm/tasks/content_discovery_latest_one_per_follower.py +++ b/nostr_dvm/tasks/content_discovery_latest_one_per_follower.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout_long, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -92,7 +92,7 @@ class Discoverlatestperfollower(DVMTaskInterface): step = 20 followers_filter = Filter().author(PublicKey.parse(options["user"])).kind(Kind(3)) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=5)) + followers = await cli.get_events_of([followers_filter], relay_timeout) if len(followers) > 0: result_list = [] @@ -140,7 +140,7 @@ class Discoverlatestperfollower(DVMTaskInterface): filter1 = (Filter().author(PublicKey.from_hex(users[i])).kind(Kind(1)) .limit(1)) filters.append(filter1) - event_from_authors = await cli.get_events_of(filters, timedelta(seconds=10)) + event_from_authors = await cli.get_events_of(filters, relay_timeout_long) for author in event_from_authors: if instance.dic[author.author().to_hex()] is None: instance.dic[author.author().to_hex()] = author diff --git a/nostr_dvm/tasks/discovery_bot_farms.py b/nostr_dvm/tasks/discovery_bot_farms.py index e9ec17e..9de1e15 100644 --- a/nostr_dvm/tasks/discovery_bot_farms.py +++ b/nostr_dvm/tasks/discovery_bot_farms.py @@ -90,11 +90,6 @@ class DiscoveryBotFarms(DVMTaskInterface): filter1 = Filter().kind(Kind(0)) events = await cli.database().query([filter1]) - # for event in events: - # print(event.as_json()) - - # events = cli.get_events_of([notes_filter], timedelta(seconds=5)) - result_list = [] print("Events: " + str(len(events))) diff --git a/nostr_dvm/tasks/discovery_censor_wot.py b/nostr_dvm/tasks/discovery_censor_wot.py index 1c8df34..53af3b0 100644 --- a/nostr_dvm/tasks/discovery_censor_wot.py +++ b/nostr_dvm/tasks/discovery_censor_wot.py @@ -8,7 +8,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -97,7 +97,7 @@ class DiscoverReports(DVMTaskInterface): # if we don't add users, e.g. by a wot, we check all our followers. if len(pubkeys) == 0: followers_filter = Filter().author(PublicKey.parse(options["sender"])).kind(Kind(3)) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=5)) + followers = await cli.get_events_of([followers_filter],relay_timeout) if len(followers) > 0: result_list = [] @@ -118,7 +118,7 @@ class DiscoverReports(DVMTaskInterface): options["since_days"]) # TODO make this an option, 180 days for now since = Timestamp.from_secs(ago) kind1984_filter = Filter().authors(pubkeys).kind(Kind(1984)).since(since) - reports = await cli.get_events_of([kind1984_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + reports = await cli.get_events_of([kind1984_filter], relay_timeout) bad_actors = [] ns.dic = {} diff --git a/nostr_dvm/tasks/discovery_inactive_follows.py b/nostr_dvm/tasks/discovery_inactive_follows.py index ff562c1..8ada462 100644 --- a/nostr_dvm/tasks/discovery_inactive_follows.py +++ b/nostr_dvm/tasks/discovery_inactive_follows.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout_long, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -92,7 +92,7 @@ class DiscoverInactiveFollows(DVMTaskInterface): step = 20 followers_filter = Filter().author(PublicKey.parse(options["user"])).kind(Kind(3)) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=5)) + followers = await cli.get_events_of([followers_filter], relay_timeout) if len(followers) > 0: result_list = [] @@ -140,7 +140,7 @@ class DiscoverInactiveFollows(DVMTaskInterface): for i in range(i, i + st): filter1 = Filter().author(PublicKey.from_hex(users[i])).since(notactivesince).limit(1) filters.append(filter1) - event_from_authors = await cli.get_events_of(filters, timedelta(seconds=10)) + event_from_authors = await cli.get_events_of(filters, relay_timeout_long) for author in event_from_authors: instance.dic[author.author().to_hex()] = "True" print(str(i) + "/" + str(len(users))) diff --git a/nostr_dvm/tasks/discovery_nonfollowers.py b/nostr_dvm/tasks/discovery_nonfollowers.py index c5f5aed..242af3b 100644 --- a/nostr_dvm/tasks/discovery_nonfollowers.py +++ b/nostr_dvm/tasks/discovery_nonfollowers.py @@ -9,7 +9,7 @@ from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKe from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -81,7 +81,7 @@ class DiscoverNonFollowers(DVMTaskInterface): step = 20 followers_filter = Filter().author(PublicKey.from_hex(options["user"])).kind(Kind(3)) - followers = await cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + followers = await cli.get_events_of([followers_filter], relay_timeout) if len(followers) > 0: result_list = [] @@ -118,7 +118,7 @@ class DiscoverNonFollowers(DVMTaskInterface): filters = [] filter1 = Filter().author(PublicKey.from_hex(users[i])).kind(Kind(3)) filters.append(filter1) - followers = await cli.get_events_of(filters, timedelta(seconds=3)) + followers = await cli.get_events_of(filters, relay_timeout) if len(followers) > 0: result_list = [] diff --git a/nostr_dvm/tasks/discovery_trending_notes_gleasonator.py b/nostr_dvm/tasks/discovery_trending_notes_gleasonator.py index 0570b24..8c6d566 100644 --- a/nostr_dvm/tasks/discovery_trending_notes_gleasonator.py +++ b/nostr_dvm/tasks/discovery_trending_notes_gleasonator.py @@ -7,7 +7,7 @@ from nostr_sdk import Tag, Kind, init_logger, LogLevel, Filter, Timestamp, Relay from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout_long from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -85,7 +85,7 @@ class TrendingNotesGleasonator(DVMTaskInterface): authors = [PublicKey.parse("db0e60d10b9555a39050c258d460c5c461f6d18f467aa9f62de1a728b8a891a4")] notes_filter = Filter().authors(authors).kind(Kind(1985)).custom_tag(SingleLetterTag.lowercase(Alphabet.L), ltags) - events = await cli.get_events_of([notes_filter], timedelta(seconds=10)) + events = await cli.get_events_of([notes_filter], relay_timeout_long) result_list = [] if len(events) > 0: diff --git a/nostr_dvm/tasks/search_users.py b/nostr_dvm/tasks/search_users.py index c8a7848..4da1500 100644 --- a/nostr_dvm/tasks/search_users.py +++ b/nostr_dvm/tasks/search_users.py @@ -96,10 +96,6 @@ class SearchUser(DVMTaskInterface): filter1 = Filter().kind(Kind(0)) events = await cli.database().query([filter1]) - # for event in events: - # print(event.as_json()) - - # events = cli.get_events_of([notes_filter], timedelta(seconds=5)) result_list = [] print("Events: " + str(len(events))) diff --git a/nostr_dvm/tasks/translation_libretranslate.py b/nostr_dvm/tasks/translation_libretranslate.py index a97308d..f43ecd4 100644 --- a/nostr_dvm/tasks/translation_libretranslate.py +++ b/nostr_dvm/tasks/translation_libretranslate.py @@ -79,7 +79,7 @@ class TranslationLibre(DVMTaskInterface): options = self.set_options(request_form) request = { "q": options["text"], - "source": "auto", + "relay_timeout": "auto", "target": options["language"] } if options["libre_api_key"] != "": diff --git a/nostr_dvm/utils/database_utils.py b/nostr_dvm/utils/database_utils.py index 143cb06..f4bf0a0 100644 --- a/nostr_dvm/utils/database_utils.py +++ b/nostr_dvm/utils/database_utils.py @@ -8,6 +8,8 @@ from datetime import timedelta from logging import Filter from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Filter, Kind + +from nostr_dvm.utils.definitions import relay_timeout from nostr_dvm.utils.nostr_utils import send_event @@ -254,7 +256,7 @@ async def fetch_user_metadata(npub, client): pk = PublicKey.parse(npub) print(f"\nGetting profile metadata for {pk.to_bech32()}...") profile_filter = Filter().kind(Kind(0)).author(pk).limit(1) - events = await client.get_events_of([profile_filter], timedelta(seconds=1)) + events = await client.get_events_of([profile_filter], relay_timeout) if len(events) > 0: latest_entry = events[0] latest_time = 0 diff --git a/nostr_dvm/utils/definitions.py b/nostr_dvm/utils/definitions.py index 0accb30..3d3e541 100644 --- a/nostr_dvm/utils/definitions.py +++ b/nostr_dvm/utils/definitions.py @@ -1,7 +1,8 @@ import os from dataclasses import dataclass +from datetime import timedelta -from nostr_sdk import Event, Kind +from nostr_sdk import Event, Kind, EventSource class EventDefinitions: @@ -105,3 +106,6 @@ class InvoiceToWatch: is_paid: bool expires: int + +relay_timeout = EventSource.relays(timedelta(seconds=5)) +relay_timeout_long = EventSource.relays(timedelta(seconds=10)) \ No newline at end of file diff --git a/nostr_dvm/utils/nip88_utils.py b/nostr_dvm/utils/nip88_utils.py index 9dbf049..1453641 100644 --- a/nostr_dvm/utils/nip88_utils.py +++ b/nostr_dvm/utils/nip88_utils.py @@ -8,7 +8,7 @@ from nostr_sdk import Filter, Tag, Keys, EventBuilder, Client, EventId, PublicKe Alphabet, Kind from nostr_dvm.utils import definitions -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.nostr_utils import send_event @@ -37,7 +37,7 @@ def nip88_create_d_tag(name, pubkey, image): async def fetch_nip88_parameters_for_deletion(keys, eventid, client, dvmconfig): idfilter = Filter().id(EventId.from_hex(eventid)).limit(1) - nip88events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT)) + nip88events = await client.get_events_of([idfilter], relay_timeout) d_tag = "" if len(nip88events) == 0: print("Event not found. Potentially gone.") @@ -60,7 +60,7 @@ async def fetch_nip88_parameters_for_deletion(keys, eventid, client, dvmconfig): async def fetch_nip88_event(keys, eventid, client, dvmconfig): idfilter = Filter().id(EventId.parse(eventid)).limit(1) - nip88events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT)) + nip88events = await client.get_events_of([idfilter], relay_timeout) d_tag = "" if len(nip88events) == 0: print("Event not found. Potentially gone.") @@ -99,7 +99,7 @@ async def nip88_has_active_subscription(user: PublicKey, tiereventdtag, client: subscriptionfilter = Filter().kind(definitions.EventDefinitions.KIND_NIP88_PAYMENT_RECIPE).pubkey( PublicKey.parse(receiver_public_key_hex)).custom_tag(SingleLetterTag.uppercase(Alphabet.P), [user.to_hex()]).limit(1) - evts = await client.get_events_of([subscriptionfilter], timedelta(seconds=3)) + evts = await client.get_events_of([subscriptionfilter], relay_timeout) if len(evts) > 0: print(evts[0].as_json()) matchesdtag = False @@ -120,7 +120,7 @@ async def nip88_has_active_subscription(user: PublicKey, tiereventdtag, client: cancel_filter = Filter().kind(EventDefinitions.KIND_NIP88_STOP_SUBSCRIPTION_EVENT).author( user).pubkey(PublicKey.parse(receiver_public_key_hex)).event( EventId.parse(subscription_status["subscriptionId"])).limit(1) - cancel_events = await client.get_events_of([cancel_filter], timedelta(seconds=3)) + cancel_events = await client.get_events_of([cancel_filter], relay_timeout) if len(cancel_events) > 0: if cancel_events[0].created_at().as_secs() > evts[0].created_at().as_secs(): subscription_status["expires"] = True diff --git a/nostr_dvm/utils/nip89_utils.py b/nostr_dvm/utils/nip89_utils.py index 4d44890..832ff95 100644 --- a/nostr_dvm/utils/nip89_utils.py +++ b/nostr_dvm/utils/nip89_utils.py @@ -6,7 +6,7 @@ from pathlib import Path import dotenv from nostr_sdk import Tag, Keys, EventBuilder, Filter, Alphabet, PublicKey, Client, EventId, SingleLetterTag, Kind -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout from nostr_dvm.utils.nostr_utils import send_event from nostr_dvm.utils.print import bcolors @@ -39,7 +39,7 @@ async def nip89_announce_tasks(dvm_config, client): async def fetch_nip89_parameters_for_deletion(keys, eventid, client, dvmconfig, pow=False): idfilter = Filter().id(EventId.from_hex(eventid)).limit(1) - nip89events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT)) + nip89events = await client.get_events_of([idfilter], relay_timeout) d_tag = "" if len(nip89events) == 0: print("Event not found. Potentially gone.") @@ -85,7 +85,7 @@ async def nip89_fetch_all_dvms(client): ktags.append(str(i)) filter = Filter().kind(EventDefinitions.KIND_ANNOUNCEMENT).custom_tag(SingleLetterTag.lowercase(Alphabet.K), ktags) - events = await client.get_events_of([filter], timedelta(seconds=5)) + events = await client.get_events_of([filter], relay_timeout) for event in events: print(event.as_json()) @@ -94,7 +94,7 @@ async def nip89_fetch_events_pubkey(client, pubkey, kind): ktags = [str(kind.as_u64())] nip89filter = (Filter().kind(EventDefinitions.KIND_ANNOUNCEMENT).author(PublicKey.parse(pubkey)). custom_tag(SingleLetterTag.lowercase(Alphabet.K), ktags)) - events = await client.get_events_of([nip89filter], timedelta(seconds=4)) + events = await client.get_events_of([nip89filter], relay_timeout) dvms = {} for event in events: diff --git a/nostr_dvm/utils/nostr_utils.py b/nostr_dvm/utils/nostr_utils.py index 759786a..8ba7efe 100644 --- a/nostr_dvm/utils/nostr_utils.py +++ b/nostr_dvm/utils/nostr_utils.py @@ -6,9 +6,10 @@ from typing import List import dotenv from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, Metadata, Options, \ - Nip19Event, SingleLetterTag, RelayOptions, RelayLimits, SecretKey, NostrSigner + Nip19Event, SingleLetterTag, RelayOptions, RelayLimits, SecretKey, NostrSigner, Connection, ConnectionTarget, \ + EventSource -from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout, relay_timeout_long async def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None: @@ -16,7 +17,7 @@ async def get_event_by_id(event_id: str, client: Client, config=None) -> Event | if len(split) == 3: pk = PublicKey.from_hex(split[1]) id_filter = Filter().author(pk).custom_tag(SingleLetterTag.lowercase(Alphabet.D), [split[2]]) - events = await client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + events = await client.get_events_of([id_filter], relay_timeout) else: if str(event_id).startswith('note'): event_id = EventId.from_bech32(event_id) @@ -31,8 +32,7 @@ async def get_event_by_id(event_id: str, client: Client, config=None) -> Event | event_id = EventId.from_hex(event_id) id_filter = Filter().id(event_id).limit(1) - #events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) - events = await client.get_events_of([id_filter], timedelta(seconds=5)) + events = await client.get_events_of([id_filter], relay_timeout) if len(events) > 0: @@ -43,7 +43,8 @@ async def get_event_by_id(event_id: str, client: Client, config=None) -> Event | return None async def get_events_async(client, filter, timeout): - events = await client.get_events_of([filter], timedelta(seconds=timeout)) + source_l = EventSource.relays(timedelta(seconds=timeout)) + events = await client.get_events_of([filter], source_l) return events @@ -55,8 +56,7 @@ async def get_events_by_ids(event_ids, client: Client, config=None) -> List | No if len(split) == 3: pk = PublicKey.from_hex(split[1]) id_filter = Filter().author(pk).custom_tag(SingleLetterTag.lowercase(Alphabet.D), [split[2]]) - events = await client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) - #events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + events = await client.get_events_of([id_filter], relay_timeout) else: if str(event_id).startswith('note'): event_id = EventId.from_bech32(event_id) @@ -72,9 +72,8 @@ async def get_events_by_ids(event_ids, client: Client, config=None) -> List | No search_ids.append(event_id) id_filter = Filter().ids(search_ids) - events = await client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + events = await client.get_events_of([id_filter], relay_timeout) - #events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) if len(events) > 0: return events else: @@ -84,7 +83,7 @@ async def get_events_by_ids(event_ids, client: Client, config=None) -> List | No async def get_events_by_id(event_ids: list, client: Client, config=None) -> list[Event] | None: id_filter = Filter().ids(event_ids) #events = asyncio.run(get_events_async(client, id_filter, config.RELAY_TIMEOUT)) - events = await client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + events = await client.get_events_of([id_filter], relay_timeout) if len(events) > 0: return events else: @@ -109,9 +108,8 @@ async def get_referenced_event_by_id(event_id, client, dvm_config, kinds) -> Eve job_id_filter = Filter().kinds(kinds).event(event_id).limit(1) else: job_id_filter = Filter().event(event_id).limit(1) - events = await client.get_events_of([job_id_filter], timedelta(seconds=dvm_config.RELAY_TIMEOUT)) - #events = await get_events_async(client, job_id_filter, dvm_config.RELAY_TIMEOUT) - #events = client.get_events_of([job_id_filter], timedelta(seconds=dvm_config.RELAY_TIMEOUT)) + events = await client.get_events_of([job_id_filter], relay_timeout) + if len(events) > 0: return events[0] @@ -127,7 +125,7 @@ async def get_inbox_relays(event_to_send: Event, client: Client, dvm_config): ptags.append(ptag) filter = Filter().kinds([EventDefinitions.KIND_RELAY_ANNOUNCEMENT]).authors(ptags) - events = await client.get_events_of([filter], timedelta(dvm_config.RELAY_TIMEOUT)) + events = await client.get_events_of([filter], relay_timeout) if len(events) == 0: return [] else: @@ -150,8 +148,13 @@ async def get_main_relays(event_to_send: Event, client: Client, dvm_config): ptag = PublicKey.parse(tag.as_vec()[1]) ptags.append(ptag) + if len(await client.relays()) == 0: + for relay in dvm_config.RELAY_LIST: + await client.add_relay(relay) + + await client.connect() filter = Filter().kinds([EventDefinitions.KIND_FOLLOW_LIST]).authors(ptags) - events = await client.get_events_of([filter], timedelta(dvm_config.RELAY_TIMEOUT)) + events = await client.get_events_of([filter], relay_timeout) if len(events) == 0: return [] else: @@ -199,25 +202,35 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId: # 5. Otherwise, we create a new Outbox client with the inbox relays and send the event there relaylimits = RelayLimits.disable() + connection = Connection().embedded_tor().target(ConnectionTarget.ONION) + #connection = Connection().addr("127.0.0.1:9050").target(ConnectionTarget.ONION) opts = ( - Options().wait_for_send(False).send_timeout(timedelta(seconds=dvm_config.RELAY_TIMEOUT)).relay_limits( - relaylimits)) + Options().wait_for_send(False).send_timeout(timedelta(seconds=20)).relay_limits( + relaylimits)).connection(connection).connection_timeout(timedelta(seconds=120)) + + + sk = SecretKey.from_hex(dvm_config.PRIVATE_KEY) keys = Keys.parse(sk.to_hex()) signer = NostrSigner.keys(keys) + client = Client.with_opts(signer, opts) + + + outboxclient = Client.with_opts(signer, opts) print("[" + dvm_config.NIP89.NAME + "] Receiver Inbox relays: " + str(relays)) for relay in relays: - opts = RelayOptions().ping(False) try: - await outboxclient.add_relay_with_opts(relay, opts) + await outboxclient.add_relay(relay) except: print("[" + dvm_config.NIP89.NAME + "] " + relay + " couldn't be added to outbox relays") - +# await outboxclient.connect() try: + print("Connected, sending event") event_id = await outboxclient.send_event(event) + print(event_id.output) except Exception as e: event_id = None print(e) @@ -227,11 +240,13 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId: for relay in relays: await outboxclient.remove_relay(relay) + relays = await get_main_relays(event, client, dvm_config) for relay in relays: opts = RelayOptions().ping(False) await outboxclient.add_relay_with_opts(relay, opts) try: + await outboxclient.connect() event_id = await outboxclient.send_event(event) except Exception as e: # Love yourself then. diff --git a/nostr_dvm/utils/nwc_tools.py b/nostr_dvm/utils/nwc_tools.py index 502a25b..2d1dd53 100644 --- a/nostr_dvm/utils/nwc_tools.py +++ b/nostr_dvm/utils/nwc_tools.py @@ -25,54 +25,6 @@ async def nwc_zap(connectionstr, bolt11, keys, externalrelay=None): event_id = await nwc.pay_invoice(bolt11) print("NWC event: " + event_id) - - - #target_pubkey, relay, secret = parse_connection_str(connectionstr) - #print(target_pubkey) - #print(relay) - #print(secret) - #SecretSK = Keys.parse(secret) - - #content = { - # "method": "pay_invoice", - # "params": { - # "invoice": bolt11 - # } - #} - - #signer = NostrSigner.keys(keys) - #client = Client(signer) - #client.add_relay(relay) - #if externalrelay is not None: - # client.add_relay(externalrelay) - - #client.connect() - - #client_public_key = PublicKey.from_hex(target_pubkey) - #encrypted_content = nip04_encrypt(SecretSK.secret_key(), client_public_key, json.dumps(content)) - - #pTag = Tag.parse(["p", client_public_key.to_hex()]) - - #event = EventBuilder(23194, encrypted_content, - # [pTag]).to_event(keys) - - #ts = Timestamp.now() - #event_id = client.send_event(event) - - - - - - - #nwc_response_filter = Filter().kind(23195).since(ts) - #events = client.get_events_of([nwc_response_filter], timedelta(seconds=5)) - - #if len(events) > 0: - # for evt in events: - # print(evt.as_json()) - #else: - # print("No response found") - return event_id diff --git a/nostr_dvm/utils/scrapper/media_scrapper.py b/nostr_dvm/utils/scrapper/media_scrapper.py index 1f9a8f5..35a1be8 100644 --- a/nostr_dvm/utils/scrapper/media_scrapper.py +++ b/nostr_dvm/utils/scrapper/media_scrapper.py @@ -215,16 +215,16 @@ def OvercastDownload(source_url, target_location): return None def get_url(html_string): - """Find the URL from the