def __init__(self, settings): today_str = date.today().strftime("%Y-%m-%d") # request to fetch the current BTC/USD exchange rate exchange_rate_req = \ RateRequest("mena='code:UBTC' and platiOdData >= '%s'" % today_str) self.exchange_rate_btc_today = \ exchange_rate_req.get_and_build_objects(ExchangeRate, settings.url, settings.user, settings.passwd, params={'limit':'1', }, ) if len(self.exchange_rate_btc_today) == 0: raise Exception('No BTC/CZK exchange rate available for today: %s' % today_str)
rates[currency[CNB_API_CURRENCY_CODE]] = rate_float / amount usd_czk = rates['USD'] # fetch BTC/USD price - weighted average for the past 24 hours client = bitstamp.client.Public() ticker = client.ticker() btc_usd = Decimal(ticker['vwap']) btc_czk = usd_czk * btc_usd btc_czk_str = "%s" % btc_czk.quantize(Decimal('1e-2')) print "USD/CZK: %s; BTC/USD: %s; BTC/CZK: %s" % (usd_czk, btc_usd, btc_czk_str) try: req = RateRequest() today_str = date.today().strftime("%Y-%m-%d") req.append(ExchangeRate.new_for_update(today_str, btc_czk_str)) print "Sending: %s" % req response = req.put(settings.url, settings.user, settings.passwd) print "Received: %s" % response.content except Exception, e: print 'Error: %s' % e print(traceback.format_exc()) sys.exit(2)