|
9 | 9 | from nostr.filter import Filter, Filters |
10 | 10 | from nostr.key import PrivateKey |
11 | 11 |
|
| 12 | +# Mapping of Nostr event kinds to human-readable names |
| 13 | +EVENT_KIND_NAMES = { |
| 14 | + 0: "SET_METADATA", |
| 15 | + 1: "TEXT_NOTE", |
| 16 | + 2: "RECOMMEND_RELAY", |
| 17 | + 3: "CONTACTS", |
| 18 | + 4: "ENCRYPTED_DM", |
| 19 | + 5: "DELETE", |
| 20 | +} |
| 21 | + |
| 22 | +def get_kind_name(kind): |
| 23 | + """Get human-readable name for an event kind""" |
| 24 | + return EVENT_KIND_NAMES.get(kind, f"UNKNOWN({kind})") |
| 25 | + |
| 26 | +def format_timestamp(timestamp): |
| 27 | + """Format a Unix timestamp to a readable date/time string""" |
| 28 | + try: |
| 29 | + import time as time_module |
| 30 | + # Convert Unix timestamp to time tuple |
| 31 | + time_tuple = time_module.localtime(timestamp) |
| 32 | + # Format as YYYY-MM-DD HH:MM |
| 33 | + return "{:04d}-{:02d}-{:02d} {:02d}:{:02d}".format( |
| 34 | + time_tuple[0], time_tuple[1], time_tuple[2], |
| 35 | + time_tuple[3], time_tuple[4] |
| 36 | + ) |
| 37 | + except: |
| 38 | + return str(timestamp) |
| 39 | + |
| 40 | +def format_tags(tags): |
| 41 | + """Format event tags into a readable string""" |
| 42 | + if not tags: |
| 43 | + return "" |
| 44 | + |
| 45 | + tag_strs = [] |
| 46 | + for tag in tags: |
| 47 | + if len(tag) >= 2: |
| 48 | + tag_type = tag[0] |
| 49 | + tag_value = tag[1] |
| 50 | + # Truncate long values |
| 51 | + if len(tag_value) > 16: |
| 52 | + tag_value = tag_value[:16] + "..." |
| 53 | + tag_strs.append(f"{tag_type}:{tag_value}") |
| 54 | + |
| 55 | + if tag_strs: |
| 56 | + return "Tags: " + ", ".join(tag_strs) |
| 57 | + return "" |
| 58 | + |
12 | 59 | class NostrEvent: |
13 | | - """Simple wrapper for a Nostr event""" |
| 60 | + """Simple wrapper for a Nostr event with rich details""" |
14 | 61 | def __init__(self, event_obj): |
15 | 62 | self.event = event_obj |
16 | 63 | self.created_at = event_obj.created_at |
17 | 64 | self.content = event_obj.content |
18 | 65 | self.public_key = event_obj.public_key |
| 66 | + self.kind = event_obj.kind |
| 67 | + self.tags = event_obj.tags if hasattr(event_obj, 'tags') else [] |
| 68 | + |
| 69 | + def get_kind_name(self): |
| 70 | + """Get human-readable name for this event's kind""" |
| 71 | + return get_kind_name(self.kind) |
| 72 | + |
| 73 | + def get_formatted_timestamp(self): |
| 74 | + """Get formatted timestamp for this event""" |
| 75 | + return format_timestamp(self.created_at) |
| 76 | + |
| 77 | + def get_formatted_tags(self): |
| 78 | + """Get formatted tags for this event""" |
| 79 | + return format_tags(self.tags) |
19 | 80 |
|
20 | 81 | def __str__(self): |
21 | | - return f"{self.content}" |
| 82 | + """Return formatted event details""" |
| 83 | + kind_name = self.get_kind_name() |
| 84 | + timestamp = self.get_formatted_timestamp() |
| 85 | + tags_str = self.get_formatted_tags() |
| 86 | + |
| 87 | + # Build the formatted event string |
| 88 | + result = f"[{kind_name}] {timestamp}\n" |
| 89 | + if self.content: |
| 90 | + result += f"{self.content}" |
| 91 | + if tags_str: |
| 92 | + result += f"\n{tags_str}" |
| 93 | + |
| 94 | + return result |
22 | 95 |
|
23 | 96 | class NostrClient(): |
24 | 97 | """Simple Nostr event subscriber that connects to a relay and subscribes to a public key's events""" |
|
0 commit comments