import simplejson
import requests
import decimal


def bc_getutxo(address, ramount):
  r = requests.get('http://btc.blockr.io/api/v1/address/unspent/'+address)

  if r.status_code == 200:
    #Process and format response from blockr.io

    unspents = r.json()['data']['unspent']

    retval = []
    avail = 0
    for tx in unspents:
      tx['amount'] =  int(decimal.Decimal(tx['amount'])*decimal.Decimal(1e8))
      avail += tx['amount']
      retval.append([ tx['tx'], tx['n'], tx['amount'] ])
      if avail >= ramount:
        return {"avail": avail, "utxos": retval, "error": "none"}
    return {"avail": avail, "error": "Low balance error"}
  else:
    return {"error": "Connection error", "code": r.status_code}


def bc_getpubkey(address):
  r = requests.get('https://blockchain.info/q/pubkeyaddr/'+address)

  if r.status_code == 200:
    return str(r.text)
  else:
    return "error"

def bc_getbalance(address):
  r= requests.get('http://btc.blockr.io/api/v1/address/balance/'+address)
  if r.status_code == 200:
    balance = int(r.json()['data']['balance']*1e8)
    return {"bal":balance , "error": None}
  else:
    return {"bal": 0 , "error": "Couldn't get balance"}

def bc_getbulkbalance(addresses):
  r= requests.get('http://btc.blockr.io/api/v1/address/balance/'+addresses)
  if r.status_code == 200:
    balances = r.json()['data']
    retval = {}
    for entry in balances:
      retval[entry['address']] = int(entry['balance']*1e8)

    return {"bal": retval, "error": None}
  else:
    return {"bal": None , "error": "Couldn't get balance"}



@@ -143,34 +143,51 @@
 nspent/'+address
++'?unconfirmed=1'
 )%0A
-
 %0A  if r.status_c
@@ -345,16 +345,52 @@
 spents:%0A
+      if tx%5B'confirmations'%5D %3E 2:%0A  
       tx
@@ -461,24 +461,26 @@
 1e8))%0A      
+  
 avail += tx%5B
@@ -491,24 +491,26 @@
 unt'%5D%0A      
+ 
+ 
 retval.appen
@@ -554,16 +554,18 @@
 )%0A      
+  
 if avail
@@ -577,16 +577,18 @@
 amount:%0A
+  
         

Fix utxo list so recently spent tx's are not tried to be used again