Compare commits
3 Commits
0509d53e25
...
35b38f61fe
| Author | SHA1 | Date | |
|---|---|---|---|
| 35b38f61fe | |||
| a52c86738c | |||
| e1b69913a4 |
29
main.py
29
main.py
@@ -23,6 +23,9 @@ http_headers = {
|
|||||||
'User-Agent': 'Python_Reddit_Notif_Dtam/1.0'
|
'User-Agent': 'Python_Reddit_Notif_Dtam/1.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Keep track of sent
|
||||||
|
sent = []
|
||||||
|
|
||||||
# Etc
|
# Etc
|
||||||
nl = '\n'
|
nl = '\n'
|
||||||
|
|
||||||
@@ -31,10 +34,28 @@ def config_error(msg):
|
|||||||
print_and_flush(f'Error parsing config file: {msg}')
|
print_and_flush(f'Error parsing config file: {msg}')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Print and flush stdout
|
||||||
def print_and_flush(msg):
|
def print_and_flush(msg):
|
||||||
print(msg)
|
print(msg)
|
||||||
sys.stdout.flush()
|
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
|
# Read config file
|
||||||
def get_config(filename):
|
def get_config(filename):
|
||||||
config = {}
|
config = {}
|
||||||
@@ -150,6 +171,10 @@ def check_reddit(config):
|
|||||||
|
|
||||||
# Send alert out
|
# Send alert out
|
||||||
def send_alert(config, title, text, url, timestamp, keyword):
|
def send_alert(config, title, text, url, timestamp, keyword):
|
||||||
|
# Check if sent previously
|
||||||
|
if sent_previously(url):
|
||||||
|
return
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
smtp_from = config.get('smtp_from')
|
smtp_from = config.get('smtp_from')
|
||||||
smtp_to = config.get('smtp_to')
|
smtp_to = config.get('smtp_to')
|
||||||
@@ -166,7 +191,7 @@ def send_alert(config, title, text, url, timestamp, keyword):
|
|||||||
body = f'Keyword: {keyword}{nl}{nl}{nl}\
|
body = f'Keyword: {keyword}{nl}{nl}{nl}\
|
||||||
{title}{nl}{nl}{nl}\
|
{title}{nl}{nl}{nl}\
|
||||||
{text}{nl}{nl}{nl}\
|
{text}{nl}{nl}{nl}\
|
||||||
URL: {url}{nl}\
|
URL: https://www.reddit.com{url}{nl}\
|
||||||
Time: {time_format.strftime("%Y-%m-%d %H:%M:%S")}'
|
Time: {time_format.strftime("%Y-%m-%d %H:%M:%S")}'
|
||||||
|
|
||||||
message.attach(MIMEText(body, "plain"))
|
message.attach(MIMEText(body, "plain"))
|
||||||
@@ -175,8 +200,6 @@ def send_alert(config, title, text, url, timestamp, keyword):
|
|||||||
try:
|
try:
|
||||||
with smtplib.SMTP(config.get('smtp_server'),
|
with smtplib.SMTP(config.get('smtp_server'),
|
||||||
config.get('smtp_port')) as server:
|
config.get('smtp_port')) as server:
|
||||||
server.login(config.get('smtp_username'),
|
|
||||||
config.get('smtp_password'))
|
|
||||||
text = message.as_string()
|
text = message.as_string()
|
||||||
|
|
||||||
server.sendmail(smtp_from, smtp_to, text)
|
server.sendmail(smtp_from, smtp_to, text)
|
||||||
|
|||||||
Reference in New Issue
Block a user