Spaces:
Sleeping
Sleeping
Update weather.py
Browse files- weather.py +23 -24
weather.py
CHANGED
|
@@ -1,25 +1,24 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import os
|
| 3 |
-
|
| 4 |
-
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY")
|
| 5 |
-
|
| 6 |
-
def get_current_weather(location: str) -> str:
|
| 7 |
-
url = "
|
| 8 |
-
params = {
|
| 9 |
-
"key": WEATHER_API_KEY,
|
| 10 |
-
"q": location,
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
response
|
| 15 |
-
response.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
except Exception as e:
|
| 25 |
return f"Error fetching weather: {str(e)}"
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY")
|
| 5 |
+
|
| 6 |
+
def get_current_weather(location: str) -> str:
|
| 7 |
+
url = "https://weatherapi-com.p.rapidapi.com/current.json?q=53.1%2C-0.13"
|
| 8 |
+
params = {
|
| 9 |
+
"key": WEATHER_API_KEY,
|
| 10 |
+
"q": location,
|
| 11 |
+
}
|
| 12 |
+
try:
|
| 13 |
+
response = requests.get(url, params=params, timeout=10)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
data = response.json()
|
| 16 |
+
location = data["location"]["name"]
|
| 17 |
+
temp_c = data["current"]["temp_c"]
|
| 18 |
+
condition = data["current"]["condition"]["text"]
|
| 19 |
+
humidity = data["current"]["humidity"]
|
| 20 |
+
wind_kph = data["current"]["wind_kph"]
|
| 21 |
+
return (f"Weather in {location}: {temp_c}°C, {condition}, "
|
| 22 |
+
f"Humidity: {humidity}%, Wind: {wind_kph} km/h")
|
| 23 |
+
except Exception as e:
|
|
|
|
| 24 |
return f"Error fetching weather: {str(e)}"
|