diff --git a/nostr_dvm/tasks/content_discovery_currently_popular.py b/nostr_dvm/tasks/content_discovery_currently_popular.py index 79ad904..fc26c18 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular.py @@ -58,6 +58,20 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): if self.logger: init_logger(LogLevel.DEBUG) + opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) + sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) + keys = Keys.parse(sk.to_hex()) + signer = NostrSigner.keys(keys) + database = NostrDatabase.sqlite(self.db_name) + self.client = ClientBuilder().signer(signer).database(database).opts(opts).build() + + self.client.add_relay("wss://relay.damus.io") + self.client.add_relay("wss://nostr.oxtr.dev") + self.client.add_relay("wss://nostr21.com") + + self.client.connect() + + if self.dvm_config.UPDATE_DATABASE: self.sync_db() @@ -111,16 +125,11 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): options = self.set_options(request_form) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).build() - - # Negentropy reconciliation - # Query events from database timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since since = Timestamp.from_secs(timestamp_hour_ago) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = cli.database().query([filter1]) + events = self.client.database().query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} @@ -129,7 +138,7 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): filt = Filter().kinds([definitions.EventDefinitions.KIND_ZAP, definitions.EventDefinitions.KIND_REPOST, definitions.EventDefinitions.KIND_REACTION, definitions.EventDefinitions.KIND_NOTE]).event(event.id()).since(since) - reactions = cli.database().query([filt]) + reactions = self.client.database().query([filt]) if len(reactions) >= self.min_reactions: ns.finallist[event.id().to_hex()] = len(reactions) @@ -168,18 +177,6 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): return 1 def sync_db(self): - opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) - sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) - keys = Keys.parse(sk.to_hex()) - signer = NostrSigner.keys(keys) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().signer(signer).database(database).opts(opts).build() - - cli.add_relay("wss://relay.damus.io") - cli.add_relay("wss://nostr.oxtr.dev") - cli.add_relay("wss://nostr21.com") - - cli.connect() timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since lasthour = Timestamp.from_secs(timestamp_hour_ago) @@ -191,9 +188,9 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Syncing notes of the last " + str( self.db_since) + " seconds.. this might take a while..") dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) - cli.reconcile(filter1, dbopts) + self.client.reconcile(filter1, dbopts) filter_delete = Filter().until(Timestamp.from_secs(Timestamp.now().as_secs() - self.db_since)) - database.delete(filter_delete) # Clear old events so db doesn't get too full. + self.client.database().delete(filter_delete) # Clear old events so db doesn't get too full. print( "[" + self.dvm_config.NIP89.NAME + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_by_top_zaps.py b/nostr_dvm/tasks/content_discovery_currently_popular_by_top_zaps.py index 6275f8b..0841d5d 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_by_top_zaps.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_by_top_zaps.py @@ -58,6 +58,20 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): if self.logger: init_logger(LogLevel.DEBUG) + + opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) + sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) + keys = Keys.parse(sk.to_hex()) + signer = NostrSigner.keys(keys) + database = NostrDatabase.sqlite(self.db_name) + self.client = ClientBuilder().signer(signer).database(database).opts(opts).build() + + self.client.add_relay("wss://relay.damus.io") + self.client.add_relay("wss://nostr.oxtr.dev") + self.client.add_relay("wss://nostr21.com") + + self.client.connect() + if self.dvm_config.UPDATE_DATABASE: self.sync_db() @@ -110,8 +124,6 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): options = self.set_options(request_form) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).build() # Negentropy reconciliation # Query events from database @@ -119,14 +131,14 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): since = Timestamp.from_secs(timestamp_hour_ago) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = cli.database().query([filter1]) + events = self.client.database().query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} for event in events: if event.created_at().as_secs() > timestamp_hour_ago: filt = Filter().kinds([definitions.EventDefinitions.KIND_ZAP]).event(event.id()).since(since) - reactions = cli.database().query([filt]) + reactions = self.client.database().query([filt]) invoice_amount = 0 haspreimage = False if len(reactions) >= self.min_reactions: @@ -180,18 +192,6 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): return 1 def sync_db(self): - opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) - sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) - keys = Keys.parse(sk.to_hex()) - signer = NostrSigner.keys(keys) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().signer(signer).database(database).opts(opts).build() - - cli.add_relay("wss://relay.damus.io") - cli.add_relay("wss://nostr.oxtr.dev") - cli.add_relay("wss://nostr21.com") - - cli.connect() timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since lasthour = Timestamp.from_secs(timestamp_hour_ago) @@ -203,9 +203,9 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Syncing notes of the last " + str( self.db_since) + " seconds.. this might take a while..") dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) - cli.reconcile(filter1, dbopts) + self.client.reconcile(filter1, dbopts) filter_delete = Filter().until(Timestamp.from_secs(Timestamp.now().as_secs() - self.db_since)) - database.delete(filter_delete) # Clear old events so db doesn't get too full. + self.client.database().delete(filter_delete) # Clear old events so db doesn't get too full. print( "[" + self.dvm_config.NIP89.NAME + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py index 48a18d6..eca6a11 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py @@ -51,6 +51,22 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): if self.logger: init_logger(LogLevel.DEBUG) + opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) + sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) + keys = Keys.parse(sk.to_hex()) + signer = NostrSigner.keys(keys) + database = NostrDatabase.sqlite(self.db_name) + self.client = ClientBuilder().signer(signer).database(database).opts(opts).build() + + self.client.add_relay("wss://relay.damus.io") + self.client.add_relay("wss://nostr.oxtr.dev") + self.client.add_relay("wss://nostr21.com") + + ropts = RelayOptions().ping(False) + self.client.add_relay_with_opts("wss://nostr.band", ropts) + + self.client.connect() + if self.dvm_config.UPDATE_DATABASE: self.sync_db() @@ -100,23 +116,10 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): opts = ( Options().wait_for_send(True).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)).relay_limits( relaylimits)) - sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) - keys = Keys.parse(sk.to_hex()) - signer = NostrSigner.keys(keys) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).signer(signer).opts(opts).build() - cli.add_relay("wss://relay.damus.io") - cli.add_relay("wss://nos.lol") - cli.add_relay("wss://nostr.mom") - - ropts = RelayOptions().ping(False) - cli.add_relay_with_opts("wss://nostr.band", ropts) - - cli.connect() user = PublicKey.parse(options["user"]) followers_filter = Filter().author(user).kinds([Kind(3)]) - followers = cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + followers = self.client.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) # Negentropy reconciliation # Query events from database @@ -141,7 +144,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): followings.append(following) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).authors(followings).since(since) - events = cli.database().query([filter1]) + events = self.client.database().query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} @@ -150,7 +153,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): filt = Filter().kinds( [EventDefinitions.KIND_ZAP, EventDefinitions.KIND_REACTION, EventDefinitions.KIND_REPOST, EventDefinitions.KIND_NOTE]).event(event.id()).since(since) - reactions = cli.database().query([filt]) + reactions = self.client.database().query([filt]) if len(reactions) >= self.min_reactions: ns.finallist[event.id().to_hex()] = len(reactions) @@ -187,18 +190,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): return 1 def sync_db(self): - opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) - sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) - keys = Keys.parse(sk.to_hex()) - signer = NostrSigner.keys(keys) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().signer(signer).database(database).opts(opts).build() - cli.add_relay("wss://relay.damus.io") - cli.add_relay("wss://nostr.oxtr.dev") - cli.add_relay("wss://nostr21.com") - - cli.connect() timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since lasthour = Timestamp.from_secs(timestamp_hour_ago) @@ -210,9 +202,9 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Syncing notes of the last " + str( self.db_since) + " seconds.. this might take a while..") dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) - cli.reconcile(filter1, dbopts) + self.client.reconcile(filter1, dbopts) filter_delete = Filter().until(Timestamp.from_secs(Timestamp.now().as_secs() - self.db_since)) - database.delete(filter_delete) # Clear old events so db doesn't get too full. + self.client.database().delete(filter_delete) # Clear old events so db doesn't get too full. print( "[" + self.dvm_config.NIP89.NAME + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_topic.py b/nostr_dvm/tasks/content_discovery_currently_popular_topic.py index acac34d..60aaf24 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_topic.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_topic.py @@ -73,6 +73,18 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): if self.logger: init_logger(LogLevel.DEBUG) + opts = (Options().wait_for_send(True).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) + sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) + keys = Keys.parse(sk.to_hex()) + signer = NostrSigner.keys(keys) + database = NostrDatabase.sqlite(self.db_name) + self.client = ClientBuilder().signer(signer).database(database).opts(opts).build() + + self.client.add_relay("wss://relay.damus.io") + self.client.add_relay("wss://nostr.oxtr.dev") + self.client.add_relay("wss://nostr21.com") + self.client.connect() + if self.dvm_config.UPDATE_DATABASE: self.sync_db() if not self.personalized: @@ -135,14 +147,13 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): options = self.set_options(request_form) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).build() + timestamp_since = Timestamp.now().as_secs() - self.db_since since = Timestamp.from_secs(timestamp_since) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = cli.database().query([filter1]) + events = self.client.database().query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.final_list = {} @@ -156,7 +167,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): [definitions.EventDefinitions.KIND_ZAP, definitions.EventDefinitions.KIND_REACTION, definitions.EventDefinitions.KIND_REPOST, definitions.EventDefinitions.KIND_NOTE]).event(event.id()).since(since) - reactions = cli.database().query([filt]) + reactions = self.client.database().query([filt]) if len(reactions) >= self.min_reactions: ns.final_list[event.id().to_hex()] = len(reactions) @@ -185,17 +196,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): return 1 def sync_db(self): - opts = (Options().wait_for_send(True).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) - sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) - keys = Keys.parse(sk.to_hex()) - signer = NostrSigner.keys(keys) - database = NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().signer(signer).database(database).opts(opts).build() - cli.add_relay("wss://relay.damus.io") - cli.add_relay("wss://nostr.oxtr.dev") - cli.add_relay("wss://nostr21.com") - cli.connect() timestamp_since = Timestamp.now().as_secs() - self.db_since since = Timestamp.from_secs(timestamp_since) @@ -208,9 +209,9 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Syncing notes of the last " + str( self.db_since) + " seconds.. this might take a while..") dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) - cli.reconcile(filter1, dbopts) + self.client.reconcile(filter1, dbopts) filter_delete = Filter().until(Timestamp.from_secs(Timestamp.now().as_secs() - self.db_since)) - database.delete(filter_delete) # Clear old events so db doesn't get too full. + self.client.database().delete(filter_delete) # Clear old events so db doesn't get too full. print( "[" + self.dvm_config.NIP89.NAME + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") diff --git a/setup.py b/setup.py index 9f072cc..f72867f 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = '0.5.2' +VERSION = '0.5.3' 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/discovery.py b/tests/discovery.py index 8af0dbf..a7c04b6 100644 --- a/tests/discovery.py +++ b/tests/discovery.py @@ -4,7 +4,7 @@ import threading from pathlib import Path import dotenv -from nostr_sdk import init_logger, LogLevel, Keys +from nostr_sdk import init_logger, LogLevel, Keys, NostrLibrary from nostr_dvm.subscription import Subscription from nostr_dvm.tasks.content_discovery_currently_popular import DicoverContentCurrentlyPopular @@ -18,9 +18,15 @@ from nostr_dvm.utils.nip89_utils import create_amount_tag, NIP89Config, check_an from nostr_dvm.utils.nostr_utils import check_and_set_private_key from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys -global_update_rate = 180 # set this high on first sync so db can fully sync before another process trys to. +rebbroadcast_NIP89 = False # Announce NIP89 on startup +global_update_rate = 180 # set this high on first sync so db can fully sync before another process trys to. +use_logger = False +#git_hash = NostrLibrary().git_hash_version() +#print("GitHash " + git_hash) +if use_logger: + init_logger(LogLevel.DEBUG) def build_example_nostrband(name, identifier, admin_config, image, about, custom_processing_msg): dvm_config: DVMConfig = build_default_config(identifier) dvm_config.USE_OWN_VENV = False @@ -207,11 +213,8 @@ def build_example_top_zapped(name, identifier, admin_config, options, image, cos def playground(): - rebbroadcast_NIP89 = True # Announce NIP89 on startup - use_logger = False - if use_logger: - init_logger(LogLevel.INFO) + # Popular NOSTR.band admin_config_trending_nostr_band = AdminConfig()