forked from CloudLabsAI-Azure/Code-Generation-Refactoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_script.py
More file actions
29 lines (23 loc) · 950 Bytes
/
weather_script.py
File metadata and controls
29 lines (23 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
def obtener_clima(lat, lon, api_key):
url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,daily,alerts&appid={api_key}&units=metric"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
def mostrar_clima(clima):
if clima:
temp_actual = clima['current']['temp']
descripcion = clima['current']['weather'][0]['description']
print(f"La temperatura actual es {temp_actual}°C con {descripcion}.")
else:
print("No se pudo obtener el clima.")
def recuperar_datos_clima():
lat = float(input("Introduce la latitud: "))
lon = float(input("Introduce la longitud: "))
api_key = input("Introduce tu API key de OpenWeatherMap: ")
clima = obtener_clima(lat, lon, api_key)
mostrar_clima(clima)
if __name__ == "__main__":
recuperar_datos_clima()