In [373]:
import requests
import numpy as np
import pandas as pd
from time import sleep
In [374]:
def decode_block(block):
def get_block(block):
_ = {
'height': block['height'],
'hash': block['hash'],
'time': block['time'],
'fee': block['fee'],
'n_tx': block['n_tx'],
'size': block['size']
}
return _
def get(tx):
return (tx['addr'], float(tx['value']))
def not_reward(x):
return 'prev_out' in x.keys()
b = get_block(block)
block_hash = b['hash']
block_height = b['height']
index = 0
txs = []
for tx in block['tx']:
inputs = [ get(_tx['prev_out']) for _tx in tx['inputs'] if not_reward(_tx) ]
outputs = [ get(_tx) for _tx in tx['out'] if float(_tx['value']) > 0 ]
_ = {
'block': block_height,
'index': index,
'time': tx['time'],
'inputs': inputs,
'outputs': outputs
}
txs.append(_)
index += 1
return b, txs
def get_block_at(height = 0):
url = 'https://blockchain.info/block-height/' + str(height) + '?format=json'
blocks = requests.get(url).json()
main_block = [b for b in blocks['blocks'] if b['main_chain'] == True][0]
return decode_block(main_block)
In [375]:
def get_blocks_by_interval(interval = [], timeout=.3):
blocks = []
txs = []
try:
for height in interval:
b,ts = get_block_at(height)
blocks.append(b)
for t in ts:
txs.append(t)
sleep(timeout)
except:
print('error ... ')
return blocks, txs
In [387]:
## pull blocks by height ... example poc ...
In [377]:
blocks, txs = get_blocks_by_interval([0,1,2])
In [378]:
df_blocks = pd.DataFrame(blocks).set_index(['height'])
df_txs = pd.DataFrame(txs).set_index(['block','index'])
In [379]:
df_blocks.head()
Out[379]:
In [380]:
df_blocks.tail()
Out[380]:
In [381]:
df_txs.head()
Out[381]:
In [382]:
df_txs.tail()
Out[382]:
In [386]:
## pull blocks by interval ... example poc ...
In [383]:
file_blocks = './/data//blocks.csv'
file_transactions = './/data//tx.csv'
def write_to_file(df_blocks, df_txs):
df_blocks.to_csv(file_blocks, header=True)
df_txs.to_csv(file_transactions, header=True)
def append_to_file(df_blocks, df_txs):
with open(file_blocks, 'a') as f:
df_blocks.to_csv(f, header=False)
with open(file_transactions, 'a') as f:
df_txs.to_csv(f, header=False)
# options:
b_ = 0
e_ = 2
i_ = 1
period = 5
write_first = True
for start in np.arange(b_, e_, i_):
s_ = start*period
e_ = s_+period
interval = np.arange(s_, e_, 1)
blocks, txs = get_blocks_by_interval(interval)
df_blocks = pd.DataFrame(blocks).set_index(['height'])
df_txs = pd.DataFrame(txs).set_index(['block','index'])
if write_first:
write_to_file(df_blocks, df_txs)
write_first = False
else:
append_to_file(df_blocks, df_txs)
In [384]:
pd.read_csv(file_blocks, index_col=['height'])
Out[384]:
In [385]:
pd.read_csv(file_transactions, index_col=['block','index'])
Out[385]: