-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmch.py
65 lines (54 loc) · 2.19 KB
/
mch.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import argparse
import json
from account import Account
parser = argparse.ArgumentParser(description='MyCryptoHoldings [MCH]')
parser.add_argument('-i', '--input', type=str, default='./cryptos.json',
help='input json (default: \"./cryptos.json\")')
parser.add_argument('-j', '--json', action='store_true',
help='print a json object')
parser.add_argument('-p', '--pretty', action='store_true', help='table print')
def total_holdings(accounts):
outobj = {}
outobj["coins"] = {}
outobj["total_usd"] = 0
for account in accounts:
account.fill_balance()
account.fill_usd_balance()
key = str(account.currency)
if key in outobj["coins"]:
outobj["coins"][key]["usd"] += account.balance_usd
outobj["coins"][key]["balance"] += account.balance
else:
outobj["coins"][key] = {}
outobj["coins"][key]["usd"] = account.balance_usd
outobj["coins"][key]["balance"] = account.balance
outobj["total_usd"] += account.balance_usd
return outobj
def print_holdings(holdings):
print('TOTAL HOLDINGS: %.2f usd' % holdings["total_usd"])
for key, value in holdings["coins"].items():
print('%s: %.8f (%.2f usd)' % (key, value["balance"], value["usd"]))
def pretty_print_holdings(holdings):
from terminaltables import SingleTable
table_data = [["CURRENCY", "BALANCE", "USD"]]
for key, value in holdings["coins"].items():
table_data.append([key, value["balance"], value["usd"]])
table = SingleTable(table_data, "Coins")
print('TOTAL HOLDINGS: %.2f usd' % holdings["total_usd"])
print(table.table)
def my_crypto_holdings():
args = parser.parse_args()
with open(args.input) as json_data:
cryptos = json.load(json_data)
if (not args.json):
print('Retrieving data...')
accounts = [Account.factory(i) for i in cryptos]
holdings = total_holdings(accounts)
if (args.json):
print(json.dumps(holdings, indent=4))
elif (args.pretty):
pretty_print_holdings(holdings)
else:
print_holdings(holdings)
if __name__ == '__main__':
my_crypto_holdings()