From 35b38f61fe2a2f7ed7458c7bf2fda107621bce1a Mon Sep 17 00:00:00 2001 From: Daniel Tam Date: Sun, 26 May 2024 04:03:01 -0500 Subject: [PATCH] check for double sends --- main.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.py b/main.py index d68da9b..ae0f143 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,9 @@ http_headers = { 'User-Agent': 'Python_Reddit_Notif_Dtam/1.0' } +# Keep track of sent +sent = [] + # Etc nl = '\n' @@ -31,10 +34,28 @@ def config_error(msg): print_and_flush(f'Error parsing config file: {msg}') sys.exit(1) +# Print and flush stdout def print_and_flush(msg): print(msg) sys.stdout.flush() +# Check if message was sent previously +def sent_previously(url): + # Simple hash to keep track of urls + hashed_url = hash(url) + + # If not sent previously, add it to the sent list + if hashed_url not in sent: + sent.append(hashed_url) + + # Prune sent list + while len(sent) > 100: + sent.pop() + + return False + else: + return True + # Read config file def get_config(filename): config = {} @@ -150,6 +171,10 @@ def check_reddit(config): # Send alert out def send_alert(config, title, text, url, timestamp, keyword): + # Check if sent previously + if sent_previously(url): + return + # Setup smtp_from = config.get('smtp_from') smtp_to = config.get('smtp_to')