Files
pagina-de-incio-universidad/home/services/weather.py

56 lines
1.2 KiB
Python

import requests
import copy
url = "https://api.openweathermap.org/data/2.5/weather/"
def query_weather(city, key):
"""
Llama a la api de openweathermap para obtener
el clima en el instante de llamar la api
Necesita que le entregen la ciudad, que puede ser
en forma "ciudad" o "ciudad, pais"
"""
data = {
'q': city,
'appid': key,
'units': 'metric',
}
response = requests.get(url, params=data)
return response.json()
def with_icon_url(weather):
"""
Transforma el codigo de icono que indica el clima
en el url correspondiente del icono
"""
url = 'http://openweathermap.org/img/wn/{0}@4x.png'
icon_code = weather['weather'][0]['icon']
modified = copy.deepcopy(weather)
modified['weather'][0]['url'] = url.format(icon_code)
return modified
def simplify_response(weather):
"""
Simplifica infinitamente lo que muestra la api :3
"""
return {
'temp': weather['main']['temp'],
'icon': weather['weather'][0]['url']
}
def get(city, key):
"""
Junta todo lo anterior asi bien nice
"""
weather = query_weather(city, key)
with_icon = with_icon_url(weather)
simple = simplify_response(with_icon)
return simple