mirror of
https://github.com/aljazceru/python-nostr.git
synced 2025-12-19 15:24:19 +01:00
verbose stuff
This commit is contained in:
6
main.py
6
main.py
@@ -20,7 +20,6 @@ async def dm():
|
|||||||
)
|
)
|
||||||
|
|
||||||
client = NostrClient(privatekey_hex=pk)
|
client = NostrClient(privatekey_hex=pk)
|
||||||
await asyncio.sleep(1)
|
|
||||||
|
|
||||||
filters = {
|
filters = {
|
||||||
"since": int(
|
"since": int(
|
||||||
@@ -61,7 +60,6 @@ async def post():
|
|||||||
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}")
|
print(f"From {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}")
|
||||||
|
|
||||||
sender_client = NostrClient(privatekey_hex=pk)
|
sender_client = NostrClient(privatekey_hex=pk)
|
||||||
await asyncio.sleep(1)
|
|
||||||
|
|
||||||
to_pubk_hex = (
|
to_pubk_hex = (
|
||||||
input(
|
input(
|
||||||
@@ -99,7 +97,7 @@ async def post():
|
|||||||
|
|
||||||
|
|
||||||
# write a DM and receive DMs
|
# write a DM and receive DMs
|
||||||
# asyncio.run(dm())
|
asyncio.run(dm())
|
||||||
|
|
||||||
# make a post and subscribe to posts
|
# make a post and subscribe to posts
|
||||||
asyncio.run(post())
|
# asyncio.run(post())
|
||||||
|
|||||||
@@ -21,9 +21,11 @@ from . import cbc
|
|||||||
|
|
||||||
class NostrClient:
|
class NostrClient:
|
||||||
relays = [
|
relays = [
|
||||||
|
"wss://relay.snort.social",
|
||||||
"wss://nostr-pub.wellorder.net",
|
"wss://nostr-pub.wellorder.net",
|
||||||
"wss://nostr.zebedee.cloud",
|
"wss://nostr.zebedee.cloud",
|
||||||
"nostr.mom",
|
"wss://nostr.mom",
|
||||||
|
# "wss://wss://lnbits.link/nostrrelay/client"
|
||||||
# "wss://no.str.cr",
|
# "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"
|
] # ["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()
|
relay_manager = RelayManager()
|
||||||
@@ -72,8 +74,6 @@ class NostrClient:
|
|||||||
request = [ClientMessageType.REQUEST, subscription_id]
|
request = [ClientMessageType.REQUEST, subscription_id]
|
||||||
request.extend(filters.to_json_array())
|
request.extend(filters.to_json_array())
|
||||||
message = json.dumps(request)
|
message = json.dumps(request)
|
||||||
# print("Subscribing to events:")
|
|
||||||
# print(message)
|
|
||||||
self.relay_manager.publish_message(message)
|
self.relay_manager.publish_message(message)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -98,8 +98,6 @@ class NostrClient:
|
|||||||
)
|
)
|
||||||
event.sign(self.private_key.hex())
|
event.sign(self.private_key.hex())
|
||||||
event_message = json.dumps([ClientMessageType.EVENT, event.to_json_object()])
|
event_message = json.dumps([ClientMessageType.EVENT, event.to_json_object()])
|
||||||
|
|
||||||
time.sleep(1)
|
|
||||||
self.relay_manager.publish_message(event_message)
|
self.relay_manager.publish_message(event_message)
|
||||||
|
|
||||||
def get_dm(self, sender_publickey: PublicKey, callback_func=None, filter_kwargs={}):
|
def get_dm(self, sender_publickey: PublicKey, callback_func=None, filter_kwargs={}):
|
||||||
|
|||||||
@@ -45,13 +45,17 @@ class Relay:
|
|||||||
on_message=self._on_message,
|
on_message=self._on_message,
|
||||||
on_error=self._on_error,
|
on_error=self._on_error,
|
||||||
on_close=self._on_close,
|
on_close=self._on_close,
|
||||||
|
on_ping=self._on_ping,
|
||||||
|
on_pong=self._on_pong,
|
||||||
)
|
)
|
||||||
|
|
||||||
def connect(self, ssl_options: dict = {}):
|
def connect(self, ssl_options: dict = {}):
|
||||||
self.ssl_options = ssl_options
|
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):
|
def close(self):
|
||||||
|
print(self.url, "🔴")
|
||||||
self.ws.close()
|
self.ws.close()
|
||||||
|
|
||||||
def check_reconnect(self):
|
def check_reconnect(self):
|
||||||
@@ -111,8 +115,11 @@ class Relay:
|
|||||||
if self._is_valid_message(message):
|
if self._is_valid_message(message):
|
||||||
self.num_received_events += 1
|
self.num_received_events += 1
|
||||||
self.message_pool.add_message(message, self.url)
|
self.message_pool.add_message(message, self.url)
|
||||||
|
else:
|
||||||
|
print(self.url, "invalid message", message)
|
||||||
|
|
||||||
def _on_error(self, class_obj, error):
|
def _on_error(self, class_obj, error):
|
||||||
|
print(self.url, "🚫", error)
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.error_counter += 1
|
self.error_counter += 1
|
||||||
if self.error_threshold and self.error_counter > self.error_threshold:
|
if self.error_threshold and self.error_counter > self.error_threshold:
|
||||||
@@ -120,6 +127,14 @@ class Relay:
|
|||||||
else:
|
else:
|
||||||
self.check_reconnect()
|
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:
|
def _is_valid_message(self, message: str) -> bool:
|
||||||
message = message.strip("\n")
|
message = message.strip("\n")
|
||||||
if not message or message[0] != "[" or message[-1] != "]":
|
if not message or message[0] != "[" or message[-1] != "]":
|
||||||
|
|||||||
Reference in New Issue
Block a user