Created
May 9, 2019 19:55
-
-
Save paduel/a4c94ff2560a80de43753a3ae224fd72 to your computer and use it in GitHub Desktop.
Un breve codigo para descargar el valor de Pi en la divisa deseada de forma continua usando callback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import get | |
import threading | |
from time import sleep | |
# Función que usa un llamada para tomar la información en formato json de la api pública | |
def get_pi(callback=None, base='EUR', wait_time=1, verbose=False): | |
while True: | |
response = get( | |
f'https://api.piexchange.io/v1/public/instrument/ticker/PIT{base}') | |
info_dict = response.json() | |
if verbose: | |
print(info_dict) | |
# Si se ha definido una función callback la llama | |
if callback: | |
callback(info_dict) | |
sleep(wait_time) | |
# Función callback de ejemplo, puede ser sustituida por cualquier otra. | |
def valor(info_dict): | |
print(f'Valor de Pi en {info_dict["symbol"][-3:]}: {info_dict["price"]}\nVolumen {info_dict["volume"]}\n' | |
f'Última actualización {info_dict["updated_at"]}\n') | |
if __name__ == "__main__": | |
# Creamos un hilo como daemon y lo dejamos corriendo indefinidamente. | |
hilo = threading.Thread(target=get_pi, daemon=True, args=(valor, 'USD')) | |
hilo.start() | |
hilo.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment