-
Notifications
You must be signed in to change notification settings - Fork 53
/
app.py
51 lines (41 loc) · 1.76 KB
/
app.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
44
45
46
47
48
49
50
51
from flask import Flask, redirect, url_for, render_template, request
import threading
import signal
import json
import os
import time
# Locals
from scripts import mongio, apis, settings
app = Flask(__name__)
# ======== Tasks =========================================================== #
def refresh_positions(exchanges):
E = []
for exchange in exchanges:
e = getattr(apis, exchange.title())
key = getattr(settings, '{0}_key'.format(exchange))
secret = getattr(settings, '{0}_secret'.format(exchange))
try:
passphrase = getattr(settings, '{0}_passphrase'.format(exchange))
E.append(e(key, secret, passphrase).get_balances())
except AttributeError:
E.append(e(key, secret).get_balances())
return sum(E).positions()
# ======== Routing =========================================================== #
@app.route('/', methods=['GET'])
def index():
if 'positions' not in mongio.db.collection_names():
mongio.db.create_collection('positions')
mongio.save(settings.mongo_portfolio, 'positions', [])
mongio.save(settings.mongo_portfolio, 'equity', {'btc':{}, 'usd':{}})
positions = mongio.load(settings.mongo_portfolio, 'positions')
equity = mongio.load(settings.mongo_portfolio, 'equity')
return render_template('index.html', p=positions, e=equity)
@app.route('/refresh', methods=['POST'])
def refresh():
positions = refresh_positions(settings.exchanges)
mongio.save(settings.mongo_portfolio, 'positions', positions)
positions = mongio.load(settings.mongo_portfolio, 'positions')
return json.dumps({'success': True, 'positions': positions})
# ======== Main ============================================================== #
if __name__ == "__main__":
app.run()