uso de funções csv.DictReader() e csv.DictWriter() para ler e escrever ficheiros csv
This commit is contained in:
@ -1,14 +1,33 @@
|
||||
import requests
|
||||
import time
|
||||
# import datetime
|
||||
|
||||
baseURL = 'https://api.coingecko.com/api/v3/'
|
||||
base_url = 'https://api.coingecko.com/api/v3/'
|
||||
coin = 'bitcoin'
|
||||
vs_currency = 'eur'
|
||||
days = '30'
|
||||
days = '365'
|
||||
precision = '3'
|
||||
intervalo = 'daily'
|
||||
|
||||
ohlc_endpoint = baseURL + 'coins/' + coin + '/ohlc?vs_currency=' + vs_currency + '&days=' + days + '&precision=' + precision
|
||||
historico_precos_header = [
|
||||
"data",
|
||||
"moeda",
|
||||
"preco_abertura",
|
||||
"preco_maximo",
|
||||
"preco_minimo",
|
||||
"preco_fecho"
|
||||
]
|
||||
|
||||
portfolio_headers = [
|
||||
"data",
|
||||
"moeda",
|
||||
"quantidade",
|
||||
"movimento"
|
||||
]
|
||||
|
||||
# https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=eur&days=365&interval=daily
|
||||
url_historico_preco = base_url + 'coins/' + coin + '/market_chart?vs_currency=' + vs_currency + '&days=' + days + '&interval=' + intervalo + '&precision=' + precision
|
||||
# https://api.coingecko.com/api/v3/coins/bitcoin/ohlc?vs_currency=eur&days=30&precision=3
|
||||
url_preco_ohlc = base_url + 'coins/' + coin + '/ohlc?vs_currency=' + vs_currency + '&days=' + days + '&precision=' + precision
|
||||
|
||||
APY_KEY = "CG-K5RS5VXsdFDip2UvY3z8VjQP"
|
||||
|
||||
@ -17,17 +36,25 @@ headers = {
|
||||
'x-cg-demo-api-key': APY_KEY
|
||||
}
|
||||
|
||||
#print(ohlc_endpoint)
|
||||
# tipo de pesquisa
|
||||
url_consulta = url_historico_preco
|
||||
|
||||
response = requests.get(ohlc_endpoint, headers= headers)
|
||||
response = requests.get(url_consulta, headers= headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
print(len(data))
|
||||
date = time.ctime(data[0][0]/1000)
|
||||
date_as_string = time.strftime("%d/%m/%Y", time.gmtime(data[0][0]/1000))
|
||||
ohlc = [ data[0][1], data[0][2], data[0][3], data[0][4] ]
|
||||
print(len(data['prices']))
|
||||
#print(data)
|
||||
|
||||
for index in range(0, len(data['prices'])):
|
||||
#date = time.ctime(data['prices'][index][0]/1000)
|
||||
date_as_string = time.strftime("%d/%m/%Y", time.gmtime(data['prices'][index][0]/1000))
|
||||
price = data['prices'][index][1]
|
||||
print(f'{date_as_string} -> {price}')
|
||||
|
||||
# ohlc = [ data[0][1], data[0][2], data[0][3], data[0][4] ]
|
||||
|
||||
# print(f'price of bitcoin in {date_as_string}:\nopen: {ohlc[0]}\nhigh: {ohlc[1]}\nlow: {ohlc[2]}\nclose: {ohlc[3]}')
|
||||
|
||||
print(f'price of bitcoin in {date_as_string}:\nopen: {ohlc[0]}\nhigh: {ohlc[1]}\nlow: {ohlc[2]}\nclose: {ohlc[3]}')
|
||||
else:
|
||||
print('failed to retrive data')
|
||||
print('failed to retrive data')
|
||||
|
||||
Reference in New Issue
Block a user