-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
151 lines (132 loc) · 5.92 KB
/
Copy pathapp.py
File metadata and controls
151 lines (132 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, render_template, jsonify
from api_client import get_buda_balance, get_binance_balance, get_cryptomkt_balance, get_notbank_balance, get_prices_from_binance
import json
import requests
app = Flask(__name__)
# Intentar cargar configuración real, si no existe usar mock
try:
from config import API_KEYS
print("Usando API keys reales desde config.py")
except ImportError:
# Mock API keys para desarrollo/pruebas
API_KEYS = {
"buda": {"apiKey": "buda_key", "apiSecret": "buda_secret"},
"binance": {"apiKey": "binance_key", "apiSecret": "binance_secret"},
"cryptomkt": {"apiKey": "cryptomkt_key", "apiSecret": "cryptomkt_secret"},
"notbank": {"apiKey": "notbank_key", "apiSecret": "notbank_secret", "userId": "test_user", "accountId": "test_account"}
}
print("Usando API keys mock para desarrollo")
@app.route('/')
def index():
buda_balances = get_buda_balance(API_KEYS['buda']['apiKey'], API_KEYS['buda']['apiSecret'])
if buda_balances is None:
buda_balances = {}
binance_balances = get_binance_balance(API_KEYS['binance']['apiKey'], API_KEYS['binance']['apiSecret'])
if binance_balances is None:
binance_balances = {}
cryptomkt_balances = get_cryptomkt_balance(API_KEYS['cryptomkt']['apiKey'], API_KEYS['cryptomkt']['apiSecret'])
if cryptomkt_balances is None:
cryptomkt_balances = {}
notbank_balances = get_notbank_balance(API_KEYS['notbank']['apiKey'], API_KEYS['notbank']['apiSecret'], API_KEYS['notbank']['userId'], API_KEYS['notbank']['accountId'])
if notbank_balances is None:
notbank_balances = {}
all_balances = {
'Buda': buda_balances,
'Binance': binance_balances,
'CryptoMKT': cryptomkt_balances,
'NotBank': notbank_balances
}
prices_usd = get_prices_from_binance()
total_portfolio = {}
for exchange, balances in all_balances.items():
for currency, amount in balances.items():
currency_code = currency.split('-')[0].upper()
total_portfolio[currency_code] = total_portfolio.get(currency_code, 0) + amount
total_portfolio_usd_value = 0
for currency, total_amount in total_portfolio.items():
price_symbol_usdt = f"{currency}USDT"
price_symbol_busd = f"{currency}BUSD"
price = prices_usd.get(price_symbol_usdt)
if price is None:
price = prices_usd.get(price_symbol_busd)
if price:
total_portfolio_usd_value += total_amount * price
return render_template('index.html',
all_balances=all_balances,
total_portfolio=total_portfolio,
total_portfolio_usd_value=total_portfolio_usd_value,
prices_usd=prices_usd)
@app.route('/api/refresh_data')
def refresh_data():
"""Endpoint para actualizar datos sin recargar la página"""
try:
# Obtener balances
buda_balances = get_buda_balance(API_KEYS['buda']['apiKey'], API_KEYS['buda']['apiSecret'])
if buda_balances is None:
buda_balances = {}
binance_balances = get_binance_balance(API_KEYS['binance']['apiKey'], API_KEYS['binance']['apiSecret'])
if binance_balances is None:
binance_balances = {}
notbank_balances = get_notbank_balance(API_KEYS['notbank']['apiKey'], API_KEYS['notbank']['apiSecret'], API_KEYS['notbank']['userId'], API_KEYS['notbank']['accountId'])
if notbank_balances is None:
notbank_balances = {}
all_balances = {
'Buda': buda_balances,
'Binance': binance_balances,
'NotBank': notbank_balances
}
# Obtener precios
prices_usd = get_prices_from_binance()
# Calcular portfolio total
total_portfolio = {}
for exchange, balances in all_balances.items():
for currency, amount in balances.items():
currency_code = currency.split('-')[0].upper()
total_portfolio[currency_code] = total_portfolio.get(currency_code, 0) + amount
total_portfolio_usd_value = 0
for currency, total_amount in total_portfolio.items():
price_symbol_usdt = f"{currency}USDT"
price_symbol_busd = f"{currency}BUSD"
price = prices_usd.get(price_symbol_usdt)
if price is None:
price = prices_usd.get(price_symbol_busd)
if price:
total_portfolio_usd_value += total_amount * price
return jsonify({
'success': True,
'all_balances': all_balances,
'total_portfolio': total_portfolio,
'total_portfolio_usd_value': total_portfolio_usd_value,
'prices_usd': prices_usd
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/historical_data/<symbol>')
def historical_data(symbol):
# Binance API endpoint for historical klines (candlestick data)
# We will get daily data for the last 30 days
url = f"https://api.binance.com/api/v3/klines?symbol={symbol.upper()}USDT&interval=1d&limit=30"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
# Format data for Chart.js
# [ [timestamp, open, high, low, close, volume, ...], ... ]
# We only need timestamp and close price
formatted_data = {
'labels': [item[0] for item in data],
'datasets': [{
'label': f'{symbol.upper()} Price (USD)',
'data': [float(item[4]) for item in data],
'borderColor': '#3498DB',
'tension': 0.1
}]
}
return jsonify(formatted_data)
except requests.exceptions.RequestException as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, port=5001)