-
Notifications
You must be signed in to change notification settings - Fork 14
/
websitebuilder.py
221 lines (181 loc) · 6.38 KB
/
websitebuilder.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
from respend.txdata import list_respends, load_respend, respend_id
from respend.rpcutil import get_cached_tx
import math
import datetime
from functools import lru_cache
ENTRIES_PER_PAGE = 10
@lru_cache(maxsize = 65536)
def get_vout_value(txid, n):
tx = get_cached_tx(txid)
for vout in tx["vout"]:
if vout["n"] == n:
return vout["value"]
def sum_in_value(tx):
value = 0
for in_ in tx["vin"]:
value += get_vout_value(in_["txid"], in_["vout"])
return float(value)
def sum_out_value(tx):
value = 0
for out in tx["vout"]:
value += out["value"]
return value
def calc_fee(tx):
COIN = 100_000_000
value_in = sum_in_value(tx)
value_out = sum_out_value(tx)
return (value_in * COIN - value_out * COIN) / tx["size"]
def get_inputs(tx):
inputs = [ ]
for in_ in tx['vin']:
txid = in_['txid']
value = get_vout_value(txid, in_['vout'])
inputs.append({
"txid" : txid,
"value" : value
})
return inputs
def get_outputs(tx):
outputs = [ ]
for out in tx['vout']:
addresses = [ ]
if "scriptPubKey" in out and "addresses" in out['scriptPubKey']:
addresses = out['scriptPubKey']['addresses']
outputs.append({
"addresses" : addresses,
"value" : out['value']
})
return outputs
def find_tags(fee_per_byte, outputs):
tags = set()
if fee_per_byte < 1.0:
tags.add("lowfee")
# Known gambling addresses
gambling = [
# Satoshidice addresses (14/03/18)
"bitcoincash:qz9cq5ylczhld5rm7a4px04zl50v5ahr3scyqudsp9",
"bitcoincash:qz9cq5ytxmejfqqr0l5clkmw0l52pj6n5yc76lup7a",
"bitcoincash:qz9cq5yfkyjpgq6xatlr6veyhmcartkyrg7wev9jzc",
"bitcoincash:qz9cq5rlkdrjy2zkfzqscq847q9n07mu5y7hj8fcge",
"bitcoincash:qz9cq5r294syv3csh56e4jpyqrpt7gl9lcj7wveruw",
"bitcoincash:qz9cq5pcexrfnz0a60qz7xtvv8wh5rqf8v6pxd3k74",
"bitcoincash:qz9cq5pgwgx68wfevx0t78xalkh33xa0v5wlx6nppx",
"bitcoincash:qz9cq5pryv9hnqwa8q8mccmynk9uf4vlu5nxerpzmc",
"bitcoincash:qz9cq5qa2mfqcxlc4220yh8fatadu4z7pcewq0ns8y",
"bitcoincash:qz9cq5qeguz30nuynwt2ulq6cxt6gfklfv2djqj9lf",
"bitcoincash:qqpx0wk0hru27l0xk2ek9xulhh269awklyauyuraxk",
];
for out in outputs:
for a in out['addresses']:
if a in gambling:
tags.add("gambling")
return list(tags)
def extract_tx_info(tx):
fee_per_byte = calc_fee(tx)
outputs = get_outputs(tx)
inputs = get_inputs(tx)
return {
"txid" : tx["txid"],
"first_seen" : tx["first_seen"],
"fee" : fee_per_byte,
"inputs" : inputs,
"outputs" : outputs,
"tags" : find_tags(fee_per_byte, outputs)
}
def seconds_between(first, second):
# Seconds between seen
def parse(date):
return datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
return (parse(second) - parse(first)).total_seconds()
def generate_respend_data():
ids = list_respends()
respends_raw = [ load_respend(r) for r in ids ]
respends_raw.sort(key = lambda r: r['first']['first_seen'], reverse = True)
# Extract the information we want
respends = [ ]
while len(respends_raw):
r = respends_raw.pop(0)
try:
first = extract_tx_info(r["first"])
second = extract_tx_info(r["second"])
except Exception as e:
print(str(e))
print("skiping respend")
continue
respends.append({
"first" : first,
"second" : second,
"winner" : r["winner"],
"seconds_between" : seconds_between(first['first_seen'], second['first_seen']),
"id" : respend_id(first['txid'], second['txid']),
})
return respends
# Generates a short, colorful hash
def shortenhash(txid):
prefix = txid[0:2]
suffix = txid[-2:]
middle = txid[2:-2]
html = prefix;
while len(middle):
c = middle[0:4]
middle = middle[4:]
html += '<span class="hash" style="background-color: #%s">%s</span>' % (c, c)
html += suffix
return html
# Remove bitcoincash prefix
def addr_filter(addr):
no_prefix = addr[len("bitcoincash:"):]
return '<a href="https://blockchair.com/bitcoin-cash/address/%s">%s</a>' % (no_prefix, no_prefix)
def txid_filter(txid):
shorten = shortenhash(txid)
return '<a href="https://blockchair.com/bitcoin-cash/transaction/%s">%s</a>' % (txid, shorten)
def amount_filter(amount):
return '<span class="amount">%.8f BCH</span>' % amount
def gen_pagination(curr, total):
def gen_url(page):
return "/%s.html#respends" % page
items = [ ]
items.append({ "title" : "Previous", "url" : None if curr == 1 else gen_url(curr - 1) })
i = curr - 3
while len(items) < 9:
if i < 1:
i += 1
continue
if i > total:
break
items.append({ "title" : str(i), "url" : gen_url(i), "page" : i })
i += 1
items.append({ "title" : "Next", "url" : None if curr == total else gen_url(curr + 1) })
return items
def build_website():
from jinja2 import Template, Environment, select_autoescape, FileSystemLoader
import os
env = Environment(loader = FileSystemLoader("tpl/"))
env.filters['addr'] = addr_filter
env.filters['txid'] = txid_filter
env.filters['bch'] = amount_filter
template = env.get_template("base.html")
print("generating data")
data = generate_respend_data()
pages = math.ceil(len(data) / ENTRIES_PER_PAGE)
print("rendering website")
for p in range(pages):
html = template.render(
page = p + 1,
pagination = gen_pagination(p + 1, pages),
respends = data[:ENTRIES_PER_PAGE],
time_now = datetime.datetime.utcnow())
data = data[ENTRIES_PER_PAGE:]
with open(os.path.join("site", "%s.html" % str(p + 1)), "w") as fh:
fh.write(html)
if p == 0:
# also render page 1 as index.html
with open(os.path.join("site", "index.html"), "w") as fh:
fh.write(html)
# create a gz dump
print("generating gz dump")
import tarfile
gz = tarfile.open("site/txdata.tar.gz", "w:gz")
gz.add("txdata")
gz.close()
print("done")