-
Notifications
You must be signed in to change notification settings - Fork 9
/
tx.py
executable file
·88 lines (65 loc) · 1.55 KB
/
tx.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
#!/usr/bin/env python
################
# dependencies #
################
import sys
import os
import numbers
import decimal
import json
import bitcoinAuth
from sys import argv
from bitcoinrpc import AuthServiceProxy, JSONRPCException
#############
# constants #
#############
rootdir = sys.path[0]
txFile = rootdir + '/data/tx.txt'
##############
# initialize #
##############
# from command line paramaters
script, txid = argv
# initialize bitcoin RPC connection and gather info
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%(bitcoinAuth.USER,bitcoinAuth.PW))
#############
# functions #
#############
# because JSON dump hates decimals for soem reason
def allToString(obj):
return str(obj)
########
# main #
########
# create new file if missing
if not os.path.isfile(txFile):
# create directory if missing
if not os.path.exists(rootdir + '/data'):
os.makedirs(rootdir + '/data')
os.chmod(rootdir + '/data', 0777)
f = os.open(txFile, os.O_CREAT)
# script will be run both as root and user
os.fchmod(f, 0777)
os.close(f)
# open file and read data
f = open(txFile,'r+')
data = f.read()
if data:
dataJson = json.loads(data)
else:
dataJson = json.loads("[]")
# move file pointer back to top
f.seek(0)
# get info from this latest tx we just learned about
txInfo = rpc_connection.gettransaction(txid)
# insert into JSON from file
dataJson.append(txInfo)
# convert back to string and write to file
dataJson = json.dumps(dataJson, default=allToString)
f.write(dataJson)
f.truncate()
# close file
f.close()
#print
#print "--"
#print "New Tx!"