-
@ ever4st
2025-02-01 21:51:44List of Wordpress articles:
- https://alashazam.wordpress.com/2024/07/01/python-nostr/
- https://alashazam.wordpress.com/2024/07/02/python-nostr-2/
- https://alashazam.wordpress.com/2024/07/03/python-nostr-second-program/
- https://alashazam.wordpress.com/2024/07/04/python-nostr-sdk/
- https://alashazam.wordpress.com/2024/07/04/python-nostr-third-program/
- https://alashazam.wordpress.com/2024/07/05/python-nostr-guessing-the-npub/
- https://alashazam.wordpress.com/2024/07/06/python-nostr_sdk-fetching-metadata/
- https://alashazam.wordpress.com/tag/python/
```python
!/usr/bin/env python3
""" Fetches the joined date from a Nostr profile and calculates the days since account creation.
Usage: python fetch_joined_date.py [npub]
Arguments: npub : The public key of the user (in npub format).
Requirements: - nostr_sdk library - Python 3.x
Example: python fetch_joined_date.py npub1... """
import argparse import time import ssl import json import uuid import logging from datetime import datetime 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
Configure logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
RELAY_URL = "wss://relay.damus.io"
def fetch_metadata(npub): """Fetches metadata from a Nostr profile given an npub key.""" pub_key = PublicKey.from_npub(npub) filters = Filters([Filter(authors=[pub_key.hex()], kinds=[EventKind.SET_METADATA])]) subscription_id = uuid.uuid1().hex request = [ClientMessageType.REQUEST, subscription_id, *filters.to_json_array()]
relay_manager = RelayManager() relay_manager.add_relay(RELAY_URL) relay_manager.add_subscription(subscription_id, filters) try: relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) time.sleep(1) # Allow time for connection relay_manager.publish_message(json.dumps(request)) time.sleep(1) # Allow time for response while relay_manager.message_pool.has_events(): event_msg = relay_manager.message_pool.get_event() metadata = json.loads(event_msg.event.content) return metadata except Exception as e: logging.error(f"Error fetching metadata: {e}") finally: relay_manager.close_connections() return None
def fetch_joined_date(npub): """Retrieves the joined date of a Nostr profile and calculates days since creation.""" metadata = fetch_metadata(npub) if not metadata or "created_at" not in metadata: logging.warning("Could not retrieve metadata or 'created_at' field is missing.") return
try: created_at = int(metadata["created_at"]) created_date = datetime.utcfromtimestamp(created_at) days_since_creation = (datetime.utcnow() - created_date).days logging.info(f"Account created on: {created_date.strftime('%Y-%m-%d %H:%M:%S UTC')}") logging.info(f"Days since account creation: {days_since_creation}") except (ValueError, TypeError): logging.error("Invalid 'created_at' timestamp.")
if name == "main": parser = argparse.ArgumentParser(description="Fetch the joined date from a Nostr profile.") parser.add_argument("npub", type=str, help="The public key of the user (in npub format).") args = parser.parse_args()
fetch_joined_date(args.npub)
```