Simplificada y mejorada la api :3

This commit is contained in:
2020-11-19 06:14:24 -03:00
parent 20c72e3af0
commit c5ad41a8ee
4 changed files with 149 additions and 16 deletions

View File

@@ -1,8 +1,16 @@
import requests
import copy
url = "https://api.openweathermap.org/data/2.5/weather/"
def get(city, key):
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,
@@ -11,3 +19,37 @@ def get(city, key):
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}@2x.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