Python Bitcoin to Euro


SUBMITTED BY: Guest

DATE: May 12, 2013, 9:22 p.m.

FORMAT: Python

SIZE: 718 Bytes

HITS: 1291

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import urllib2
  5. import json
  6. from decimal import Decimal
  7. currency = 'EUR'
  8. try:
  9. response = urllib2.urlopen('http://data.mtgox.com/api/1/BTC%s/ticker' % currency)
  10. except Exception, why:
  11. print '[Error] Can not connect!', why
  12. sys.exit()
  13. data = json.loads(response.read())
  14. bitcoin_cur_value = Decimal(data['return']['avg']['value'])
  15. r = 1 / bitcoin_cur_value
  16. while True:
  17. user_input = raw_input('BTC: ')
  18. if user_input:
  19. bitcoin_amount = Decimal(user_input)
  20. cur_amount = bitcoin_amount / r
  21. print '%s: %.2f' % (currency, cur_amount)

comments powered by Disqus