Compare commits

...

3 Commits

Author SHA1 Message Date
fcb17394c6 add debug messages 2024-05-26 03:22:40 -05:00
f9e19fb597 fix stdout flushing 2024-05-26 03:17:10 -05:00
4135f4960e flush stdout 2024-05-26 03:15:28 -05:00

21
main.py
View File

@@ -28,9 +28,13 @@ nl = '\n'
# Config error message
def config_error(msg):
print(f'Error parsing config file: {msg}')
print_and_flush(f'Error parsing config file: {msg}')
sys.exit(1)
def print_and_flush(msg):
print(msg)
sys.stdout.flush()
# Read config file
def get_config(filename):
config = {}
@@ -43,7 +47,7 @@ def get_config(filename):
raise configparser.Error
except configparser.Error as e:
print(f'Error reading config file {e}')
print_and_flush(f'Error reading config file {e}')
sys.exit(1)
# Parse config file
@@ -111,6 +115,10 @@ def check_reddit(config):
while(True):
for subreddit in subreddits:
# Debug message
current_time = datetime.datetime.now()
print_and_flush(f'Starting search at: {current_time.strftime("%Y-%m-%d %H:%M:%S")}')
resp = requests.get(f'https://www.reddit.com/r/{subreddit}/new.json', headers=http_headers)
if resp.status_code == 200:
@@ -145,13 +153,16 @@ def send_alert(config, title, text, url, timestamp, keyword):
# Setup
smtp_from = config.get('smtp_from')
smtp_to = config.get('smtp_to')
time_format = datetime.datetime.fromtimestamp(timestamp)
# Debug message
print_and_flush(f'Found match: {title} at {time_format.strftime("%Y-%m-%d %H:%M:%S")}')
# Setup message
message = MIMEMultipart()
message["From"] = smtp_from
message["To"] = smtp_to
message["Subject"] = f'Reddit Notify: Found Match ({title})'
time_format = datetime.datetime.fromtimestamp(timestamp)
body = f'Keyword: {keyword}{nl}{nl}{nl}\
{title}{nl}{nl}{nl}\
{text}{nl}{nl}{nl}\
@@ -170,12 +181,12 @@ def send_alert(config, title, text, url, timestamp, keyword):
server.sendmail(smtp_from, smtp_to, text)
except Exception as e:
print(f'SMTP Send Error: {e}')
print_and_flush(f'SMTP Send Error: {e}')
if __name__ == '__main__':
config = get_config(args.config)
print(f'Current config file: {config}')
print_and_flush(f'Current config file: {config}')
check_reddit(config)