Skip to content

Commit 4d6b591

Browse files
authored
Update EOAgriTool.py
Updated
1 parent f57b9dc commit 4d6b591

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

EOAgriTool.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33
import requests
44
from bs4 import BeautifulSoup
5+
import concurrent.futures
56

67
app = 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")
8488
def 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("/")
95103
def home():
96104
sensor_data = get_sensor_data()

0 commit comments

Comments
 (0)