mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-12-03 13:26:32 +01:00
more adaptation to new sdk (async functions)
This commit is contained in:
@@ -13,7 +13,7 @@ from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key
|
|||||||
from nostr_dvm.utils.definitions import EventDefinitions
|
from nostr_dvm.utils.definitions import EventDefinitions
|
||||||
|
|
||||||
|
|
||||||
def nostr_client_test_tts(prompt):
|
async def nostr_client_test_tts(prompt):
|
||||||
keys = Keys.parse(check_and_set_private_key("test_client"))
|
keys = Keys.parse(check_and_set_private_key("test_client"))
|
||||||
|
|
||||||
iTag = Tag.parse(["i", prompt, "text"])
|
iTag = Tag.parse(["i", prompt, "text"])
|
||||||
@@ -34,10 +34,10 @@ def nostr_client_test_tts(prompt):
|
|||||||
signer = NostrSigner.keys(keys)
|
signer = NostrSigner.keys(keys)
|
||||||
client = Client(signer)
|
client = Client(signer)
|
||||||
for relay in relay_list:
|
for relay in relay_list:
|
||||||
client.add_relay(relay)
|
await client.add_relay(relay)
|
||||||
client.connect()
|
await client.connect()
|
||||||
config = DVMConfig
|
config = DVMConfig
|
||||||
send_event(event, client=client, dvm_config=config)
|
await send_event(event, client=client, dvm_config=config)
|
||||||
return event.as_json()
|
return event.as_json()
|
||||||
|
|
||||||
async def nostr_client():
|
async def nostr_client():
|
||||||
@@ -61,13 +61,13 @@ async def nostr_client():
|
|||||||
await client.subscribe([dm_zap_filter, dvm_filter])
|
await client.subscribe([dm_zap_filter, dvm_filter])
|
||||||
|
|
||||||
|
|
||||||
nostr_client_test_tts("Hello, this is a test. Mic check one, two.")
|
await nostr_client_test_tts("Hello, this is a test. Mic check one, two.")
|
||||||
print("Sending Job Request")
|
print("Sending Job Request")
|
||||||
|
|
||||||
|
|
||||||
#nostr_client_test_image_private("a beautiful ostrich watching the sunset")
|
#nostr_client_test_image_private("a beautiful ostrich watching the sunset")
|
||||||
class NotificationHandler(HandleNotification):
|
class NotificationHandler(HandleNotification):
|
||||||
def handle(self, relay_url, subscription_id, event: Event):
|
async def handle(self, relay_url, subscription_id, event: Event):
|
||||||
print(f"Received new event from {relay_url}: {event.as_json()}")
|
print(f"Received new event from {relay_url}: {event.as_json()}")
|
||||||
if event.kind() == 7000:
|
if event.kind() == 7000:
|
||||||
print("[Nostr Client]: " + event.as_json())
|
print("[Nostr Client]: " + event.as_json())
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ class Subscription:
|
|||||||
await client.add_relay(relay)
|
await client.add_relay(relay)
|
||||||
await client.connect()
|
await client.connect()
|
||||||
recipeid = await client.send_event(event)
|
recipeid = await client.send_event(event)
|
||||||
await client.disconnect()
|
await client.shutdown()
|
||||||
recipe = recipeid.to_hex()
|
recipe = recipeid.to_hex()
|
||||||
return recipe
|
return recipe
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class DiscoverReports(DVMTaskInterface):
|
|||||||
cli = Client.with_opts(signer, opts)
|
cli = Client.with_opts(signer, opts)
|
||||||
# cli.add_relay("wss://relay.nostr.band")
|
# cli.add_relay("wss://relay.nostr.band")
|
||||||
for relay in self.dvm_config.RELAY_LIST:
|
for relay in self.dvm_config.RELAY_LIST:
|
||||||
cli.add_relay(relay)
|
await cli.add_relay(relay)
|
||||||
# add nostr band, too.
|
# add nostr band, too.
|
||||||
ropts = RelayOptions().ping(False)
|
ropts = RelayOptions().ping(False)
|
||||||
await cli.add_relay_with_opts("wss://nostr.band", ropts)
|
await cli.add_relay_with_opts("wss://nostr.band", ropts)
|
||||||
|
|||||||
@@ -140,11 +140,11 @@ class DiscoverInactiveFollows(DVMTaskInterface):
|
|||||||
for i in range(i, i + st):
|
for i in range(i, i + st):
|
||||||
filter1 = Filter().author(PublicKey.from_hex(users[i])).since(notactivesince).limit(1)
|
filter1 = Filter().author(PublicKey.from_hex(users[i])).since(notactivesince).limit(1)
|
||||||
filters.append(filter1)
|
filters.append(filter1)
|
||||||
event_from_authors = cli.get_events_of(filters, timedelta(seconds=10))
|
event_from_authors = await cli.get_events_of(filters, timedelta(seconds=10))
|
||||||
for author in event_from_authors:
|
for author in event_from_authors:
|
||||||
instance.dic[author.author().to_hex()] = "True"
|
instance.dic[author.author().to_hex()] = "True"
|
||||||
print(str(i) + "/" + str(len(users)))
|
print(str(i) + "/" + str(len(users)))
|
||||||
cli.disconnect()
|
await cli.shutdown()
|
||||||
|
|
||||||
threads = []
|
threads = []
|
||||||
begin = 0
|
begin = 0
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class DiscoverNonFollowers(DVMTaskInterface):
|
|||||||
step = 20
|
step = 20
|
||||||
|
|
||||||
followers_filter = Filter().author(PublicKey.from_hex(options["user"])).kind(Kind(3))
|
followers_filter = Filter().author(PublicKey.from_hex(options["user"])).kind(Kind(3))
|
||||||
followers = cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
|
followers = await cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
|
||||||
|
|
||||||
if len(followers) > 0:
|
if len(followers) > 0:
|
||||||
result_list = []
|
result_list = []
|
||||||
@@ -111,7 +111,7 @@ class DiscoverNonFollowers(DVMTaskInterface):
|
|||||||
cli = Client.with_opts(signer, opts)
|
cli = Client.with_opts(signer, opts)
|
||||||
for relay in self.dvm_config.RELAY_LIST:
|
for relay in self.dvm_config.RELAY_LIST:
|
||||||
await cli.add_relay(relay)
|
await cli.add_relay(relay)
|
||||||
cli.connect()
|
await cli.connect()
|
||||||
|
|
||||||
for i in range(i, i + st):
|
for i in range(i, i + st):
|
||||||
filters = []
|
filters = []
|
||||||
@@ -141,7 +141,7 @@ class DiscoverNonFollowers(DVMTaskInterface):
|
|||||||
print("DIDNT FIND " + best_entry.author().to_nostr_uri())
|
print("DIDNT FIND " + best_entry.author().to_nostr_uri())
|
||||||
|
|
||||||
print(str(i) + "/" + str(len(users)))
|
print(str(i) + "/" + str(len(users)))
|
||||||
cli.disconnect()
|
await cli.shutdown()
|
||||||
|
|
||||||
threads = []
|
threads = []
|
||||||
begin = 0
|
begin = 0
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ class SearchUser(DVMTaskInterface):
|
|||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
await cli.disconnect()
|
await cli.shutdown()
|
||||||
return json.dumps(result_list)
|
return json.dumps(result_list)
|
||||||
|
|
||||||
def post_process(self, result, event):
|
def post_process(self, result, event):
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -1,6 +1,6 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
VERSION = '0.6.0'
|
VERSION = '0.6.1'
|
||||||
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
|
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')
|
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')
|
||||||
|
|
||||||
|
|||||||
20
tests/db.py
20
tests/db.py
@@ -6,9 +6,9 @@ print(keys.public_key().to_bech32())
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reconcile_db():
|
async def reconcile_db():
|
||||||
# Create/open SQLite database
|
# Create/open SQLite database
|
||||||
database = NostrDatabase.sqlite("nostr.db")
|
database = await NostrDatabase.sqlite("nostr.db")
|
||||||
|
|
||||||
# NOT AVAILABLE ON WINDOWS AT THE MOMENT!
|
# NOT AVAILABLE ON WINDOWS AT THE MOMENT!
|
||||||
# Create/open nostrdb database
|
# Create/open nostrdb database
|
||||||
@@ -16,21 +16,21 @@ def reconcile_db():
|
|||||||
|
|
||||||
client = ClientBuilder().database(database).build()
|
client = ClientBuilder().database(database).build()
|
||||||
|
|
||||||
client.add_relay("wss://relay.damus.io")
|
await client.add_relay("wss://relay.damus.io")
|
||||||
client.add_relay("wss://atl.purplerelay.com")
|
await client.add_relay("wss://atl.purplerelay.com")
|
||||||
client.connect()
|
await client.connect()
|
||||||
|
|
||||||
# Negentropy reconciliation
|
# Negentropy reconciliation
|
||||||
f = Filter().author(keys.public_key())
|
f = Filter().author(keys.public_key())
|
||||||
opts = NegentropyOptions()
|
opts = NegentropyOptions()
|
||||||
client.reconcile(f, opts)
|
await client.reconcile(f, opts)
|
||||||
|
|
||||||
do_some_work()
|
await do_some_work()
|
||||||
|
|
||||||
def do_some_work():
|
async def do_some_work():
|
||||||
database = NostrDatabase.sqlite("nostr.db")
|
database = await NostrDatabase.sqlite("nostr.db")
|
||||||
f = Filter().author(keys.public_key()).limit(10)
|
f = Filter().author(keys.public_key()).limit(10)
|
||||||
events = database.query([f])
|
events = await database.query([f])
|
||||||
|
|
||||||
for event in events:
|
for event in events:
|
||||||
print(event.as_json())
|
print(event.as_json())
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ 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.nostr_utils import check_and_set_private_key
|
||||||
from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys
|
from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys
|
||||||
|
|
||||||
rebroadcast_NIP89 = False # Announce NIP89 on startup
|
rebroadcast_NIP89 = True # Announce NIP89 on startup
|
||||||
rebroadcast_NIP65_Relay_List = False
|
rebroadcast_NIP65_Relay_List = False
|
||||||
update_profile = False
|
update_profile = False
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -9,22 +10,45 @@ from nostr_dvm.utils import definitions, dvmconfig
|
|||||||
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
|
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
|
||||||
|
|
||||||
|
|
||||||
relay_list = dvmconfig.DVMConfig.RELAY_LIST
|
|
||||||
keys = Keys.parse(check_and_set_private_key("test_client"))
|
|
||||||
wait_for_send = False
|
|
||||||
skip_disconnected_relays = True
|
|
||||||
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=5))
|
|
||||||
.skip_disconnected_relays(skip_disconnected_relays))
|
|
||||||
|
|
||||||
signer = NostrSigner.keys(keys)
|
async def test():
|
||||||
client = Client.with_opts(signer, opts)
|
|
||||||
|
|
||||||
for relay in relay_list:
|
relay_list = dvmconfig.DVMConfig.RELAY_LIST
|
||||||
client.add_relay(relay)
|
keys = Keys.parse(check_and_set_private_key("test_client"))
|
||||||
client.connect()
|
wait_for_send = False
|
||||||
|
skip_disconnected_relays = True
|
||||||
|
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=5))
|
||||||
|
.skip_disconnected_relays(skip_disconnected_relays))
|
||||||
|
|
||||||
|
signer = NostrSigner.keys(keys)
|
||||||
|
client = Client.with_opts(signer, opts)
|
||||||
|
|
||||||
|
for relay in relay_list:
|
||||||
|
await client.add_relay(relay)
|
||||||
|
client.connect()
|
||||||
|
|
||||||
|
await test_referred_events(client,"c70fbd4dbaad22c427d4359981d3bdddd3971ed1a38227ca2f8e5e760f58103c",
|
||||||
|
definitions.EventDefinitions.ANY_RESULT)
|
||||||
|
|
||||||
|
# shows kind 7000 reaction but not kind 6300 result (d05e7ae9271fe2d8968cccb67c01e3458dbafa4a415e306d49b22729b088c8a1)
|
||||||
|
await test_referred_events(client, "5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e",
|
||||||
|
definitions.EventDefinitions.ANY_RESULT)
|
||||||
|
|
||||||
|
bech32evnt = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_bech32()
|
||||||
|
print(bech32evnt)
|
||||||
|
|
||||||
|
test = Nip19Event.from_bech32(
|
||||||
|
"nevent1qqsrjcpejsrlt3u7dy42y6rc97svrq9ver08xy4jr2ll55ynq3sxafcppamhxue69uhkummnw3ezumt0d5pzpmnqx2pla0zvxxcfjqeeysy29ll3mtmf4s3yff0y45r7egau080vqvzqqqqqqyu4q839")
|
||||||
|
print(test.event_id().to_hex())
|
||||||
|
|
||||||
|
nostruri = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_nostr_uri()
|
||||||
|
print(nostruri)
|
||||||
|
|
||||||
|
await test_search_by_user_since_days(client,
|
||||||
|
PublicKey.from_bech32("npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8"), 60, "Bitcoin")
|
||||||
|
|
||||||
|
|
||||||
def test_referred_events(event_id, kinds=None):
|
async def test_referred_events(client, event_id, kinds=None):
|
||||||
|
|
||||||
if kinds is None:
|
if kinds is None:
|
||||||
kinds = []
|
kinds = []
|
||||||
@@ -34,7 +58,7 @@ def test_referred_events(event_id, kinds=None):
|
|||||||
else:
|
else:
|
||||||
job_id_filter = Filter().event(EventId.from_hex(event_id))
|
job_id_filter = Filter().event(EventId.from_hex(event_id))
|
||||||
|
|
||||||
events = client.get_events_of([job_id_filter], timedelta(seconds=5))
|
events = await client.get_events_of([job_id_filter], timedelta(seconds=5))
|
||||||
|
|
||||||
if len(events) > 0:
|
if len(events) > 0:
|
||||||
for event in events:
|
for event in events:
|
||||||
@@ -45,13 +69,13 @@ def test_referred_events(event_id, kinds=None):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def test_search_by_user_since_days(pubkey, days, prompt):
|
async def test_search_by_user_since_days(client, pubkey, days, prompt):
|
||||||
since_seconds = int(days) * 24 * 60 * 60
|
since_seconds = int(days) * 24 * 60 * 60
|
||||||
dif = Timestamp.now().as_secs() - since_seconds
|
dif = Timestamp.now().as_secs() - since_seconds
|
||||||
since = Timestamp.from_secs(dif)
|
since = Timestamp.from_secs(dif)
|
||||||
|
|
||||||
filterts = Filter().search(prompt).author(pubkey).kinds([1]).since(since)
|
filterts = Filter().search(prompt).author(pubkey).kinds([1]).since(since)
|
||||||
events = client.get_events_of([filterts], timedelta(seconds=5))
|
events = await client.get_events_of([filterts], timedelta(seconds=5))
|
||||||
|
|
||||||
if len(events) > 0:
|
if len(events) > 0:
|
||||||
for event in events:
|
for event in events:
|
||||||
@@ -71,21 +95,7 @@ if __name__ == '__main__':
|
|||||||
else:
|
else:
|
||||||
raise FileNotFoundError(f'.env file not found at {env_path} ')
|
raise FileNotFoundError(f'.env file not found at {env_path} ')
|
||||||
|
|
||||||
|
asyncio.run(test())
|
||||||
|
|
||||||
# works
|
# works
|
||||||
test_referred_events("c70fbd4dbaad22c427d4359981d3bdddd3971ed1a38227ca2f8e5e760f58103c", definitions.EventDefinitions.ANY_RESULT)
|
|
||||||
|
|
||||||
#shows kind 7000 reaction but not kind 6300 result (d05e7ae9271fe2d8968cccb67c01e3458dbafa4a415e306d49b22729b088c8a1)
|
|
||||||
test_referred_events("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e", definitions.EventDefinitions.ANY_RESULT)
|
|
||||||
|
|
||||||
bech32evnt = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_bech32()
|
|
||||||
print(bech32evnt)
|
|
||||||
|
|
||||||
test = Nip19Event.from_bech32("nevent1qqsrjcpejsrlt3u7dy42y6rc97svrq9ver08xy4jr2ll55ynq3sxafcppamhxue69uhkummnw3ezumt0d5pzpmnqx2pla0zvxxcfjqeeysy29ll3mtmf4s3yff0y45r7egau080vqvzqqqqqqyu4q839")
|
|
||||||
print(test.event_id().to_hex())
|
|
||||||
|
|
||||||
nostruri = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_nostr_uri()
|
|
||||||
print(nostruri)
|
|
||||||
|
|
||||||
test_search_by_user_since_days(PublicKey.from_bech32("npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8"), 60, "Bitcoin")
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user