-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (89 loc) · 4.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import datetime
import json
import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import argparse
import pytz
parser = argparse.ArgumentParser(description="A Python tool designed to send email notifications about user updates "
"in an ADS-B network.")
parser.add_argument("--from-email", default=None, help="Sender's email address", required=True)
parser.add_argument("--to", default=None, help="Receiver's email address", required=True)
parser.add_argument("--server", default=None, help="Mail server", required=True)
parser.add_argument("--port", default=465, type=int, help="Port server")
parser.add_argument("--user", default=None, help="Username for mail server access")
parser.add_argument("--password", default=None, help="Password for mail server access", required=True)
parser.add_argument("--timezone", default='Europe/Rome', help="Time zone")
args = parser.parse_args()
FROM_EMAIL = args.from_email
TO = args.to
SERVER = args.server
PORT = args.port
USER = args.user if args.user else args.from_email
PASSWORD = args.password
TIMEZONE = args.timezone
old_peers = {}
while True:
print("Started Fly Italy ADS-B New Receivers Notifier")
current_date = datetime.datetime.now().strftime('%Y-%m-%d')
# Read from clients.json
with open("/data/clients.json", "r") as file:
data = json.load(file)
lista_peer = [str(token.split('_')[0]) for token in data.keys()]
lista_peer.sort()
if lista_peer != old_peers:
lista_peer_set = set(lista_peer)
old_peers_set = set(old_peers)
added_peers = lista_peer_set - old_peers_set
removed_peers = old_peers_set - lista_peer_set
added_dict = {index: value for index, value in enumerate(added_peers)}
removed_dict = {index: value for index, value in enumerate(removed_peers)}
result = [added_dict, removed_dict]
different_peers_str = json.dumps(result)
msg = MIMEMultipart()
msg['From'] = FROM_EMAIL
msg['To'] = TO
msg['Subject'] = f'Users Update - Aggiornamento Utenti {datetime.datetime.now().strftime("%d-%m-%y")}'
html_output = f"""
<div style="color: white; font-size: 1px; max-height: 0; overflow: hidden;">
new:{len(added_dict.keys())}, old: {len(removed_dict.keys())}
</div>
"""
html_output += "<table border='1'>"
html_output += "<thead><tr><th>Index</th><th>New</th><th>Removed</th></tr></thead>"
html_output += "<tbody>"
for i in range(max(len(added_dict), len(removed_dict))):
html_output += "<tr>"
# Index
html_output += f"<td>{i}</td>"
# Added
if i in added_dict:
html_output += f"<td>{added_dict[i]}</td>"
else:
html_output += "<td></td>"
# Removed
if i in removed_dict:
html_output += f"<td>{removed_dict[i]}</td>"
else:
html_output += "<td></td>"
html_output += "</tr>"
html_output += "</tbody>"
html_output += "</table>"
msg.attach(MIMEText(html_output, 'html'))
try:
with smtplib.SMTP_SSL(SERVER, PORT) as server:
server.connect(SERVER, PORT)
server.login(USER, PASSWORD)
server.sendmail(FROM_EMAIL, TO, msg.as_string())
print(f"Email sent successfully on {current_date}!")
except smtplib.SMTPException as e:
print(f"Failed to send email on {current_date}: {e} ")
except Exception as e:
print(f"An unexpected error occurred on {current_date}: {e}")
old_peers = lista_peer
else:
print(f"Email not sent on {current_date}: same peers of yesterday.")
time.sleep(((datetime.datetime.now(pytz.timezone(TIMEZONE)).replace(hour=0, minute=0, second=0,
microsecond=0) + datetime.timedelta(
days=1)) - datetime.datetime.now(pytz.timezone(TIMEZONE))).total_seconds())