-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnpl.py
87 lines (64 loc) · 2.35 KB
/
nnpl.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
#!/usr/local/bin/python3
# eth, sia, zec via nanopool api
# 7/29/17
# updated 11/7/17
import minor
import pickle
import requests
import logging
from datetime import datetime
class Nnpl(object):
'''basic functions to interact with nanopool api'''
addresses = minor.addresses
def __init__(self, key):
self.key = key
self.currency = self.key[0:3]
self.address = self.addresses[self.key]
self.payments = {}
def _construct_url(self, action):
if action == 'prices':
return 'https://api.nanopool.org/v1/{currency}/{action}'.format(currency=self.currency, action=action)
else:
return 'https://api.nanopool.org/v1/{currency}/{action}/{acct}'.format(currency=self.currency, action=action, acct=self.address)
def _request(self, action):
retries = 5
while retries:
try:
r = requests.get(self._construct_url(action))
if r.json()['status'] is True:
return r.json()['data']
else:
logging.debug("r.json()['status'] == False for action: {}, key: {}. probably an old account, returning 0")
return 0
except KeyError:
if retries > 1:
logging.error("KeyError calling r.json()['data'] for action: {}, key: {}. retrying request...".format(action, self.key.upper()))
retries -= 1
def _convert_to_datetime(self):
for pymnt in self.payments:
pymnt['date'] = datetime.fromtimestamp(pymnt['date'])
def get_lew(self):
lews_cut = 0.25
balance = self.get_balance()
lew_paid = lews_cut * balance
with open(minor.lew_pymnts, 'rb') as pckl:
lew = pickle.load(pckl)
lew_paid += lew
return lew_paid
def get_balance(self):
return self._request('balance')
def get_payments(self):
self.payments = self._request('payments')
if self.payments:
self._convert_to_datetime()
return self.payments
def get_paid(self):
paid = 0
self.get_payments()
for payment in self.payments:
paid += payment['amount']
return paid
def get_prices(self):
return self._request('prices')
def get_hashrate(self):
return self._request('hashrate')