From 6cfae6abdfcd0412c451c0b610799b24f22d60a2 Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Sun, 3 Mar 2024 12:29:36 -0500 Subject: [PATCH] config file added for folloer notifications --- .gitignore | 1 + new_follower_notification.py | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cffcb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/new_follower_notification.py b/new_follower_notification.py index 46edda3..a29c079 100644 --- a/new_follower_notification.py +++ b/new_follower_notification.py @@ -2,11 +2,16 @@ import json import asyncio import websockets -# Replace these values with your own Nostr account details -your_pubkey = "16f1a0100d4cfffbcc4230e8e0e4290cc5849c1adc64d6653fda07c031b1074b" -your_relay_url = "wss://nos.lol" +def load_config(file_path): + try: + with open(file_path, 'r') as config_file: + config = json.load(config_file) + return config + except FileNotFoundError: + print(f"Config file not found at {file_path}.") + return None -async def listen_to_relay(): +async def listen_to_relay(your_pubkey,your_relay_url): uri = f"{your_relay_url}/ws" async with websockets.connect(uri) as websocket: # Subscribe to all kind 3 events @@ -15,16 +20,16 @@ async def listen_to_relay(): while True: response = await websocket.recv() - await handle_event(response) # Await the handle_event coroutine + await handle_event(response, your_pubkey) # Await the handle_event coroutine -async def handle_event(event_json): +async def handle_event(event_json, your_pubkey): event = json.loads(event_json) if event[0] == "EVENT": event_data = event[2] if event_data["kind"] == 3: # Follow list event - await handle_follow_list(event_data) # Await the handle_follow_list coroutine + await handle_follow_list(event_data, your_pubkey) # Await the handle_follow_list coroutine -async def handle_follow_list(follow_list_event): +async def handle_follow_list(follow_list_event, your_pubkey): if "tags" in follow_list_event: # Extract the list of "p" tags from the follow list event p_tags = [tag for tag in follow_list_event["tags"] if tag[0] == "p"] @@ -34,4 +39,8 @@ async def handle_follow_list(follow_list_event): print(f"You have a new follower: {follow_list_event.get('pubkey', '')}") if __name__ == "__main__": - asyncio.get_event_loop().run_until_complete(listen_to_relay()) + config_path = 'config.json' + config = load_config(config_path) + + if config: + asyncio.get_event_loop().run_until_complete(listen_to_relay(config["your_pubkey"], config["your_relay_url"]))