#!/usr/bin/env python
-import sys, time
-from electrum import Interface
+import sys, time, electrum
try:
addr = sys.argv[1]
-except:
+except Exception:
print "usage: watch_address <bitcoin_address>"
sys.exit(1)
-i = Interface()
-i.start()
-i.send([('blockchain.address.subscribe',[addr])] )
-time.sleep(1)
+# 1. start the interface and wait for connection
+interface = electrum.Interface('electrum.no-ip.org:50002:s')
+interface.start(wait = True)
+if not interface.is_connected:
+ print "not connected"
+ exit()
+
+# 2. send the subscription
+callback = lambda _,result: electrum.print_json(result.get('result'))
+interface.send([('blockchain.address.subscribe',[addr])], callback)
+
+# 3. wait for results
while True:
- r = i.get_response()
- method = r.get('method')
- if method == 'blockchain.address.subscribe':
- i.send([('blockchain.address.get_history',[addr])])
-
- elif method == 'blockchain.address.get_history':
- for line in r.get('result'):
- print line
- print "---"
+ time.sleep(1)
+