Simplificada y mejorada la api :3
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from flask import Flask
|
from flask import Flask, request
|
||||||
from . import services
|
from . import services
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -6,16 +6,19 @@ app.config.from_object('home.settings')
|
|||||||
|
|
||||||
@app.route('/currency')
|
@app.route('/currency')
|
||||||
def currency():
|
def currency():
|
||||||
return services.currency.get(app.config['FOREX_KEY'])
|
base = request.args.get('base', 'CLP')
|
||||||
|
return services.currency.rates(base, app.config['FIXER_KEY'])
|
||||||
|
|
||||||
@app.route('/weather')
|
@app.route('/weather')
|
||||||
def weather():
|
def weather():
|
||||||
return services.weather.get('temuco', app.config['OPENWEATHERMAP_KEY'])
|
city = request.args.get('city', 'temuco')
|
||||||
|
return services.weather.get(city, app.config['OPENWEATHERMAP_KEY'])
|
||||||
|
|
||||||
@app.route('/photos')
|
@app.route('/photos')
|
||||||
def photos():
|
def photos():
|
||||||
return services.photos.get(app.config['UNSPLASH_KEY'])
|
page = request.args.get('page', 1)
|
||||||
|
return services.photos.get(app.config['UNSPLASH_KEY'], page)
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello_world():
|
def hey():
|
||||||
return 'Hello, World!'
|
return 'Hey, esta es la api men :c!'
|
||||||
|
|||||||
@@ -1,12 +1,50 @@
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
url = "https://v2.api.forex/rates/latest.json"
|
url = "http://data.fixer.io/api/{0}"
|
||||||
|
|
||||||
def get(key):
|
def query_rates(key):
|
||||||
data = {
|
"""
|
||||||
'key': key,
|
Llama a la api de fixer para obtener los rates del dinero
|
||||||
}
|
Esta informacion esta en base al euro
|
||||||
|
|
||||||
|
No se hace ninguna validacion porque... no quiero
|
||||||
|
"""
|
||||||
|
data = {'access_key': key}
|
||||||
|
response = requests.get(url.format('latest'), params=data)
|
||||||
|
return response.json().get('rates')
|
||||||
|
|
||||||
|
def query_symbols(key):
|
||||||
|
"""
|
||||||
|
Llama a la api de fixer para obtener el significado
|
||||||
|
de los simbolos de 3 letras de cada moneda
|
||||||
|
|
||||||
|
No se hace ninguna validacion porque... no quiero
|
||||||
|
"""
|
||||||
|
data = {'access_key': key}
|
||||||
|
response = requests.get(url.format('symbols'), params=data)
|
||||||
|
return response.json().get('symbols')
|
||||||
|
|
||||||
|
def to_base(base, raw):
|
||||||
|
"""
|
||||||
|
Transforma desde la base del euro a otra moneda
|
||||||
|
"""
|
||||||
|
base = raw.get(base)
|
||||||
|
|
||||||
|
transformed = {}
|
||||||
|
for k, v in raw.items():
|
||||||
|
transformed[k] = raw[k] / base
|
||||||
|
|
||||||
|
return transformed
|
||||||
|
|
||||||
|
|
||||||
|
def rates(base, key):
|
||||||
|
"""
|
||||||
|
Junta todo bien bonito para enviarlo al que llame la api
|
||||||
|
"""
|
||||||
|
rates = query_rates(key)
|
||||||
|
other_base= to_base(base, rates)
|
||||||
|
symbols = query_symbols(key)
|
||||||
|
|
||||||
|
return {'rates' : other_base, 'symbols': symbols}
|
||||||
|
|
||||||
response = requests.get(url, params=data)
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,61 @@ import requests
|
|||||||
|
|
||||||
url = "https://api.unsplash.com/photos/"
|
url = "https://api.unsplash.com/photos/"
|
||||||
|
|
||||||
def get(key):
|
def query_api(key, page):
|
||||||
|
"""
|
||||||
|
Llama a la api de unsplash para obtener 20 fotos
|
||||||
|
en orden de popularidad
|
||||||
|
"""
|
||||||
data = {
|
data = {
|
||||||
'client_id': key,
|
'client_id': key,
|
||||||
|
'per_page': 20,
|
||||||
|
'page': page,
|
||||||
|
'order_by': 'popular',
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(url, params=data)
|
response = requests.get(url, params=data)
|
||||||
return {'result': response.json()}
|
return response.json()
|
||||||
|
|
||||||
|
def simplify_data(photos):
|
||||||
|
"""
|
||||||
|
Simplifica las informacion que entrega en las fotos
|
||||||
|
para tener solo lo mas basico necesario
|
||||||
|
|
||||||
|
el titulo alternativo de la foto, los links mas importantes
|
||||||
|
de la foto y los creditos al autor
|
||||||
|
"""
|
||||||
|
simple = []
|
||||||
|
|
||||||
|
for photo in photos:
|
||||||
|
first_name = photo['user'].get('first_name')
|
||||||
|
last_name = photo['user'].get('last_name')
|
||||||
|
|
||||||
|
if last_name is None:
|
||||||
|
last_name = ''
|
||||||
|
|
||||||
|
full_name = ' '.join([first_name, last_name])
|
||||||
|
|
||||||
|
simple.append({
|
||||||
|
'alt': photo['alt_description'],
|
||||||
|
'urls': {
|
||||||
|
'full': photo['urls']['full'],
|
||||||
|
'small': photo['urls']['small'],
|
||||||
|
},
|
||||||
|
'credit': {
|
||||||
|
'name': full_name,
|
||||||
|
'url': photo['user']['links']['html']
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return simple
|
||||||
|
|
||||||
|
def get(key, page):
|
||||||
|
"""
|
||||||
|
Junta todo bien bonito para enviarlo alq ue llame la api
|
||||||
|
"""
|
||||||
|
photos = query_api(key, page)
|
||||||
|
simple = simplify_data(photos)
|
||||||
|
return {'results': simple}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
import requests
|
import requests
|
||||||
|
import copy
|
||||||
|
|
||||||
url = "https://api.openweathermap.org/data/2.5/weather/"
|
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 = {
|
data = {
|
||||||
'q': city,
|
'q': city,
|
||||||
'appid': key,
|
'appid': key,
|
||||||
@@ -11,3 +19,37 @@ def get(city, key):
|
|||||||
response = requests.get(url, params=data)
|
response = requests.get(url, params=data)
|
||||||
return response.json()
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user