-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitcoin.py
54 lines (37 loc) · 1.35 KB
/
bitcoin.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
import json
import requests
import logging
from hdfs_writer import write_to_hdfs
from kafka import store_in_big_data
from settings import BTC_BLOCK_TOPIC, BTC_HOST, BTC_PORT
from cassandra_write import write_to_cassandra
logger = logging.getLogger("default")
def get_block_hash(height):
response = requests.get("http://{}:{}/rest/blockhashbyheight/{}.json".format(BTC_HOST, BTC_PORT, height))
if response.status_code != 200:
return
return json.loads(response.text)['blockhash']
def get_block(block_hash):
response = requests.get("http://{}:{}/rest/block/{}.json".format(BTC_HOST, BTC_PORT, block_hash))
if response.status_code != 200:
return
return json.loads(response.text)
def btc():
last_height = 0
try:
with open('checkpoints/btc', 'r') as file:
last_height = int(file.read().strip())
except:
pass
while True:
block_hash = get_block_hash(last_height)
if block_hash:
logger.info('storing block {}.'.format(block_hash))
block = get_block(block_hash)
if write_to_cassandra(block):
write_to_hdfs(block)
last_height += 1
with open('checkpoints/btc', 'w') as file:
file.write(str(last_height))
if __name__ == "__main__":
btc()