raannakasturi commited on
Commit
80a697c
·
verified ·
1 Parent(s): b9adfa7

Update saveDataSendMail.py

Browse files
Files changed (1) hide show
  1. saveDataSendMail.py +135 -135
saveDataSendMail.py CHANGED
@@ -1,135 +1,135 @@
1
- import psycopg2
2
- from psycopg2 import sql
3
- from dotenv import load_dotenv
4
- import os
5
- from getStatus import getStatus
6
- import tldextract
7
- import smtplib
8
- from email.mime.text import MIMEText
9
- import ssl
10
-
11
- load_dotenv()
12
- database = os.getenv('POSTGRES_DATABASE')
13
- user = os.getenv('POSTGRES_USER')
14
- password = os.getenv('POSTGRES_PASSWORD')
15
- host = os.getenv('POSTGRES_HOST')
16
- port = os.getenv('POSTGRES_DATABASE_PORT')
17
- endpoint_id = os.getenv('ENDPOINT_ID')
18
- sender_email = os.getenv('EMAIL_ADDRESS')
19
- sender_password = os.getenv('EMAIL_PASSWORD')
20
- smtp_server = os.getenv('SMTP_SERVER')
21
- smtp_port = os.getenv('SMTP_PORT')
22
-
23
- databaseConn = f"postgresql://{user}:{password}@{host}:{port}/{database}?options=endpoint%3D{endpoint_id}"
24
-
25
- def insert(domain, email, status, downcount):
26
- try:
27
- conn = psycopg2.connect(databaseConn)
28
- cursor = conn.cursor()
29
- insertData = """
30
- INSERT INTO USERDATA (DOMAIN, EMAIL, STATUS, DOWNCOUNT)
31
- VALUES (%s, %s, %s, %s);
32
- """
33
- cursor.execute(insertData, (domain, email, status, downcount))
34
- conn.commit()
35
- print(f"Data for {domain} inserted successfully.")
36
- except Exception as e:
37
- print(f"Error inserting data for {domain}: {e}")
38
- finally:
39
- cursor.close()
40
- conn.close()
41
-
42
- def get(domain):
43
- try:
44
- conn = psycopg2.connect(databaseConn)
45
- cursor = conn.cursor()
46
- getData = """
47
- SELECT STATUS, DOWNCOUNT FROM USERDATA WHERE DOMAIN = %s;
48
- """
49
- cursor.execute(getData, (domain,))
50
- existing_data = cursor.fetchone()
51
- if existing_data:
52
- return existing_data[1]
53
- else:
54
- return None
55
- except Exception as e:
56
- print(f"Error retrieving data for {domain}: {e}")
57
- return None
58
- finally:
59
- cursor.close()
60
- conn.close()
61
-
62
- def update(domain, status, downcount):
63
- try:
64
- conn = psycopg2.connect(databaseConn)
65
- cursor = conn.cursor()
66
- updateData = """
67
- UPDATE USERDATA SET STATUS = %s, DOWNCOUNT = %s WHERE DOMAIN = %s;
68
- """
69
- cursor.execute(updateData, (status, downcount, domain))
70
- conn.commit()
71
- print(f"Data for {domain} updated successfully.")
72
- except Exception as e:
73
- print(f"Error updating data for {domain}: {e}")
74
- finally:
75
- cursor.close()
76
- conn.close()
77
-
78
- def getData(EMAIL, URL, downcount):
79
- data = getStatus(URL)
80
- if data[0].startswith("2") or data[0].startswith("3"):
81
- status = "Up"
82
- downcount = 0
83
- else:
84
- status = "Down"
85
- downcount += 1
86
- domainData = tldextract.extract(URL)
87
- if domainData.subdomain == "":
88
- domain = domainData.domain + "." + domainData.suffix
89
- else:
90
- domain = domainData.subdomain + "." + domainData.domain + "." + domainData.suffix
91
- email = EMAIL
92
- return domain, email, status, downcount
93
-
94
- def saveDataSendMail(URL, email):
95
- domain, email, status, downcount = getData(email, URL, 0)
96
- existing_downcount = get(domain)
97
-
98
- if existing_downcount is None:
99
- existing_downcount = 0
100
- if status == "Up":
101
- downcount = 0
102
- else:
103
- downcount = existing_downcount + 1
104
- sendMail(email, domain, status)
105
- insert(domain, email, status, downcount)
106
- else:
107
- if status == "Up":
108
- downcount = 0
109
- else:
110
- downcount = existing_downcount + 1
111
- sendMail(email, domain, status)
112
- update(domain, status, downcount)
113
- print(f"{domain} is currently {status}")
114
- return email, downcount
115
-
116
- def sendMail(recipient_email, website_domain, status):
117
- """Sends an email notification to the user when the website is down."""
118
- subject = f"Website Alert: {website_domain} is currently {status}"
119
- body = f"The website {website_domain} is currently unavailable.\nPlease check it as soon as possible.\nIf you think this is a mistake, Contact Us."
120
-
121
- message = MIMEText(body, 'plain')
122
- message['Subject'] = subject
123
- message['From'] = sender_email
124
- message['To'] = recipient_email
125
-
126
- context = ssl.create_default_context()
127
-
128
- try:
129
- with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
130
- server.login(sender_email, sender_password)
131
- server.sendmail(sender_email, recipient_email, message.as_string())
132
- return f"Sent email notification for {website_domain} to {recipient_email}"
133
- except Exception as e:
134
- return f"Error sending email: {e}"
135
-
 
1
+ import psycopg2
2
+ from psycopg2 import sql
3
+ from dotenv import load_dotenv
4
+ import os
5
+ from getStatus import getStatus
6
+ import tldextract
7
+ import smtplib
8
+ from email.mime.text import MIMEText
9
+ import ssl
10
+
11
+ load_dotenv()
12
+ database = os.getenv('POSTGRES_DATABASE')
13
+ user = os.getenv('POSTGRES_USER')
14
+ password = os.getenv('POSTGRES_PASSWORD')
15
+ host = os.getenv('POSTGRES_HOST')
16
+ port = os.getenv('POSTGRES_DATABASE_PORT')
17
+ endpoint_id = os.getenv('ENDPOINT_ID')
18
+ sender_email = os.getenv('EMAIL_ADDRESS')
19
+ sender_password = os.getenv('EMAIL_PASSWORD')
20
+ smtp_server = os.getenv('SMTP_SERVER')
21
+ smtp_port = os.getenv('SMTP_PORT')
22
+
23
+ databaseConn = f"postgresql://{user}:{password}@{host}:{port}/{database}?options=endpoint%3D{endpoint_id}"
24
+
25
+ def insert(domain, email, status, downcount):
26
+ try:
27
+ conn = psycopg2.connect(databaseConn)
28
+ cursor = conn.cursor()
29
+ insertData = """
30
+ INSERT INTO USERDATA (DOMAIN, EMAIL, STATUS, DOWNCOUNT)
31
+ VALUES (%s, %s, %s, %s);
32
+ """
33
+ cursor.execute(insertData, (domain, email, status, downcount))
34
+ conn.commit()
35
+ print(f"Data for {domain} inserted successfully.")
36
+ except Exception as e:
37
+ print(f"Error inserting data for {domain}: {e}")
38
+ finally:
39
+ cursor.close()
40
+ conn.close()
41
+
42
+ def get(domain):
43
+ try:
44
+ conn = psycopg2.connect(databaseConn)
45
+ cursor = conn.cursor()
46
+ getData = """
47
+ SELECT STATUS, DOWNCOUNT FROM USERDATA WHERE DOMAIN = %s;
48
+ """
49
+ cursor.execute(getData, (domain,))
50
+ existing_data = cursor.fetchone()
51
+ if existing_data:
52
+ return existing_data[1]
53
+ else:
54
+ return None
55
+ except Exception as e:
56
+ print(f"Error retrieving data for {domain}: {e}")
57
+ return None
58
+ finally:
59
+ cursor.close()
60
+ conn.close()
61
+
62
+ def update(domain, status, downcount):
63
+ try:
64
+ conn = psycopg2.connect(databaseConn)
65
+ cursor = conn.cursor()
66
+ updateData = """
67
+ UPDATE USERDATA SET STATUS = %s, DOWNCOUNT = %s WHERE DOMAIN = %s;
68
+ """
69
+ cursor.execute(updateData, (status, downcount, domain))
70
+ conn.commit()
71
+ print(f"Data for {domain} updated successfully.")
72
+ except Exception as e:
73
+ print(f"Error updating data for {domain}: {e}")
74
+ finally:
75
+ cursor.close()
76
+ conn.close()
77
+
78
+ def getData(EMAIL, URL, downcount):
79
+ data = getStatus(URL)
80
+ if data[0].startswith("2") or data[0].startswith("3"):
81
+ status = "Up"
82
+ downcount = 0
83
+ else:
84
+ status = "Down"
85
+ downcount += 1
86
+ domainData = tldextract.extract(URL)
87
+ if domainData.subdomain == "":
88
+ domain = domainData.domain + "." + domainData.suffix
89
+ else:
90
+ domain = domainData.subdomain + "." + domainData.domain + "." + domainData.suffix
91
+ email = EMAIL
92
+ return domain, email, status, downcount
93
+
94
+ def saveDataSendMail(URL, email):
95
+ domain, email, status, downcount = getData(email, URL, 0)
96
+ existing_downcount = get(domain)
97
+
98
+ if existing_downcount is None:
99
+ existing_downcount = 0
100
+ if status == "Up":
101
+ downcount = 0
102
+ else:
103
+ downcount = existing_downcount + 1
104
+ mailStatus = sendMail(email, domain, status)
105
+ insert(domain, email, status, downcount)
106
+ else:
107
+ if status == "Up":
108
+ downcount = 0
109
+ else:
110
+ downcount = existing_downcount + 1
111
+ mailStatus = sendMail(email, domain, status)
112
+ update(domain, status, downcount)
113
+ print(f"{domain} is currently {status}")
114
+ return email, downcount, mailStatus
115
+
116
+ def sendMail(recipient_email, website_domain, status):
117
+ """Sends an email notification to the user when the website is down."""
118
+ subject = f"Website Alert: {website_domain} is currently {status}"
119
+ body = f"The website {website_domain} is currently unavailable.\nPlease check it as soon as possible.\nIf you think this is a mistake, Contact Us."
120
+
121
+ message = MIMEText(body, 'plain')
122
+ message['Subject'] = subject
123
+ message['From'] = sender_email
124
+ message['To'] = recipient_email
125
+
126
+ context = ssl.create_default_context()
127
+
128
+ try:
129
+ with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
130
+ server.login(sender_email, sender_password)
131
+ server.sendmail(sender_email, recipient_email, message.as_string())
132
+ return f"Sent email notification for {website_domain} to {recipient_email}"
133
+ except Exception as e:
134
+ return f"Error sending email: {e}"
135
+