acecalisto3 commited on
Commit
8a444d6
·
verified ·
1 Parent(s): 442c5e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
app.py CHANGED
@@ -1063,43 +1063,60 @@ if __name__ == "__main__":
1063
  metadata={"category": "utility"}
1064
  )
1065
 
1066
- "website_monitor": Template(
1067
  code="""
1068
- import gradio as gr
1069
- import requests
1070
- from datetime import datetime
 
1071
 
1072
- def monitor_website(url):
1073
- try:
1074
- response = requests.get(url)
1075
- status_code = response.status_code
1076
- status = "Up" if status_code == 200 else "Down"
1077
- return {
1078
- "url": url,
1079
- "status": status,
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
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
1087
- gr.Markdown("# Website Uptime Monitor")
1088
- with gr.Row():
1089
- with gr.Column():
1090
- url_input = gr.Textbox(label="Website URL", placeholder="https://example.com")
1091
- check_btn = gr.Button("Check Website")
1092
- with gr.Column():
1093
- result_output = gr.JSON(label="Monitoring Result")
1094
 
1095
- check_btn.click(
1096
- fn=monitor_website,
1097
- inputs=url_input,
1098
- outputs=result_output
1099
- )
 
 
 
 
1100
 
1101
- if __name__ == "__main__":
1102
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"],