-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
43 lines (38 loc) · 1.29 KB
/
api.py
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
from flask import Flask, render_template, request, flash, redirect, url_for, jsonify
from binance.client import Client
from binance.enums import *
import os
app = Flask(__name__, static_folder='build', static_url_path='/')
app.config['SECRET_KEY'] = "azeqqzdsrgsfqzdsefsfsdqzgxgsefsdwxc"
API_KEY = os.getenv('API_KEY')
API_SECRET = os.environ.get('API_SECRET')
client = Client(API_KEY, API_SECRET)
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/api/history/<symbol>/<timeframe>')
def history(symbol, timeframe):
timeframe_periods = {
"1m": "6 hours ago UTC",
"5m": "18 hours ago UTC",
"15m": "90 hours ago UTC",
"1h": "15 days ago UTC",
"1d": "360 days ago UTC"
}
try:
# Fetch candles
candles = client.get_historical_klines(symbol, timeframe, timeframe_periods.get(timeframe))
# Map response
processed_candles = []
for data in candles:
candle = {
'time': data[0]/1000,
'open': data[1],
'high': data[2],
'low': data[3],
'close': data[4]
}
processed_candles.append(candle)
return jsonify(processed_candles)
except:
return jsonify({"errorStatus": True})