-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin.py
125 lines (93 loc) · 3.09 KB
/
coin.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
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
#!/usr/local/bin/python3
# check coin balances via nanopool api
# 6/22/17
# updated 8/17/17
import yaml
import dcrd
import nnpl
import minor
import logging
import logging.config
class Coin(object):
'''basic functions to interact with nanopool api'''
def __init__(self, key, interface):
self.key = key
self.currency = self.key[0:3]
self.interface = interface
self.balance = 0
self.payments = {}
self.paid = 0
self.total = 0
self.btc = 0
self.usd = 0
self.prices = {
'btc': 0,
'usd': 0
}
def _get_total(self):
self.balance = self.interface.get_balance()
self.paid = self.interface.get_paid()
self.total = self.balance + self.paid
if self.key == 'zec':
self.total -= self.interface.get_lew()
def _convert_to_prices(self):
self.get_prices()
self.btc = self.total * self.prices['btc']
self.usd = self.total * self.prices['usd']
def get_balance(self):
return self.interface.get_balance()
def get_payments(self):
self.payments = self.interface.get_payments()
return self.payments
def get_last_payment(self):
self.get_payments()
if self.payments:
return self.payments[0]
else:
return self.payments # equal to None if request failed
def get_prices(self):
prices = self.interface.get_prices()
self.prices['btc'] = prices['price_btc']
self.prices['usd'] = prices['price_usd']
def update(self):
self._get_total()
self._convert_to_prices()
self.info = {
'balance': self.balance,
'paid': self.paid,
'total': self.total,
'total_btc': self.btc,
'total_usd': self.usd,
'price': self.prices['usd']
}
def initialize_logger():
with open(minor.log_conf, 'r') as log_conf:
log_config = yaml.safe_load(log_conf)
logging.config.dictConfig(log_config)
logger = logging.getLogger('coin')
logger.info('* * * * * * * * * * * * * * * * * * * *')
logger.info('coin logger instantiated')
return logger
if __name__ == '__main__':
logger = initialize_logger()
stat_order = ['balance', 'paid', 'total', 'total_btc', 'total_usd', 'price']
coins = [key for key in minor.addresses.keys()]
total_usd = 0
total_btc = 0
for currency in coins:
if currency == 'dcr':
api = dcrd
else:
api = nnpl.Nnpl(currency)
altcoin = Coin(currency, api)
altcoin.update()
total_usd += altcoin.info['total_usd']
total_btc += altcoin.info['total_btc']
logger.info(' - - - {} - - - '.format(currency.upper()))
for key in stat_order:
logger.info('{}: {:.6f}'.format(key, altcoin.info[key]))
logger.info(' - - - - - - - - ')
logger.info(' - - - TOTAL - - - ')
logger.info('total_usd: {:.6f}'.format(total_usd))
logger.info('total_btc: {:.6f}'.format(total_btc))
logger.info(' - - - - - - - - ')