-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.py
58 lines (41 loc) · 1.57 KB
/
update.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
import requests
from app import db, Transaction
from datetime import datetime
from os import environ
from pytz import utc
def get_coindesk_rate():
TICKER_URL = 'https://api.coindesk.com/v1/bpi/currentprice.json'
ticker = requests.get(TICKER_URL)
rate = ticker.json()['bpi']['USD']['rate_float']
return rate
def parse_tx(btc_addr, tx, usd_rate):
if 'block_height' not in tx:
# skip this TX, it's unverified
return
for inpt in tx['inputs']:
if inpt['prev_out']['addr'] == btc_addr:
# skip change transactions
return
hash_id = tx['hash']
block_height = tx['block_height']
time = datetime.fromtimestamp(tx['time'], tz=utc)
amount = sum([x['value'] for x in tx['out'] if x['addr'] == btc_addr])
usd_worth = float(usd_rate) * amount / 10e7
sender = tx['inputs'][0]['prev_out']['addr']
dbtx = Transaction.query.filter_by(hash_id=hash_id).first()
if not dbtx:
print('Inserting tx {}'.format(hash_id))
dbtx = Transaction(hash_id, sender, block_height, time, amount, usd_worth)
db.session.add(dbtx)
db.session.commit()
def get_blockchain_txs(usd_rate):
ADDRESS_URL = 'https://blockchain.info/address/{}'
BTC_ADDR = environ.get('BTC_ADDR')
res = requests.get(ADDRESS_URL.format(BTC_ADDR), params={'format': 'json'})
for tx in res.json()['txs']:
parse_tx(BTC_ADDR, tx, usd_rate)
print('Starting update.py')
usd_rate = get_coindesk_rate()
print('Current coindesk BTC/USD rate: ${}'.format(usd_rate))
get_blockchain_txs(usd_rate)
print('Done.')