22import random
33import requests
44from bs4 import BeautifulSoup
5+ import concurrent .futures
56
67app = Flask (__name__ )
78
@@ -55,7 +56,7 @@ def fetch_real_time_best_practices():
5556 best_practices = []
5657 headers = {"User-Agent" : "Mozilla/5.0" }
5758
58- for url in FAO_URLS :
59+ def fetch_data ( url ) :
5960 try :
6061 response = requests .get (url , headers = headers , timeout = 10 )
6162 response .raise_for_status ()
@@ -64,21 +65,24 @@ def fetch_real_time_best_practices():
6465 titles = soup .find_all (["h2" , "h3" ], limit = 5 ) or []
6566 paragraphs = soup .find_all ("p" , limit = 5 ) or []
6667
67- for title , paragraph in zip ( titles , paragraphs ):
68- best_practices . append ( {
68+ return [
69+ {
6970 "title" : title .get_text (strip = True ),
7071 "details" : paragraph .get_text (strip = True ),
7172 "source" : url
72- })
73-
73+ }
74+ for title , paragraph in zip (titles , paragraphs )
75+ ]
7476 except requests .exceptions .RequestException as e :
75- best_practices .append ({
76- "title" : "Error Fetching Data" ,
77- "details" : str (e ),
78- "source" : url
79- })
77+ return [{"title" : "Error Fetching Data" , "details" : str (e ), "source" : url }]
78+
79+ with concurrent .futures .ThreadPoolExecutor () as executor :
80+ results = executor .map (fetch_data , FAO_URLS )
81+
82+ for result in results :
83+ best_practices .extend (result )
8084
81- return best_practices or [{"title" : "No Data" , "details" : "Could not retrieve best practices." , "source" : "N/A" }]
85+ return best_practices if best_practices else [{"title" : "No Data" , "details" : "Could not retrieve best practices." , "source" : "N/A" }]
8286
8387@app .route ("/api/dashboard" )
8488def api_dashboard ():
@@ -91,6 +95,10 @@ def api_dashboard():
9195 "web_best_practices" : web_best_practices
9296 })
9397
98+ @app .route ("/api/sensor" )
99+ def api_sensor ():
100+ return jsonify (get_sensor_data ())
101+
94102@app .route ("/" )
95103def home ():
96104 sensor_data = get_sensor_data ()
@@ -102,4 +110,4 @@ def home():
102110 web_best_practices = web_best_practices )
103111
104112if __name__ == "__main__" :
105- app .run (host = "20.48.204.5 " , port = 8000 , debug = True )
113+ app .run (host = "0.0.0.0 " , port = 8000 , debug = True )
0 commit comments