Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get profile information from pubkey #82

Open
steveboi123 opened this issue Feb 26, 2023 · 5 comments
Open

How to get profile information from pubkey #82

steveboi123 opened this issue Feb 26, 2023 · 5 comments

Comments

@steveboi123
Copy link

If I have a pubkey, how can I query to find out about their profile information like bio and lightning address?

Thank you

@lndrkzts
Copy link

You can do that using the README.md example named "Receive events from relays", but you need to change the filter to EventKind.SET_METADATA.

I really don't know if it's the most appropriate way but it worked for me. I'm just starting to explore this library.

@daiangan
Copy link

You can do that using the README.md example named "Receive events from relays", but you need to change the filter to EventKind.SET_METADATA.

I really don't know if it's the most appropriate way but it worked for me. I'm just starting to explore this library.

@lndrkzts can you please share your piece of code?

@daiangan
Copy link

After some tests, this is the final code to get profile updates using an npub:

import time
import uuid
from nostr.event import EventKind
from nostr.relay_manager import RelayManager
from nostr.key import PublicKey
from nostr.filter import Filter, Filters


pub_key = PublicKey.from_npub('npubxxxxxxxxxx')
filters = Filters([Filter(authors=[pub_key.hex()], kinds=[EventKind.SET_METADATA])])
subscription_id = uuid.uuid1().hex

relay_manager = RelayManager()
relay_manager.add_relay("wss://relay.snort.social")
relay_manager.add_relay("wss://relay.damus.io")
relay_manager.add_subscription_on_all_relays(subscription_id, filters)

time.sleep(1)

while relay_manager.message_pool.has_events():
  event_msg = relay_manager.message_pool.get_event()
  print(event_msg.event.content)
  
relay_manager.close_all_relay_connections()

Keep in mind that you need to add at least one of the relays where the update was done to have some results.

@PlebeiusGaragicus
Copy link

@daiangan is this code sample using the relay-threads branch?? I can't get that branch to work.

File "/Users/micah/Downloads/NOSTR/nospy/nospy/commands/publish.py", line 5, in <module>
    from nostr.relay_manager import RelayManager
  File "/Users/micah/Downloads/NOSTR/nospy/venv/lib/python3.11/site-packages/nostr/relay_manager.py", line 11, in <module>
    from .relay import Relay, RelayPolicy, RelayProxyConnectionConfig
  File "/Users/micah/Downloads/NOSTR/nospy/venv/lib/python3.11/site-packages/nostr/relay.py", line 32, in <module>
    @dataclass
     ^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py", line 1220, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py", line 1210, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py", line 815, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'nostr.relay.RelayPolicy'> for field policy is not allowed: use default_factory

Also, I can't get your code to work using the main branch and changing function names of relay_manager.add_subscription_on_all_relays, etc. It runs, but doesn't get any events for the pubkeys I give it.

Stumped...

@PlebeiusGaragicus
Copy link

I FIGURED IT OUT!!

import time
import uuid
import ssl
import json
from nostr.event import EventKind
from nostr.relay_manager import RelayManager
from nostr.key import PublicKey
from nostr.filter import Filter, Filters
from nostr.message_type import ClientMessageType

def get_info():
    pub_key = PublicKey.from_npub('npub1xegedgkkjf24pl4d76cdwhufacng5hapzjnrtgms3pyhlvmyqj9suym08k')
    filters = Filters([Filter(authors=[pub_key.hex()], kinds=[EventKind.SET_METADATA])]) # TEXT_NOTE
    subscription_id = uuid.uuid1().hex

    request = [ClientMessageType.REQUEST, subscription_id]
    request.extend(filters.to_json_array())

    relay_manager = RelayManager()
    # relay_manager.add_relay("wss://nostr-pub.wellorder.net")
    # relay_manager.add_relay("wss://relay.snort.social")
    relay_manager.add_relay("wss://relay.damus.io")
    relay_manager.add_subscription(subscription_id, filters)
    relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE})
    time.sleep(1.25) # allow the connections to open

    message = json.dumps(request)
    relay_manager.publish_message(message)
    time.sleep(1) # allow the messages to send

    while relay_manager.message_pool.has_events():
        event_msg = relay_manager.message_pool.get_event()

        data = json.loads(event_msg.event.content)
        pretty_json = json.dumps(data, indent=4)
        print(pretty_json)
    
    relay_manager.close_connections()

if __name__ == "__main__":
    get_info()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants