Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Frontend/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,18 @@ if (scrollTopBtn) {
});
});
}

try {
const response = await fetch(`/weather?city=${city}&state=${state}&country=${country}`);
const data = await response.json();

if (!response.ok) {
document.getElementById("formErrors").textContent = data.error;
return;
}

// Show weather data if successful
console.log(data);
} catch (err) {
document.getElementById("formErrors").textContent = "Unexpected error. Please try again.";
}
33 changes: 33 additions & 0 deletions backend/alertsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import requests

from flask import request, jsonify
from backend import app
from dotenv import load_dotenv

load_dotenv()
Expand Down Expand Up @@ -697,6 +699,37 @@ def chatbot():
"Chatbot unavailable."
})



OPENWEATHER_API_KEY = "YOUR_KEY"

@app.route("/weather")
def weather():
city = request.args.get("city")
state = request.args.get("state")
country = request.args.get("country")

if not city or not country:
return jsonify({"error": "Missing required fields"}), 400

try:
url = f"http://api.openweathermap.org/data/2.5/weather?q={city},{state},{country}&appid={OPENWEATHER_API_KEY}"
resp = requests.get(url, timeout=5)

# Handle API response codes
if resp.status_code == 404:
return jsonify({"error": "City not found. Please check spelling."}), 404
elif resp.status_code != 200:
return jsonify({"error": "Weather service error. Try again later."}), resp.status_code

data = resp.json()
return jsonify(data)

except requests.exceptions.Timeout:
return jsonify({"error": "Weather service timed out. Please retry."}), 504
except requests.exceptions.RequestException:
return jsonify({"error": "Network failure. Please check your connection."}), 503

# =========================================================
# LOCAL RUN
# =========================================================
Expand Down