adicionada funcao 'consultar_preco()' para o endpoint '/simple/price'
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
# . documentação API: https://docs.coingecko.com/v3.0.1/reference/introduction
|
# . documentação API: https://docs.coingecko.com/v3.0.1/reference/introduction
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
CHAVE_API = 'CG-K5RS5VXsdFDip2UvY3z8VjQP'
|
CHAVE_API = 'CG-K5RS5VXsdFDip2UvY3z8VjQP'
|
||||||
headers = {
|
headers = {
|
||||||
@ -18,30 +19,92 @@ url_raiz_API = 'https://api.coingecko.com/api/v3/'
|
|||||||
|
|
||||||
#url_consulta_ohlc = 'coins/' + criptomoeda + '/ohlc?vs_currency=' + vs_currency + '&days=' + dias + '&precision=' + precisao
|
#url_consulta_ohlc = 'coins/' + criptomoeda + '/ohlc?vs_currency=' + vs_currency + '&days=' + dias + '&precision=' + precisao
|
||||||
|
|
||||||
|
# https://docs.coingecko.com/v3.0.1/reference/ping-server
|
||||||
# verificar o estado do servidor API
|
# verificar o estado do servidor API
|
||||||
def ping() -> tuple:
|
def ping() -> tuple:
|
||||||
endpoint_api = 'ping'
|
endpoint_api = 'ping'
|
||||||
url_pedido = url_raiz_API + endpoint_api
|
url_pedido = url_raiz_API + endpoint_api
|
||||||
resposta = requests.get(url_pedido, headers=headers)
|
resposta = requests.get(url_pedido, headers=headers)
|
||||||
return resposta.status_code, resposta.json()['gecko_says']
|
return resposta.status_code, resposta.json()['gecko_says']
|
||||||
# debug
|
# debug (decomentar linhas seguintes para testar funcao)
|
||||||
# codigo, dados = ping()
|
# codigo, dados = ping()
|
||||||
# print(f'codigo: {codigo}')
|
# print(f'codigo: {codigo}')
|
||||||
# print(f'resposta: {dados}')
|
# print(f'resposta: {dados}')
|
||||||
|
|
||||||
|
# https://docs.coingecko.com/v3.0.1/reference/simple-price
|
||||||
|
# consultar preco de uma ou mais moedas usando o seu ID de Moeda API
|
||||||
|
def consultar_preco(id_criptomoeda: str = '',
|
||||||
|
vs_currency: str = '',
|
||||||
|
flag_include_market_cap: bool = False,
|
||||||
|
flag_include_24hr_vol: bool = False,
|
||||||
|
flag_include_24hr_change: bool = False,
|
||||||
|
flag_include_last_updated_at: bool = False,
|
||||||
|
precisao: int | str = 'full'
|
||||||
|
) -> tuple:
|
||||||
|
# validacao de parametros
|
||||||
|
if not isinstance(id_criptomoeda, str):
|
||||||
|
raise TypeError('\'id_criptomoeda\' não é do tipo \'str\'')
|
||||||
|
if id_criptomoeda == '':
|
||||||
|
raise ValueError('\'id_criptomoeda\' tem de ser definido')
|
||||||
|
if not isinstance(vs_currency, str):
|
||||||
|
raise TypeError('\'vs_currency\' não é do tipo \'str\'')
|
||||||
|
if vs_currency == '':
|
||||||
|
raise ValueError('')
|
||||||
|
if not isinstance(flag_include_market_cap, bool):
|
||||||
|
flag_include_market_cap = False
|
||||||
|
if not isinstance(flag_include_24hr_vol, bool):
|
||||||
|
flag_include_24hr_vol = False
|
||||||
|
if not isinstance(flag_include_24hr_change, bool):
|
||||||
|
flag_include_24hr_change = False
|
||||||
|
if not isinstance(flag_include_last_updated_at, bool):
|
||||||
|
flag_include_last_updated_at = False
|
||||||
|
if (not isinstance(precisao, int)) and (not isinstance(precisao, str)):
|
||||||
|
raise TypeError('\'precisao\' não é do tipo \'int\' ou \'str\'')
|
||||||
|
if isinstance(precisao, int):
|
||||||
|
if precisao < 0:
|
||||||
|
precisao = 0
|
||||||
|
if precisao > 18:
|
||||||
|
precisao = 18
|
||||||
|
precisao = str(precisao)
|
||||||
|
|
||||||
|
endpoint_api = 'simple/price?' + 'ids=' + id_criptomoeda + \
|
||||||
|
'&vs_currencies=' + vs_currency + \
|
||||||
|
'&include_market_cap=' + str(flag_include_market_cap).lower() + \
|
||||||
|
'&include_24hr_vol=' + str(flag_include_24hr_vol).lower() + \
|
||||||
|
'&include_24hr_change=' + str(flag_include_24hr_change).lower() + \
|
||||||
|
'&include_last_updated_at=' + str(flag_include_last_updated_at).lower() + \
|
||||||
|
'&precision=' + precisao
|
||||||
|
url_pedido = url_raiz_API + endpoint_api
|
||||||
|
resposta = requests.get(url_pedido, headers=headers)
|
||||||
|
return resposta.status_code, resposta.json()
|
||||||
|
# debug (decomentar linhas seguintes para testar funcao)
|
||||||
|
# codigo, dados_dict = consultar_preco('bitcoin', 'eur', precisao=0)
|
||||||
|
# print(f'codigo: {codigo}')
|
||||||
|
# for item in dados_dict:
|
||||||
|
# print(f'{dados_dict[item]}')
|
||||||
|
# print(dados)
|
||||||
|
|
||||||
# fazer consulta de dados OHLC
|
# fazer consulta de dados OHLC
|
||||||
def consulta_ohcl(criptomoeda: str, vs_currency: str = 'eur', dias: int | str = 30, precisao: int | str = 3) -> tuple:
|
def consulta_ohcl(criptomoeda: str = '',
|
||||||
|
vs_currency: str = '',
|
||||||
|
dias: int | str = 30,
|
||||||
|
precisao: int | str = 3
|
||||||
|
) -> tuple:
|
||||||
# validacao de parametros
|
# validacao de parametros
|
||||||
if not isinstance(criptomoeda, str):
|
if not isinstance(criptomoeda, str):
|
||||||
raise ValueError('\'criptomoeda\' não é do tipo \'str\'')
|
raise TypeError('\'criptomoeda\' não é do tipo \'str\'')
|
||||||
|
if criptomoeda == '':
|
||||||
|
raise ValueError('\'criptomoeda\' tem de ser definido')
|
||||||
if not isinstance(vs_currency, str):
|
if not isinstance(vs_currency, str):
|
||||||
raise ValueError('\'vs_currency\' não é do tipo \'str\'')
|
raise TypeError('\'vs_currency\' não é do tipo \'str\'')
|
||||||
|
if vs_currency == '':
|
||||||
|
raise ValueError('\'vs_currency\' tem de ser definido')
|
||||||
if (not isinstance(dias, str)) and (not isinstance(dias, int)):
|
if (not isinstance(dias, str)) and (not isinstance(dias, int)):
|
||||||
raise ValueError('\'dias\' não é do tipo \'str\' ou \'int\'')
|
raise TypeError('\'dias\' não é do tipo \'str\' ou \'int\'')
|
||||||
if isinstance(dias, int):
|
if isinstance(dias, int):
|
||||||
dias = str(dias)
|
dias = str(dias)
|
||||||
if (not isinstance(precisao, str)) and (not isinstance(precisao, int)):
|
if (not isinstance(precisao, str)) and (not isinstance(precisao, int)):
|
||||||
raise ValueError('\'precisao\' não é do tipo \'str\' ou \'int\'')
|
raise TypeError('\'precisao\' não é do tipo \'str\' ou \'int\'')
|
||||||
if isinstance(precisao, int):
|
if isinstance(precisao, int):
|
||||||
precisao = str(precisao)
|
precisao = str(precisao)
|
||||||
|
|
||||||
@ -49,8 +112,8 @@ def consulta_ohcl(criptomoeda: str, vs_currency: str = 'eur', dias: int | str =
|
|||||||
url_pedido = url_raiz_API + endpoint_api
|
url_pedido = url_raiz_API + endpoint_api
|
||||||
resposta = requests.get(url_pedido, headers=headers)
|
resposta = requests.get(url_pedido, headers=headers)
|
||||||
return resposta.status_code, resposta.json()
|
return resposta.status_code, resposta.json()
|
||||||
# debug
|
# debug (decomentar linhas seguintes para testar funcao)
|
||||||
# codigo, dados = consulta_ohcl('bitcoin')
|
# codigo, dados = consulta_ohcl('dogecoin', 'eur', precisao=5)
|
||||||
# print(f'codigo: {codigo}')
|
# print(f'codigo: {codigo}')
|
||||||
# for item in dados:
|
# for item in dados:
|
||||||
# print(item)
|
# print(item)
|
||||||
|
|||||||
Reference in New Issue
Block a user