From c5ad41a8ee5af9ead286b8e2238e6338044ae688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Cort=C3=A9s?= Date: Thu, 19 Nov 2020 06:14:24 -0300 Subject: [PATCH] Simplificada y mejorada la api :3 --- home/entry.py | 15 ++++++----- home/services/currency.py | 52 ++++++++++++++++++++++++++++++++----- home/services/photos.py | 54 +++++++++++++++++++++++++++++++++++++-- home/services/weather.py | 44 ++++++++++++++++++++++++++++++- 4 files changed, 149 insertions(+), 16 deletions(-) diff --git a/home/entry.py b/home/entry.py index 06827c8..dccdd95 100644 --- a/home/entry.py +++ b/home/entry.py @@ -1,4 +1,4 @@ -from flask import Flask +from flask import Flask, request from . import services app = Flask(__name__) @@ -6,16 +6,19 @@ app.config.from_object('home.settings') @app.route('/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') 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') 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('/') -def hello_world(): - return 'Hello, World!' +def hey(): + return 'Hey, esta es la api men :c!' diff --git a/home/services/currency.py b/home/services/currency.py index ec1080e..556330c 100644 --- a/home/services/currency.py +++ b/home/services/currency.py @@ -1,12 +1,50 @@ import requests -url = "https://v2.api.forex/rates/latest.json" +url = "http://data.fixer.io/api/{0}" -def get(key): - data = { - 'key': key, - } +def query_rates(key): + """ + Llama a la api de fixer para obtener los rates del dinero + Esta informacion esta en base al euro - response = requests.get(url, params=data) - return response.json() + 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} + diff --git a/home/services/photos.py b/home/services/photos.py index 7fa8abb..ae26a46 100644 --- a/home/services/photos.py +++ b/home/services/photos.py @@ -2,11 +2,61 @@ import requests 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 = { 'client_id': key, + 'per_page': 20, + 'page': page, + 'order_by': 'popular', } 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} + + diff --git a/home/services/weather.py b/home/services/weather.py index 79adbcb..67eb46b 100644 --- a/home/services/weather.py +++ b/home/services/weather.py @@ -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 + +