diff --git a/nostr_dvm/bot.py b/nostr_dvm/bot.py index e8590a6..9e28b63 100644 --- a/nostr_dvm/bot.py +++ b/nostr_dvm/bot.py @@ -24,7 +24,22 @@ class Bot: job_list: list # This is a simple list just to keep track which events we created and manage, so we don't pay for other requests - async def init_bot(self, dvm_config, admin_config=None): + + def __init__(self, dvm_config, admin_config=None): + asyncio.run(self.run_bot(dvm_config, admin_config)) + + + # add_sql_table_column(dvm_config.DB) + async def run_bot(self, dvm_config, admin_config): + self.NAME = "Bot" + dvm_config.DB = "db/" + self.NAME + ".db" + self.dvm_config = dvm_config + nip89config = NIP89Config() + nip89config.NAME = self.NAME + self.dvm_config.NIP89 = nip89config + self.admin_config = admin_config + self.keys = Keys.parse(dvm_config.PRIVATE_KEY) + wait_for_send = True skip_disconnected_relays = True opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) @@ -59,22 +74,7 @@ class Bot: create_sql_table(self.dvm_config.DB) await admin_make_database_updates(adminconfig=self.admin_config, dvmconfig=self.dvm_config, client=self.client) - def __init__(self, dvm_config, admin_config=None): - self.NAME = "Bot" - dvm_config.DB = "db/" + self.NAME + ".db" - self.dvm_config = dvm_config - nip89config = NIP89Config() - nip89config.NAME = self.NAME - self.dvm_config.NIP89 = nip89config - self.admin_config = admin_config - self.keys = Keys.parse(dvm_config.PRIVATE_KEY) - asyncio.run(self.init_bot(dvm_config, admin_config)) - asyncio.run(self.run_bot(dvm_config)) - - - # add_sql_table_column(dvm_config.DB) - async def run_bot(self, dvm_config): class NotificationHandler(HandleNotification): client = self.client dvm_config = self.dvm_config diff --git a/nostr_dvm/dvm.py b/nostr_dvm/dvm.py index 6b307ea..58391e9 100644 --- a/nostr_dvm/dvm.py +++ b/nostr_dvm/dvm.py @@ -35,7 +35,13 @@ class DVM: job_list: list jobs_on_hold_list: list - async def init_dvm(self, dvm_config, admin_config=None): + def __init__(self, dvm_config, admin_config=None): + asyncio.run(self.run_dvm(dvm_config, admin_config)) + + async def run_dvm(self, dvm_config, admin_config): + self.dvm_config = dvm_config + self.admin_config = admin_config + self.keys = Keys.parse(dvm_config.PRIVATE_KEY) wait_for_send = False skip_disconnected_relays = True relaylimits = RelayLimits.disable() @@ -67,15 +73,7 @@ class DVM: await admin_make_database_updates(adminconfig=self.admin_config, dvmconfig=self.dvm_config, client=self.client) await self.client.subscribe([dvm_filter, zap_filter], None) - def __init__(self, dvm_config, admin_config=None): - self.dvm_config = dvm_config - self.admin_config = admin_config - self.keys = Keys.parse(dvm_config.PRIVATE_KEY) - asyncio.run(self.init_dvm(dvm_config, admin_config)) - asyncio.run(self.run_dvm(dvm_config)) - - async def run_dvm(self, dvm_config): class NotificationHandler(HandleNotification): client = self.client dvm_config = self.dvm_config diff --git a/nostr_dvm/subscription.py b/nostr_dvm/subscription.py index 27dba05..845c03e 100644 --- a/nostr_dvm/subscription.py +++ b/nostr_dvm/subscription.py @@ -25,7 +25,21 @@ from nostr_dvm.utils.zap_utils import create_bolt11_lud16, zaprequest class Subscription: job_list: list - async def init_subscription(self, dvm_config, admin_config=None): + # This is a simple list just to keep track which events we created and manage, so we don't pay for other requests + def __init__(self, dvm_config, admin_config=None): + asyncio.run(self.run_subscription(dvm_config, admin_config)) + + async def run_subscription(self, dvm_config, admin_config): + + self.NAME = "Subscription Handler" + dvm_config.DB = "db/" + "subscriptions" + ".db" + self.dvm_config = dvm_config + nip89config = NIP89Config() + nip89config.NAME = self.NAME + self.dvm_config.NIP89 = nip89config + self.admin_config = admin_config + self.keys = Keys.parse(dvm_config.PRIVATE_KEY) + wait_for_send = False skip_disconnected_relays = True opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) @@ -64,23 +78,6 @@ class Subscription: await self.client.subscribe([zap_filter, dvm_filter, cancel_subscription_filter], None) create_subscription_sql_table(dvm_config.DB) - - # This is a simple list just to keep track which events we created and manage, so we don't pay for other requests - def __init__(self, dvm_config, admin_config=None): - self.NAME = "Subscription Handler" - dvm_config.DB = "db/" + "subscriptions" + ".db" - self.dvm_config = dvm_config - nip89config = NIP89Config() - nip89config.NAME = self.NAME - self.dvm_config.NIP89 = nip89config - self.admin_config = admin_config - self.keys = Keys.parse(dvm_config.PRIVATE_KEY) - - asyncio.run(self.init_subscription(dvm_config, admin_config)) - asyncio.run(self.run_subscription(dvm_config)) - - async def run_subscription(self, dvm_config): - class NotificationHandler(HandleNotification): client = self.client dvm_config = self.dvm_config diff --git a/nostr_dvm/tasks/content_discovery_currently_popular.py b/nostr_dvm/tasks/content_discovery_currently_popular.py index b849d24..51ac82f 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular.py @@ -109,14 +109,14 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): options = self.set_options(request_form) - 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) + #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 = await NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).signer(signer).opts(opts).build() - await cli.connect() + #cli = ClientBuilder().database(database).signer(signer).opts(opts).build() + #await cli.connect() # Negentropy reconciliation # Query events from database @@ -124,7 +124,7 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): since = Timestamp.from_secs(timestamp_hour_ago) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = await cli.database().query([filter1]) + events = await database.query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} for event in events: @@ -132,11 +132,11 @@ 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 = await cli.database().query([filt]) + reactions = await database.query([filt]) if len(reactions) >= self.min_reactions: ns.finallist[event.id().to_hex()] = len(reactions) if len(ns.finallist) == 0: - await cli.shutdown() + #await cli.shutdown() return self.result result_list = [] @@ -145,7 +145,7 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface): # print(EventId.parse(entry[0]).to_bech32() + "/" + EventId.parse(entry[0]).to_hex() + ": " + str(entry[1])) e_tag = Tag.parse(["e", entry[0]]) result_list.append(e_tag.as_vec()) - await cli.shutdown() + #await cli.shutdown() print("[" + self.dvm_config.NIP89.NAME + "] Filtered " + str( len(result_list)) + " fitting events.") return json.dumps(result_list) 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 dc2d2c6..41cd1e4 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 @@ -110,15 +110,15 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): options = self.set_options(request_form) - 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) + #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 = await NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).signer(signer).opts(opts).build() + #cli = ClientBuilder().database(database).signer(signer).opts(opts).build() - await cli.connect() + #await cli.connect() # Negentropy reconciliation # Query events from database @@ -126,14 +126,14 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): since = Timestamp.from_secs(timestamp_hour_ago) filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = await cli.database().query([filter1]) + events = await 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 = await cli.database().query([filt]) + reactions = await database.query([filt]) invoice_amount = 0 haspreimage = False if len(reactions) >= self.min_reactions: @@ -162,8 +162,7 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Filtered " + str( len(result_list)) + " fitting events.") - await cli.disconnect() - await cli.shutdown() + #await cli.shutdown() return json.dumps(result_list) diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py index 3535d80..00195fd 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_followers.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_followers.py @@ -160,7 +160,7 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface): # print(EventId.parse(entry[0]).to_bech32() + "/" + EventId.parse(entry[0]).to_hex() + ": " + str(entry[1])) e_tag = Tag.parse(["e", entry[0]]) result_list.append(e_tag.as_vec()) - await cli.connect() + #await cli.connect() await cli.shutdown() print("[" + self.dvm_config.NIP89.NAME + "] Filtered " + str( len(result_list)) + " fitting events.") diff --git a/nostr_dvm/tasks/content_discovery_currently_popular_topic.py b/nostr_dvm/tasks/content_discovery_currently_popular_topic.py index 4d83d0c..a4dc12b 100644 --- a/nostr_dvm/tasks/content_discovery_currently_popular_topic.py +++ b/nostr_dvm/tasks/content_discovery_currently_popular_topic.py @@ -131,15 +131,15 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): options = self.set_options(request_form) - 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) + #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 = await NostrDatabase.sqlite(self.db_name) - cli = ClientBuilder().database(database).signer(signer).opts(opts).build() + #cli = ClientBuilder().database(database).signer(signer).opts(opts).build() - await cli.connect() + #await cli.connect() # Negentropy reconciliation # Query events from database @@ -148,7 +148,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): filter1 = Filter().kind(definitions.EventDefinitions.KIND_NOTE).since(since) - events = await cli.database().query([filter1]) + events = await database.query([filter1]) print("[" + self.dvm_config.NIP89.NAME + "] Considering " + str(len(events)) + " Events") ns.finallist = {} @@ -161,7 +161,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 = await cli.database().query([filt]) + reactions = await database.query([filt]) if len(reactions) >= self.min_reactions: ns.finallist[event.id().to_hex()] = len(reactions) @@ -174,7 +174,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface): print("[" + self.dvm_config.NIP89.NAME + "] Filtered " + str( len(result_list)) + " fitting events.") - await cli.shutdown() + #await cli.shutdown() return json.dumps(result_list) async def schedule(self, dvm_config): diff --git a/nostr_dvm/utils/dvmconfig.py b/nostr_dvm/utils/dvmconfig.py index 2a19af2..b4286f7 100644 --- a/nostr_dvm/utils/dvmconfig.py +++ b/nostr_dvm/utils/dvmconfig.py @@ -31,7 +31,10 @@ class DVMConfig: "wss://nostr-pub.semisol.dev", "wss://mostr.pub", "wss://minds.com", "wss://yabu.me", "wss://relay.yozora.world", "wss://filter.nostr.wine/?global=all", "wss://eden.nostr.land", "wss://relay.orangepill.ovh", "wss://nostr.jcloud.es", "wss://af.purplerelay.com", "wss://za.purplerelay.com", - "wss://relay.nostrich.land", "wss://relay.nostrplebs.com" "wss://relay.nostrich.land", + "wss://relay.nostrich.land", "wss://relay.nostrplebs.com", "wss://relay.nostrich.land", + "wss://rss.nos.social", "wss://atlas.nostr.land", "wss://puravida.nostr.land", "wss://nostr.inosta.cc", + "wss://relay.orangepill.dev", "wss://no.str.cr", "wss://nostr.milou.lol", "wss://relay.nostr.com.au", + "wss://puravida.nostr.land", "wss://atlas.nostr.land", "wss://nostr-pub.wellorder.net", "wss://eelay.current.fyi", ] diff --git a/nostr_dvm/utils/nostr_utils.py b/nostr_dvm/utils/nostr_utils.py index c535252..67d756e 100644 --- a/nostr_dvm/utils/nostr_utils.py +++ b/nostr_dvm/utils/nostr_utils.py @@ -178,11 +178,10 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId: keys = Keys.parse(sk.to_hex()) signer = NostrSigner.keys(keys) outboxclient = Client.with_opts(signer, opts) - print("[" + dvm_config.NIP89.NAME + "] Receiver Inbox relays: " + str(relays)) for relay in relays: - opts = RelayOptions().ping(False) + opts = RelayOptions().ping(True) try: await outboxclient.add_relay_with_opts(relay, opts) except: diff --git a/tests/discovery.py b/tests/discovery.py index 20590cf..f2a8577 100644 --- a/tests/discovery.py +++ b/tests/discovery.py @@ -29,23 +29,25 @@ global_update_rate = 120 # set this high on first sync so db can fully sync use_logger = True AVOID_PAID_OUTBOX_RELAY_LIST = ["wss://nostrelay.yeghro.site", "wss://nostr.wine", "wss://filter.nostr.wine" - "wss://nostr21.com", - "wss://nostr.bitcoiner.social", "wss://nostr.orangepill.dev", - "wss://relay.lnpay.me", "wss://relay.snort.social", "wss://relay.minds.com/nostr/v1/ws", - "wss://nostr-pub.semisol.dev", "wss://mostr.pub", "wss://minds.com", - "wss://yabu.me", "wss://relay.yozora.world", "wss://filter.nostr.wine/?global=all", - "wss://eden.nostr.land", - "wss://relay.orangepill.ovh", "wss://nostr.jcloud.es", "wss://af.purplerelay.com", - "wss://za.purplerelay.com", - "wss://relay.nostrich.land", "wss://relay.nostrplebs.com" "wss://relay.nostrich.land", + "wss://nostr21.com", "wss://nostr.bitcoiner.social", "wss://nostr.orangepill.dev", + "wss://relay.lnpay.me", "wss://relay.snort.social", "wss://relay.minds.com/nostr/v1/ws", + "wss://nostr-pub.semisol.dev", "wss://mostr.pub", "wss://minds.com", + "wss://yabu.me", "wss://relay.yozora.world", "wss://filter.nostr.wine/?global=all", "wss://eden.nostr.land", + "wss://relay.orangepill.ovh", "wss://nostr.jcloud.es", "wss://af.purplerelay.com", "wss://za.purplerelay.com", + "wss://relay.nostrich.land", "wss://relay.nostrplebs.com", "wss://relay.nostrich.land", + "wss://rss.nos.social", "wss://atlas.nostr.land", "wss://puravida.nostr.land", "wss://nostr.inosta.cc", + "wss://relay.orangepill.dev", "wss://no.str.cr", "wss://nostr.milou.lol", "wss://relay.nostr.com.au", + "wss://puravida.nostr.land", "wss://atlas.nostr.land", "wss://nostr-pub.wellorder.net", "wss://eelay.current.fyi", + "wss://nostr.thesamecat.io", "wss://nostr.plebchain.org", "wss://relay.noswhere.com" - ] + + ] #git_hash = NostrLibrary().git_hash_version() #print("GitHash " + git_hash) if use_logger: - init_logger(LogLevel.INFO) + init_logger(LogLevel.DEBUG) def build_db_scheduler(name, identifier, admin_config, options, image, description, update_rate=600, cost=0,