-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_wifi_report.py
94 lines (79 loc) · 3.28 KB
/
email_wifi_report.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
import matplotlib.pyplot as plt
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import csv
from datetime import datetime, timedelta
from config import SENDER_EMAIL, RECIEVER_EMAIL, APP_PASSWORD
import os
import shutil
WIFI_SPEED_DATA = os.path.join(os.path.dirname(os.path.realpath(__file__)), "wifi_speed_data.csv")
WIFI_SPEED_DAILY_AVERAGES = os.path.join(os.path.dirname(os.path.realpath(__file__)), "wifi_speed_daily_averages.png")
PYCACHE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "__pycache__")
def clean_up():
#Delete wifi_speed_daily_averags.png
os.remove(WIFI_SPEED_DAILY_AVERAGES)
# Check if the __pycache__ folder exists
if os.path.exists(PYCACHE):
try:
# Delete the __pycache__ folder and all its contents recursively
shutil.rmtree(PYCACHE)
except OSError as e:
print(f"Error: {e.strerror}")
def calculate_daily_averages():
daily_data = {}
with open(WIFI_SPEED_DATA, "r") as f:
csv_reader = csv.reader(f)
for row in csv_reader:
timestamp_str, download_speed, upload_speed = row
date = datetime.fromisoformat(timestamp_str).date()
if date not in daily_data:
daily_data[date] = {"download_sum": 0, "upload_sum": 0, "count": 0}
daily_data[date]["download_sum"] += float(download_speed)
daily_data[date]["upload_sum"] += float(upload_speed)
daily_data[date]["count"] += 1
daily_averages = {}
for date, data in daily_data.items():
daily_averages[date] = {
"download_avg": data["download_sum"] / data["count"],
"upload_avg": data["upload_sum"] / data["count"]
}
return daily_averages
def create_report_graph():
daily_averages = calculate_daily_averages()
# Get the dates of the previous seven days
dates = [datetime.now().date() - timedelta(days=i) for i in range(6, -1, -1)]
download_averages = [daily_averages.get(date, {"download_avg": 0})["download_avg"] for date in dates]
upload_averages = [daily_averages.get(date, {"upload_avg": 0})["upload_avg"] for date in dates]
# Create graph
plt.plot(dates, download_averages, label="Average Download Speed (Mbps)")
plt.plot(dates, upload_averages, label="Average Upload Speed (Mbps)")
plt.xlabel("Date")
plt.ylabel("Speed (Mbps)")
plt.title("WiFi Speed Daily Averages")
plt.xticks(rotation=45)
plt.legend()
plt.tight_layout()
plt.savefig(WIFI_SPEED_DAILY_AVERAGES)
def send_email_with_report():
create_report_graph()
# Send email
msg = MIMEMultipart()
msg['From'] = SENDER_EMAIL
msg['To'] = RECIEVER_EMAIL
msg['Subject'] = "WiFi Speed Daily Averages Report"
body = "WiFi speed daily averages report is attached."
msg.attach(MIMEText(body, 'plain'))
with open(WIFI_SPEED_DAILY_AVERAGES, "rb") as img_file:
img = MIMEImage(img_file.read())
msg.attach(img)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(SENDER_EMAIL, APP_PASSWORD)
text = msg.as_string()
server.sendmail(SENDER_EMAIL, RECIEVER_EMAIL, text)
server.quit()
if __name__ == "__main__":
send_email_with_report()
clean_up()