-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmon.py
More file actions
79 lines (63 loc) · 1.88 KB
/
mon.py
File metadata and controls
79 lines (63 loc) · 1.88 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
import os
import signal
import sys
import threading
import util
from db import money_db
from client import plaid_client
from flask import Flask, jsonify, render_template
host = os.getenv('MONEY_HOST', '127.0.0.1')
port = os.getenv('MONEY_PORT', 8888)
money_database = os.getenv('MONEY_DB')
db_key = os.getenv('DB_KEY')
plaid_id = os.getenv('PLAID_CLIENT_ID')
plaid_pubkey = os.getenv('PLAID_PUBLIC_KEY')
plaid_secret = os.getenv('PLAID_SECRET')
mdb = money_db(db_key, money_database)
mdb.connect()
pc = plaid_client(plaid_id, plaid_secret, plaid_pubkey)
pc.connect()
refreshes = []
def refresh_thread():
keys = mdb.get_keys()
for key in keys:
bal = pc.get_balance(key)
if bal is None:
continue
for a in bal['accounts']:
account = util.prep_account(a, key[0])
mdb.add_account(account)
bals = util.prep_balances(a)
for b in bals:
mdb.add_balance(b)
mdb.encrypt()
def handler(signal, frame):
print('closing down')
mdb.disconnect()
sys.exit(0)
signal.signal(signal.SIGINT, handler)
app = Flask(__name__, static_url_path='', static_folder='static')
@app.route('/')
def index():
data = 'sup'
return render_template('index.ejs', data=data)
@app.route('/accounts')
def accounts():
accounts = mdb.get_accounts();
return jsonify(accounts)
@app.route('/refresh')
def refresh():
if len(refreshes) > 0:
if refreshes[0].is_alive():
return jsonify('refresh in progress. please wait')
else:
del refreshes[0]
t = threading.Thread(target=refresh_thread)
refreshes.append(t)
t.start()
return jsonify('ok')
@app.route('/balance/<balance_type>/<account_id>')
def balance(balance_type, account_id):
bal = mdb.get_balance_by_account(balance_type, account_id)
return jsonify(util.prep_balance(bal))
app.run(host=host, port=port)