ruby get BTC balance


SUBMITTED BY: Guest

DATE: Sept. 29, 2014, 5:46 p.m.

FORMAT: Text only

SIZE: 772 Bytes

HITS: 1303

  1. #!/usr/bin/env ruby
  2. # encoding: UTF-8
  3. require 'uri'
  4. require 'open-uri'
  5. require 'nokogiri'
  6. class Btc_Balance
  7. def convert_btc_unit(balance)
  8. final = balance/100000000.0
  9. return final
  10. end
  11. def get_balance(addr)
  12. url = "https://blockchain.info/q/addressbalance/" << addr
  13. data = URI.parse(url).read
  14. doc = Nokogiri::HTML(data)
  15. balance = doc.search('//html/body/p').text
  16. return balance
  17. end
  18. end
  19. def main
  20. btc_account = Btc_Balance.new
  21. addr = "your BTC address"
  22. balance = btc_account.get_balance(addr)
  23. final_balance = btc_account.convert_btc_unit(balance.to_i)
  24. puts "%.8f" % final_balance << " BTC"
  25. end
  26. main

comments powered by Disqus