#!/usr/bin/env python3
import json
import asyncio
from datetime import timedelta
from nostr_sdk import Keys, Client, NostrSigner, Filter, Kind, RelayUrl, SingleLetterTag, Alphabet

RELAY_URL = "ws://143.198.27.163:2929"
KEYS_FILE = "/Users/apayne/.timmy/nostr/agent_keys.json"
GROUP_ID = "b082d1"

with open(KEYS_FILE) as f:
    all_keys = json.load(f)

async def main():
    keys = Keys.parse(all_keys["timmy"]["hex_sec"])
    signer = NostrSigner.keys(keys)
    client = Client(signer)
    await client.add_relay(RelayUrl.parse(RELAY_URL))
    await client.connect()
    await asyncio.sleep(2)

    pub_to_name = {data["hex_pub"]: name for name, data in all_keys.items()}

    for kind_num in [39000, 39001, 39002, 9, 10, 11, 12, 9005]:
        print(f"=== kind {kind_num} for group {GROUP_ID} ===")
        f = Filter().kind(Kind(kind_num)).custom_tag(
            SingleLetterTag.lowercase(Alphabet.H), GROUP_ID
        )
        events = await client.fetch_events(f, timedelta(seconds=10))
        ev_list = events.to_vec()
        print(f"count={len(ev_list)}")
        for ev in ev_list[:20]:
            author = pub_to_name.get(ev.author().to_hex(), ev.author().to_hex()[:12])
            try:
                tags = [t.as_vec() for t in ev.tags().to_vec()]
            except Exception:
                tags = []
            print(f" author={author} content={ev.content()!r} tags={tags}")
        print()

    # try metadata with d tag? (not supported by helper, so skip)
    await client.disconnect()

asyncio.run(main())
