Spaces:
Sleeping
Sleeping
File size: 4,499 Bytes
bd0aee5 | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import psycopg2
from psycopg2 import sql
from dotenv import load_dotenv
import os
from getStatus import getStatus
import tldextract
import smtplib
from email.mime.text import MIMEText
import ssl
load_dotenv()
database = os.getenv('POSTGRES_DATABASE')
user = os.getenv('POSTGRES_USER')
password = os.getenv('POSTGRES_PASSWORD')
host = os.getenv('POSTGRES_HOST')
port = os.getenv('POSTGRES_DATABASE_PORT')
endpoint_id = os.getenv('ENDPOINT_ID')
sender_email = os.getenv('EMAIL_ADDRESS')
sender_password = os.getenv('EMAIL_PASSWORD')
smtp_server = os.getenv('SMTP_SERVER')
smtp_port = os.getenv('SMTP_PORT')
databaseConn = f"postgresql://{user}:{password}@{host}:{port}/{database}?options=endpoint%3D{endpoint_id}"
def insert(domain, email, status, downcount):
try:
conn = psycopg2.connect(databaseConn)
cursor = conn.cursor()
insertData = """
INSERT INTO USERDATA (DOMAIN, EMAIL, STATUS, DOWNCOUNT)
VALUES (%s, %s, %s, %s);
"""
cursor.execute(insertData, (domain, email, status, downcount))
conn.commit()
cursor.close()
conn.close()
print(f"Data for {domain} inserted successfully.")
except Exception as e:
print(f"Error inserting data for {domain}: {e}")
def get(domain):
try:
conn = psycopg2.connect(databaseConn)
cursor = conn.cursor()
getData = """
SELECT STATUS, DOWNCOUNT FROM USERDATA WHERE DOMAIN = %s;
"""
cursor.execute(getData, (domain,))
existing_data = cursor.fetchone()
cursor.close()
conn.close()
if existing_data:
return existing_data[1]
else:
return None
except Exception as e:
print(f"Error retrieving data for {domain}: {e}")
return None
def update(domain, status, downcount):
try:
conn = psycopg2.connect(databaseConn)
cursor = conn.cursor()
updateData = """
UPDATE USERDATA SET STATUS = %s, DOWNCOUNT = %s WHERE DOMAIN = %s;
"""
cursor.execute(updateData, (status, downcount, domain))
conn.commit()
cursor.close()
conn.close()
print(f"Data for {domain} updated successfully.")
except Exception as e:
print(f"Error updating data for {domain}: {e}")
def getData(EMAIL, URL, downcount):
data = getStatus(URL)
if data[0].startswith("2") or data[0].startswith("3"):
status = "Up"
downcount = 0
else:
status = "Down"
downcount += 1
domainData = tldextract.extract(URL)
if domainData.subdomain == "":
domain = domainData.domain + "." + domainData.suffix
else:
domain = domainData.subdomain + "." + domainData.domain + "." + domainData.suffix
email = EMAIL
return domain, email, status, downcount
def saveDataSendMail(URL, email):
domain, email, status, downcount = getData(email, URL, 0)
existing_downcount = get(domain)
if existing_downcount is None:
existing_downcount = 0
if status == "Up":
downcount = 0
else:
downcount = existing_downcount + 1
sendMail(email, domain, status)
insert(domain, email, status, downcount)
else:
if status == "Up":
downcount = 0
else:
downcount = existing_downcount + 1
sendMail(email, domain, status)
update(domain, status, downcount)
print(f"{domain} is currently {status}")
return email, downcount
def sendMail(recipient_email, website_domain, status):
"""Sends an email notification to the user when the website is down."""
subject = f"Website Alert: {website_domain} is currently {status}"
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."
message = MIMEText(body, 'plain')
message['Subject'] = subject
message['From'] = sender_email
message['To'] = recipient_email
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient_email, message.as_string())
mailStatus = f"Sent email notification for {website_domain} to {recipient_email}"
except Exception as e:
mailStatus = f"Error sending email: {e}"
return mailStatus
|