-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebserver.py
executable file
·230 lines (192 loc) · 8.51 KB
/
webserver.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# author: [email protected]
import ConfigParser
from bottle import route, install, run, template, static_file, response, PasteServer, post, get, request, debug
from bottle_sqlite import SQLitePlugin
import json
import urllib
import urllib2
import datetime
import math
config = ConfigParser.RawConfigParser()
config.read('config.ini')
install(SQLitePlugin(dbfile=(config.get("pool", "database"))))
@route('/api/btime')
def blocktime():
response.headers['Cache-Control'] = 'public, max-age=100'
payload = {
'requestType': 'getForging',
'secretPhrase': config.get("pool", "poolphrase")
}
opener = urllib2.build_opener(urllib2.HTTPHandler())
data = urllib.urlencode(payload)
forging = json.loads(opener.open(config.get("pool", "nhzhost")+'/nhz', data=data).read())
getdl = forging["deadline"]
dl = str(datetime.timedelta(seconds=getdl))
return {'blocktime': dl}
@route('/api/accounts')
def apiaccounts(db):
response.headers['Cache-Control'] = 'public, max-age=3600'
getlastheight = db.execute("SELECT height FROM blocks ORDER BY timestamp DESC").fetchone()
try:
lastheight = getlastheight[0]
except:
lastheight = 0
c = db.execute("SELECT ars, heightfrom, heightto, CAST(amount AS FLOAT)/100000000 AS amount FROM leased WHERE heightto > %s" % (lastheight)).fetchall()
accounts = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return accounts
@route('/api/blocks')
def apiblocks(db):
response.headers['Cache-Control'] = 'public, max-age=1800'
c = db.execute("SELECT height, datetime(timestamp+1395526942, 'unixepoch', 'localtime') as timestamp, CAST(totalfee AS FLOAT)/100000000 AS totalfee FROM blocks WHERE totalfee > 0 ORDER BY timestamp DESC").fetchall()
blocks = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return blocks
@route('/api/leased')
def apileased():
getaccounts = json.loads(urllib2.urlopen(config.get("pool", "nhzhost")+"/nhz?requestType=getAccount&account="+config.get("pool", "poolaccount")).read())
try:
leasebal = getaccounts['effectiveBalanceNHZ']
except:
leasebal = 0
return {'blocktime': leasebal}
@route('/api/payouts')
def apipayouts(db):
response.headers['Cache-Control'] = 'public, max-age=3600'
c = db.execute("SELECT account, CAST(fee AS FLOAT)/100000000 AS fee, CAST(payment AS FLOAT)/100000000 AS payment FROM payouts DESC").fetchall()
pays = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return pays
@route('/api/paid')
def apipaid(db):
response.headers['Cache-Control'] = 'public, max-age=3600'
c = db.execute("SELECT datetime(blocktime+1395526942, 'unixepoch', 'localtime') as blocktime, account, percentage, CAST(amount AS FLOAT)/100000000 AS amount FROM accounts WHERE paid>0 ORDER BY blocktime DESC").fetchall()
pays = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return pays
@route('/api/unpaid')
def apiunpaid(db):
response.headers['Cache-Control'] = 'public, max-age=3600'
c = db.execute("SELECT datetime(blocktime+1395526942, 'unixepoch', 'localtime') as blocktime, account, percentage, CAST(amount AS FLOAT)/100000000 AS amount FROM accounts WHERE paid=0 ORDER BY blocktime DESC").fetchall()
pays = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return pays
@route('/api/userunpaid/:user#NHZ-[0-9A-Z-]+#')
def apiuserunpaid(db, user):
response.headers['Cache-Control'] = 'public, max-age=600'
c = db.execute("SELECT datetime(blocktime+1395526942, 'unixepoch', 'localtime') as blocktime, percentage, CAST(amount AS FLOAT)/100000000 AS amount FROM accounts WHERE paid=0 and account LIKE ?", (user,)).fetchall()
pays = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return pays
@route('/api/userpaid/:user#NHZ-[0-9A-Z-]+#')
def apiuserpaid(db, user):
response.headers['Cache-Control'] = 'public, max-age=600'
c = db.execute("SELECT datetime(blocktime+1395526942, 'unixepoch', 'localtime') as blocktime, percentage, CAST(amount AS FLOAT)/100000000 AS amount FROM accounts WHERE paid>0 and account LIKE ?", (user,)).fetchall()
pays = json.dumps( [dict(ix) for ix in c], separators=(',',':'))
return pays
@route('/')
def default(db):
response.headers['Cache-Control'] = 'public, max-age=3600'
poolaccount = config.get("pool", "poolaccountrs")
poolfee = config.get("pool", "feePercent")
payoutlimit = config.get("pool", "payoutlimit")
c = db.execute("SELECT ars, heightto, CAST(amount AS FLOAT)/100000000 AS amount FROM leased ORDER BY heightfrom DESC limit 10")
result = c.fetchall()
e = db.execute("SELECT height, datetime(timestamp+1395526942, 'unixepoch', 'localtime') as timestamp, CAST(totalfee AS FLOAT)/100000000 AS totalfee FROM blocks ORDER BY timestamp DESC limit 10")
block = e.fetchall()
cunpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid=0").fetchone()
for r in cunpaid:
try:
unpaid = math.trunc(float(r)/100000000)
except TypeError:
unpaid = 0
getaccounts = json.loads(urllib2.urlopen(config.get("pool", "nhzhost")+"/nhz?requestType=getAccount&account="+config.get("pool", "poolaccount")).read())
try:
leasebal = getaccounts['effectiveBalanceNHZ']
except KeyError:
leasebal = 0
output = template('default', pa=poolaccount, fee=poolfee, rows=result, blocks=block, nhzb=leasebal, payoutlimit=payoutlimit, unpaid=unpaid)
return output
@route('/static/:path#.+#', name='static')
def static(path):
response.headers['Cache-Control'] = 'public, max-age=2592000'
return static_file(path, root='static')
@route('/favicon.ico')
def get_favicon():
response.headers['Cache-Control'] = 'public, max-age=2592000'
return static('favicon.ico')
@route('/getting_started')
def getting_started():
response.headers['Cache-Control'] = 'public, max-age=43200'
output = template('gettingstarted')
return output
@route('/accounts')
def accounts(db):
response.headers['Cache-Control'] = 'public, max-age=43200'
output = template('accounts')
return output
@route('/blocks')
def blocks(db):
response.headers['Cache-Control'] = 'public, max-age=43200'
output = template('blocks')
return output
@route('/payouts')
def payouts(db):
response.headers['Cache-Control'] = 'public, max-age=43200'
output = template('payouts')
return output
@route('/unpaid')
def unpaid(db):
response.headers['Cache-Control'] = 'public, max-age=43200'
cunpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid=0").fetchone()
for e in cunpaid:
try:
unpaid = float(e)/100000000
except TypeError:
unpaid = 0
output = template('unpaid', unpaid=unpaid)
return output
@route('/paid')
def paid(db):
response.headers['Cache-Control'] = 'public, max-age=43200'
cpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid>0").fetchone()
for d in cpaid:
try:
paid = float(d)/100000000
except TypeError:
paid = 0
output = template('paid', paid=paid)
return output
@get('/user')
def user(db):
aid = request.GET.get('username')
user=aid
cpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid>0 and account LIKE ?", (user,)).fetchone()
for d in cpaid:
try:
paid = float(d)/100000000
except TypeError:
paid = 0
cunpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid=0 and account LIKE ?", (user,)).fetchone()
for e in cunpaid:
try:
unpaid = float(e)/100000000
except TypeError:
unpaid = 0
output = template('user', user=user, aid=aid, paid=paid, unpaid=unpaid)
return output
@post('/user')
def user(db):
aid = request.forms.get('username')
user=aid
cpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid>0 and account LIKE ?", (user,)).fetchone()
for d in cpaid:
try:
paid = float(d)/100000000
except TypeError:
paid = 0
cunpaid = db.execute("SELECT sum(amount) FROM accounts WHERE paid=0 and account LIKE ?", (user,)).fetchone()
for e in cunpaid:
try:
unpaid = float(e)/100000000
except TypeError:
unpaid = 0
output = template('user', user=user, aid=aid, paid=paid, unpaid=unpaid)
return output
#debug(True)
run(server=PasteServer, port=config.get("pool", "port"), host='0.0.0.0')