Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1063,43 +1063,60 @@ if __name__ == "__main__":
|
|
1063 |
metadata={"category": "utility"}
|
1064 |
)
|
1065 |
|
1066 |
-
"website_monitor": Template(
|
1067 |
code="""
|
1068 |
-
|
1069 |
-
|
1070 |
-
|
|
|
1071 |
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
1075 |
-
|
1076 |
-
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
"response_time": response.elapsed.total_seconds(),
|
1081 |
-
"last_checked": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
1082 |
-
}
|
1083 |
-
except Exception as e:
|
1084 |
-
return {"error": str(e)}
|
1085 |
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
|
1091 |
-
|
1092 |
-
|
1093 |
-
result_output = gr.JSON(label="Monitoring Result")
|
1094 |
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
1099 |
-
|
|
|
|
|
|
|
|
|
1100 |
|
1101 |
-
|
1102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1103 |
""",
|
1104 |
description="Monitor the uptime and response time of a website",
|
1105 |
components=["Textbox", "Button", "JSON"],
|
|
|
1063 |
metadata={"category": "utility"}
|
1064 |
)
|
1065 |
|
1066 |
+
"website_monitor": Template(
|
1067 |
code="""
|
1068 |
+
import requests
|
1069 |
+
from bs4 import BeautifulSoup
|
1070 |
+
import time
|
1071 |
+
import os
|
1072 |
|
1073 |
+
def get_website_content(url):
|
1074 |
+
try:
|
1075 |
+
response = requests.get(url)
|
1076 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
1077 |
+
return response.text
|
1078 |
+
except requests.exceptions.RequestException as e:
|
1079 |
+
print(f"Error fetching website content: {e}")
|
1080 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
1081 |
|
1082 |
+
def parse_website_content(html):
|
1083 |
+
try:
|
1084 |
+
soup = BeautifulSoup(html, 'html.parser')
|
1085 |
+
return soup
|
1086 |
+
except Exception as e:
|
1087 |
+
print(f"Error parsing website content: {e}")
|
1088 |
+
return None
|
|
|
1089 |
|
1090 |
+
def extract_data(soup):
|
1091 |
+
try:
|
1092 |
+
# Implement data extraction logic here
|
1093 |
+
# For example, let's assume we want to extract the title of the webpage
|
1094 |
+
title = soup.find('title').text
|
1095 |
+
return title
|
1096 |
+
except Exception as e:
|
1097 |
+
print(f"Error extracting data: {e}")
|
1098 |
+
return None
|
1099 |
|
1100 |
+
def monitor_website(url, output_file):
|
1101 |
+
while True:
|
1102 |
+
html = get_website_content(url)
|
1103 |
+
if html:
|
1104 |
+
soup = parse_website_content(html)
|
1105 |
+
if soup:
|
1106 |
+
data = extract_data(soup)
|
1107 |
+
if data:
|
1108 |
+
with open(output_file, 'a') as f:
|
1109 |
+
f.write(data + '\n')
|
1110 |
+
print(f"Data extracted and written to {output_file}")
|
1111 |
+
time.sleep(60) # Monitor the website every 60 seconds
|
1112 |
+
|
1113 |
+
def main():
|
1114 |
+
url = input("Enter the website URL to monitor: ")
|
1115 |
+
output_file = input("Enter the output file path: ")
|
1116 |
+
monitor_website(url, output_file)
|
1117 |
+
|
1118 |
+
if __name__ == "__main__":
|
1119 |
+
main()
|
1120 |
""",
|
1121 |
description="Monitor the uptime and response time of a website",
|
1122 |
components=["Textbox", "Button", "JSON"],
|