use database directly

This commit is contained in:
Believethehype 2024-06-06 13:55:47 +02:00
parent c0d77808e7
commit 2ae742b691
10 changed files with 85 additions and 87 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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.")

View File

@ -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):

View File

@ -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",
]

View File

@ -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:

View File

@ -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,