|
| 1 | +#encoding:UTF-8 |
| 2 | +import json, os, sys, time |
| 3 | +import requests |
| 4 | +from contextlib import suppress |
| 5 | +from concurrent import futures |
| 6 | +from steem.blockchain import Blockchain |
| 7 | +from steem.steemd import Steemd |
| 8 | + |
| 9 | + |
| 10 | +env_dist = os.environ |
| 11 | +api_url = env_dist.get('API_URL') |
| 12 | +if api_url == None: |
| 13 | + print("Please set API_URL env") |
| 14 | + sys.exit() |
| 15 | +print(api_url) |
| 16 | +worker_num = env_dist.get('WORKER_NUM') |
| 17 | +if worker_num == None: |
| 18 | + worker_num = 5 |
| 19 | +print('Worker num: %s' % (worker_num)) |
| 20 | +worker_num = int(worker_num) |
| 21 | +env_block_num = env_dist.get('BLOCK_NUM') |
| 22 | +if env_block_num == None: |
| 23 | + start_block_num = 0 |
| 24 | +else: |
| 25 | + start_block_num = int(env_block_num) |
| 26 | + |
| 27 | +steemd_nodes = [ |
| 28 | + 'https://api.steemit.com', |
| 29 | +] |
| 30 | +s = Steemd(nodes=steemd_nodes) |
| 31 | +b = Blockchain(s) |
| 32 | + |
| 33 | +def worker(start, end): |
| 34 | + global s, b |
| 35 | + print('start from {start} to {end}'.format(start=start, end=end)) |
| 36 | + block_infos = s.get_blocks(range(start, end+1)) |
| 37 | + # print(block_infos) |
| 38 | + for block_info in block_infos: |
| 39 | + transactions = block_info['transactions'] |
| 40 | + for trans in transactions: |
| 41 | + operations = trans['operations'] |
| 42 | + for op in operations: |
| 43 | + if op[0] == 'comment' and op[1]['parent_author'] != '': |
| 44 | + print('send data: ', op[1]) |
| 45 | + postdata = json.dumps(op) |
| 46 | + r = requests.post(api_url, data=postdata) |
| 47 | + print('{start}:{end}: {result}'.format( |
| 48 | + start=start, |
| 49 | + end=end, |
| 50 | + result=r.text) |
| 51 | + ) |
| 52 | + if op[0] == 'transfer': |
| 53 | + print('send data: ', op[1]) |
| 54 | + postdata = json.dumps(op) |
| 55 | + r = requests.post(api_url, data=postdata) |
| 56 | + print('{start}:{end}: {result}'.format( |
| 57 | + start=start, |
| 58 | + end=end, |
| 59 | + result=r.text) |
| 60 | + ) |
| 61 | + |
| 62 | +def run(): |
| 63 | + global start_block_num |
| 64 | + steemd_nodes = [ |
| 65 | + 'https://api.steemit.com', |
| 66 | + ] |
| 67 | + s = Steemd() |
| 68 | + # s = Steemd(nodes=steemd_nodes) |
| 69 | + b = Blockchain(s) |
| 70 | + |
| 71 | + while True: |
| 72 | + head_block_number = b.info()['head_block_number'] |
| 73 | + end_block_num = int(head_block_number) |
| 74 | + if start_block_num == 0: |
| 75 | + start_block_num = end_block_num - 3 |
| 76 | + if start_block_num >= end_block_num: |
| 77 | + continue |
| 78 | + with futures.ThreadPoolExecutor(max_workers=worker_num) as executor: |
| 79 | + executor.submit(worker, start_block_num, end_block_num) |
| 80 | + start_block_num = end_block_num + 1 |
| 81 | + time.sleep(3) |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + with suppress(KeyboardInterrupt): |
| 85 | + run() |
0 commit comments