nostrdvm/tests/db.py

40 lines
1.1 KiB
Python
Raw Permalink Normal View History

from threading import Thread
from nostr_sdk import Keys, Filter, ClientBuilder, NostrDatabase, NegentropyOptions, init_logger, LogLevel
init_logger(LogLevel.INFO)
keys = Keys.parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")
print(keys.public_key().to_bech32())
def reconcile_db():
# Create/open SQLite database
database = NostrDatabase.sqlite("nostr.db")
# NOT AVAILABLE ON WINDOWS AT THE MOMENT!
# Create/open nostrdb database
# database = NostrDatabase.ndb("ndb")
client = ClientBuilder().database(database).build()
client.add_relay("wss://relay.damus.io")
client.add_relay("wss://atl.purplerelay.com")
client.connect()
# Negentropy reconciliation
f = Filter().author(keys.public_key())
opts = NegentropyOptions()
client.reconcile(f, opts)
do_some_work()
def do_some_work():
database = NostrDatabase.sqlite("nostr.db")
f = Filter().author(keys.public_key()).limit(10)
events = database.query([f])
for event in events:
print(event.as_json())
nostr_dvm_thread = Thread(target=reconcile_db)
nostr_dvm_thread.start()