verbose stuff

This commit is contained in:
callebtc
2023-02-01 15:48:23 +01:00
parent f9e1a53609
commit fa802d7862
3 changed files with 21 additions and 10 deletions

View File

@@ -20,7 +20,6 @@ async def dm():
)
client = NostrClient(privatekey_hex=pk)
await asyncio.sleep(1)
filters = {
"since": int(
@@ -61,7 +60,6 @@ async def post():
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}")
sender_client = NostrClient(privatekey_hex=pk)
await asyncio.sleep(1)
to_pubk_hex = (
input(
@@ -99,7 +97,7 @@ async def post():
# write a DM and receive DMs
# asyncio.run(dm())
asyncio.run(dm())
# make a post and subscribe to posts
asyncio.run(post())
# asyncio.run(post())

View File

@@ -21,9 +21,11 @@ from . import cbc
class NostrClient:
relays = [
"wss://relay.snort.social",
"wss://nostr-pub.wellorder.net",
"wss://nostr.zebedee.cloud",
"nostr.mom",
"wss://nostr.mom",
# "wss://wss://lnbits.link/nostrrelay/client"
# "wss://no.str.cr",
] # ["wss://nostr.oxtr.dev"] # ["wss://relay.nostr.info"] "wss://nostr-pub.wellorder.net" "ws://91.237.88.218:2700", "wss://nostrrr.bublina.eu.org", ""wss://nostr-relay.freeberty.net"", , "wss://nostr.oxtr.dev", "wss://relay.nostr.info", "wss://nostr-pub.wellorder.net" , "wss://relayer.fiatjaf.com", "wss://nodestr.fmt.wiz.biz/", "wss://no.str.cr"
relay_manager = RelayManager()
@@ -72,8 +74,6 @@ class NostrClient:
request = [ClientMessageType.REQUEST, subscription_id]
request.extend(filters.to_json_array())
message = json.dumps(request)
# print("Subscribing to events:")
# print(message)
self.relay_manager.publish_message(message)
while True:
@@ -98,8 +98,6 @@ class NostrClient:
)
event.sign(self.private_key.hex())
event_message = json.dumps([ClientMessageType.EVENT, event.to_json_object()])
time.sleep(1)
self.relay_manager.publish_message(event_message)
def get_dm(self, sender_publickey: PublicKey, callback_func=None, filter_kwargs={}):

View File

@@ -45,13 +45,17 @@ class Relay:
on_message=self._on_message,
on_error=self._on_error,
on_close=self._on_close,
on_ping=self._on_ping,
on_pong=self._on_pong,
)
def connect(self, ssl_options: dict = {}):
self.ssl_options = ssl_options
self.ws.run_forever(sslopt=self.ssl_options, ping_interval=2)
print(self.url, "🟢")
self.ws.run_forever(sslopt=self.ssl_options)
def close(self):
print(self.url, "🔴")
self.ws.close()
def check_reconnect(self):
@@ -111,8 +115,11 @@ class Relay:
if self._is_valid_message(message):
self.num_received_events += 1
self.message_pool.add_message(message, self.url)
else:
print(self.url, "invalid message", message)
def _on_error(self, class_obj, error):
print(self.url, "🚫", error)
self.connected = False
self.error_counter += 1
if self.error_threshold and self.error_counter > self.error_threshold:
@@ -120,6 +127,14 @@ class Relay:
else:
self.check_reconnect()
def _on_ping(self, class_obj, message):
print(self.url, "ping", message)
return
def _on_pong(self, class_obj, message):
print(self.url, "pong", message)
return
def _is_valid_message(self, message: str) -> bool:
message = message.strip("\n")
if not message or message[0] != "[" or message[-1] != "]":