Spaces:
Runtime error
Runtime error
Create weather_utils.py
Browse files- weather_utils.py +29 -0
weather_utils.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
API_KEY = "YOUR_API_KEY" # Replace this with your actual OpenWeatherMap API key
|
4 |
+
|
5 |
+
def get_emoji(condition):
|
6 |
+
condition = condition.lower()
|
7 |
+
if "rain" in condition:
|
8 |
+
return "🌧️"
|
9 |
+
elif "cloud" in condition:
|
10 |
+
return "☁️"
|
11 |
+
elif "clear" in condition or "sun" in condition:
|
12 |
+
return "☀️"
|
13 |
+
else:
|
14 |
+
return "🌈"
|
15 |
+
|
16 |
+
def get_weather_by_coordinates(lat, lon):
|
17 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}&units=metric"
|
18 |
+
response = requests.get(url)
|
19 |
+
|
20 |
+
if response.status_code != 200:
|
21 |
+
return "❌ Could not fetch weather data"
|
22 |
+
|
23 |
+
data = response.json()
|
24 |
+
temp = data["main"]["temp"]
|
25 |
+
condition = data["weather"][0]["description"]
|
26 |
+
emoji = get_emoji(condition)
|
27 |
+
|
28 |
+
location = data["name"] or "Selected Area"
|
29 |
+
return f"📍 **Location:** {location}\n🌡️ **Temperature:** {temp}°C\n📌 **Condition:** {condition} {emoji}"
|