Movido proyecto a subdirectorio para acomodar el frontend aqui
This commit is contained in:
56
backend/home/services/weather.py
Normal file
56
backend/home/services/weather.py
Normal file
@@ -0,0 +1,56 @@
|
||||
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 {
|
||||
'name': weather['name'],
|
||||
'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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user