Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- app.py +33 -32
- getScreenshot.py +86 -117
- getStatus.py +2 -2
- main.py +44 -39
- saveDataSendMail.py +135 -133
app.py
CHANGED
@@ -1,33 +1,34 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from main import getData, installGC
|
3 |
-
|
4 |
-
def run(URL, email):
|
5 |
-
domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount = getData(URL, email)
|
6 |
-
return domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount
|
7 |
-
|
8 |
-
app = gr.Interface(
|
9 |
-
fn=run,
|
10 |
-
inputs=[
|
11 |
-
gr.Textbox(label="Enter URL", placeholder="https://google.com", type="text", interactive=True),
|
12 |
-
gr.Textbox(label="Enter Email", placeholder="example@gmail.com", type="
|
13 |
-
],
|
14 |
-
outputs=[
|
15 |
-
gr.Textbox(label="Domain", type="text", interactive=False),
|
16 |
-
gr.Textbox(label="URL", type="text", interactive=False),
|
17 |
-
gr.Textbox(label="Code", type="text", interactive=False),
|
18 |
-
gr.Textbox(label="Status", type="text", interactive=False),
|
19 |
-
gr.Textbox(label="Web Status", type="text", interactive=False),
|
20 |
-
gr.Textbox(label="More Details", type="text", interactive=False),
|
21 |
-
gr.Image(label="Website Screenshot"),
|
22 |
-
gr.Textbox(label="
|
23 |
-
gr.Textbox(label="
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
app.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from main import getData, installGC
|
3 |
+
|
4 |
+
def run(URL, email):
|
5 |
+
domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount = getData(URL, email)
|
6 |
+
return domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount
|
7 |
+
|
8 |
+
app = gr.Interface(
|
9 |
+
fn=run,
|
10 |
+
inputs=[
|
11 |
+
gr.Textbox(label="Enter URL", placeholder="https://google.com", type="text", interactive=True),
|
12 |
+
gr.Textbox(label="Enter Email", placeholder="example@gmail.com", type="email", interactive=True)
|
13 |
+
],
|
14 |
+
outputs=[
|
15 |
+
gr.Textbox(label="Domain", type="text", interactive=False, allow_copy=True),
|
16 |
+
gr.Textbox(label="URL", type="text", interactive=False, allow_copy=True),
|
17 |
+
gr.Textbox(label="Code", type="text", interactive=False),
|
18 |
+
gr.Textbox(label="Status", type="text", interactive=False),
|
19 |
+
gr.Textbox(label="Web Status", type="text", interactive=False),
|
20 |
+
gr.Textbox(label="More Details", type="text", interactive=False, allow_copy=True),
|
21 |
+
gr.Image(label="Website Screenshot"),
|
22 |
+
gr.Textbox(label="Screenshot URL/Error", type="text", interactive=False, allow_copy=True),
|
23 |
+
gr.Textbox(label="Email", type="email", interactive=False),
|
24 |
+
gr.Textbox(label="Download Count", type="text", interactive=False),
|
25 |
+
],
|
26 |
+
title="Website Monitor<br> by <a href='https://nayankasturi.eu.org'>Nayan Kasturi</a> aka Raanna.<br> Checkout the <a href='https://github.com/raannakasturi'>Github</a> for more projects and contact info.",
|
27 |
+
description="This app captures website status and its screenshot and displays it, along with sending mail to the person, in case website is down.",
|
28 |
+
api_name="get",
|
29 |
+
concurrency_limit=10
|
30 |
+
)
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
#installGC()
|
34 |
app.launch()
|
getScreenshot.py
CHANGED
@@ -1,117 +1,86 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import json
|
4 |
-
import base64
|
5 |
-
import time
|
6 |
-
import requests
|
7 |
-
from selenium import webdriver
|
8 |
-
from selenium.webdriver.chrome.service import Service
|
9 |
-
from selenium.webdriver.chrome.options import Options
|
10 |
-
from selenium_stealth import stealth
|
11 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
12 |
-
from dotenv import load_dotenv
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
options
|
17 |
-
options.add_argument("--
|
18 |
-
options.add_argument("--disable-
|
19 |
-
options.add_argument("--
|
20 |
-
options.add_argument("--
|
21 |
-
options.
|
22 |
-
options.add_experimental_option(
|
23 |
-
options.
|
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 |
-
def
|
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 |
-
return
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
return img, imgurl
|
88 |
-
else:
|
89 |
-
ss = saveScreenshot(url)
|
90 |
-
img, imgurl = uploadandDelSS(ss)
|
91 |
-
if ss == "1366-768.png":
|
92 |
-
return img, imgurl
|
93 |
-
else:
|
94 |
-
os.remove(ss)
|
95 |
-
return img, imgurl
|
96 |
-
|
97 |
-
def checkSystem():
|
98 |
-
OS = sys.platform
|
99 |
-
if OS.startswith('linux'):
|
100 |
-
print("Linux OS detected.")
|
101 |
-
return "linux"
|
102 |
-
elif OS.startswith('win32') or OS.startswith('cygwin'):
|
103 |
-
print("Windows OS detected.")
|
104 |
-
return "windows"
|
105 |
-
else:
|
106 |
-
print("Unsupported OS. We are currently supported on Windows and Linux only.")
|
107 |
-
return "unsupported"
|
108 |
-
|
109 |
-
def checkinstallChrome():
|
110 |
-
OS = checkSystem()
|
111 |
-
if OS == "linux":
|
112 |
-
return checkinstallGC.checkGC("linux")
|
113 |
-
elif OS == "windows":
|
114 |
-
return checkinstallGC.checkGC("windows")
|
115 |
-
else:
|
116 |
-
print("Can't check for Chrome installation or install Chrome. Exiting..")
|
117 |
-
return False
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import json
|
4 |
+
import base64
|
5 |
+
import time
|
6 |
+
import requests
|
7 |
+
from selenium import webdriver
|
8 |
+
from selenium.webdriver.chrome.service import Service
|
9 |
+
from selenium.webdriver.chrome.options import Options
|
10 |
+
from selenium_stealth import stealth
|
11 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
|
14 |
+
def driverSetup():
|
15 |
+
options = Options()
|
16 |
+
options.add_argument("--no-sandbox")
|
17 |
+
options.add_argument("--disable-dev-shm-usage")
|
18 |
+
options.add_argument("--disable-gpu")
|
19 |
+
options.add_argument("--start-maximized")
|
20 |
+
options.add_argument("--headless=new")
|
21 |
+
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
22 |
+
options.add_experimental_option('useAutomationExtension', False)
|
23 |
+
options.add_argument("--window-size=1366,768")
|
24 |
+
service = Service(ChromeDriverManager().install())
|
25 |
+
driver = webdriver.Chrome(service=service, options=options)
|
26 |
+
|
27 |
+
stealth(driver,
|
28 |
+
languages=["en-US", "en"],
|
29 |
+
vendor="Google Inc.",
|
30 |
+
platform="Linux",
|
31 |
+
webgl_vendor="MesaLib",
|
32 |
+
renderer="Mesa DRI Intel(R) HD Graphics 5500",
|
33 |
+
fix_hairline=True,
|
34 |
+
)
|
35 |
+
return driver
|
36 |
+
|
37 |
+
def screenshotName(url):
|
38 |
+
timestr = time.strftime("%Y%m%d_%H%M%S")
|
39 |
+
url = url.replace('https://', '').replace('http://', '').replace('www.', '')
|
40 |
+
if url.endswith('/'):
|
41 |
+
url = url[:-1]
|
42 |
+
return f"{url.replace('/', '')}_{timestr}.png"
|
43 |
+
|
44 |
+
def getScreenshots(driver, url):
|
45 |
+
driver.get(url)
|
46 |
+
ssname = screenshotName(url)
|
47 |
+
driver.save_screenshot(ssname)
|
48 |
+
return ssname
|
49 |
+
|
50 |
+
def saveScreenshot(url):
|
51 |
+
driver = driverSetup()
|
52 |
+
screenshot_file = getScreenshots(driver, url)
|
53 |
+
driver.quit()
|
54 |
+
return screenshot_file
|
55 |
+
|
56 |
+
def uploadandDelSS(file):
|
57 |
+
load_dotenv()
|
58 |
+
api = os.getenv("IMGBBAPI")
|
59 |
+
url = os.getenv("IMGBBURL")
|
60 |
+
filename = file
|
61 |
+
try:
|
62 |
+
with open(f"{filename}", "rb") as file:
|
63 |
+
name = file.name[:-4]
|
64 |
+
payload = {
|
65 |
+
"key": api,
|
66 |
+
"image": base64.b64encode(file.read()),
|
67 |
+
"name": name,
|
68 |
+
"expiration": "15552000"
|
69 |
+
}
|
70 |
+
r = requests.post(url, data=payload)
|
71 |
+
view_url = json.loads(r.text)["data"]["display_url"]
|
72 |
+
return view_url, view_url
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Error: {e}")
|
75 |
+
return "https://i.ibb.co/s5c9QpD/1366x768.png", "Error in uploading the image. Please try again."
|
76 |
+
|
77 |
+
def getScreenshot(url):
|
78 |
+
if url == "":
|
79 |
+
img = "https://i.ibb.co/s5c9QpD/1366x768.png"
|
80 |
+
imgurl = "Please Enter the URL to capture the screenshot."
|
81 |
+
return img, imgurl
|
82 |
+
else:
|
83 |
+
ss = saveScreenshot(url)
|
84 |
+
img, imgurl = uploadandDelSS(ss)
|
85 |
+
os.remove(ss)
|
86 |
+
return img, imgurl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getStatus.py
CHANGED
@@ -2,8 +2,8 @@ import requests
|
|
2 |
|
3 |
def getStatus(URL):
|
4 |
try:
|
5 |
-
headers = {'User-Agent': 'Mozilla/5.0
|
6 |
-
webResponse = requests.get(URL, headers=headers)
|
7 |
webCode = str(webResponse.status_code)
|
8 |
code, status, webStatus, moreDetails = statusCodes(webCode)
|
9 |
return code, status, webStatus, moreDetails
|
|
|
2 |
|
3 |
def getStatus(URL):
|
4 |
try:
|
5 |
+
headers = {'User-Agent': 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +https://www.google.com/bot.html) Safari/537.36'}
|
6 |
+
webResponse = requests.get(URL, headers=headers, timeout=10)
|
7 |
webCode = str(webResponse.status_code)
|
8 |
code, status, webStatus, moreDetails = statusCodes(webCode)
|
9 |
return code, status, webStatus, moreDetails
|
main.py
CHANGED
@@ -1,40 +1,45 @@
|
|
1 |
-
import tldextract
|
2 |
-
from getStatus import getStatus
|
3 |
-
from getScreenshot import getScreenshot
|
4 |
-
from saveDataSendMail import saveDataSendMail
|
5 |
-
import subprocess
|
6 |
-
import sys
|
7 |
-
|
8 |
-
def cleanURL(inputURL):
|
9 |
-
url = tldextract.extract(inputURL)
|
10 |
-
if url.subdomain == "":
|
11 |
-
domain = url.domain + "." + url.suffix
|
12 |
-
URL = "
|
13 |
-
else:
|
14 |
-
domain = url.subdomain + "." + url.domain + "." + url.suffix
|
15 |
-
URL = "
|
16 |
-
return domain, URL
|
17 |
-
|
18 |
-
def getData(url, email):
|
19 |
-
domain, URL = cleanURL(url)
|
20 |
-
code, status, webStatus, moreDetails = getStatus(URL)
|
21 |
-
if
|
22 |
-
img, imgurl = getScreenshot(URL)
|
23 |
-
else:
|
24 |
-
img = "1366-768.png"
|
25 |
-
imgurl = "Website
|
26 |
-
email, downcount = saveDataSendMail(URL, email)
|
27 |
-
return domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount
|
28 |
-
|
29 |
-
def installGC():
|
30 |
-
OS = sys.platform
|
31 |
-
|
32 |
-
|
33 |
-
subprocess.run(['apt-get', '
|
34 |
-
subprocess.run(['
|
35 |
-
subprocess.run(['
|
36 |
-
subprocess.run(['
|
37 |
-
subprocess.run(['
|
38 |
-
subprocess.run(['apt-get', '
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
subprocess.run(['powershell', '-command', 'winget', 'install', 'Google.Chrome', '--force'])
|
|
|
1 |
+
import tldextract
|
2 |
+
from getStatus import getStatus
|
3 |
+
from getScreenshot import getScreenshot
|
4 |
+
from saveDataSendMail import saveDataSendMail
|
5 |
+
import subprocess
|
6 |
+
import sys
|
7 |
+
|
8 |
+
def cleanURL(inputURL):
|
9 |
+
url = tldextract.extract(inputURL)
|
10 |
+
if url.subdomain == "":
|
11 |
+
domain = url.domain + "." + url.suffix
|
12 |
+
URL = "https://" + domain
|
13 |
+
else:
|
14 |
+
domain = url.subdomain + "." + url.domain + "." + url.suffix
|
15 |
+
URL = "https://" + domain
|
16 |
+
return domain, URL
|
17 |
+
|
18 |
+
def getData(url, email):
|
19 |
+
domain, URL = cleanURL(url)
|
20 |
+
code, status, webStatus, moreDetails = getStatus(URL)
|
21 |
+
if code.startswith("2"):
|
22 |
+
img, imgurl = getScreenshot(URL)
|
23 |
+
else:
|
24 |
+
img = "1366-768.png"
|
25 |
+
imgurl = "Website is down. No screenshot available."
|
26 |
+
email, downcount = saveDataSendMail(URL, email)
|
27 |
+
return domain, URL, code, status, webStatus, moreDetails, img, imgurl, email, downcount
|
28 |
+
|
29 |
+
def installGC():
|
30 |
+
OS = sys.platform
|
31 |
+
if OS == 'linux':
|
32 |
+
subprocess.run(['apt-get', 'update'])
|
33 |
+
subprocess.run(['apt-get', 'install', '-y', 'wget', 'unzip'])
|
34 |
+
subprocess.run(['wget', 'https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'])
|
35 |
+
subprocess.run(['apt-get', 'install', '-y', './google-chrome-stable_current_amd64.deb'])
|
36 |
+
subprocess.run(['rm', 'google-chrome-stable_current_amd64.deb'])
|
37 |
+
subprocess.run(['apt-get', 'clean'])
|
38 |
+
subprocess.run(['sudo', 'apt-get', 'update'])
|
39 |
+
subprocess.run(['sudo', 'apt-get', 'install', '-y', 'wget', 'unzip'])
|
40 |
+
subprocess.run(['wget', 'https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'])
|
41 |
+
subprocess.run(['sudo', 'apt-get', 'install', '-y', './google-chrome-stable_current_amd64.deb'])
|
42 |
+
subprocess.run(['sudo', 'rm', 'google-chrome-stable_current_amd64.deb'])
|
43 |
+
subprocess.run(['sudo', 'apt-get', 'clean'])
|
44 |
+
else:
|
45 |
subprocess.run(['powershell', '-command', 'winget', 'install', 'Google.Chrome', '--force'])
|
saveDataSendMail.py
CHANGED
@@ -1,133 +1,135 @@
|
|
1 |
-
import psycopg2
|
2 |
-
from psycopg2 import sql
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
import os
|
5 |
-
from
|
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 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
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 |
-
|
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 |
-
domain = domainData.
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
downcount =
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
downcount =
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
message
|
122 |
-
message['
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
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 |
+
|