Update itbit/coindesk exchange rate api

itbit/coindesk exchange rate requires separate API calls per currency
Moved the calls to when the exchange rate plugin updates and when the
user modifies a selection instead of every time the program needs to
query the current exchange rate

This fixes an issue with slow response time in the send tab with the
exchange rate plugin turned on and set to coindesk or itbit
This commit is contained in:
Michael Wozniak 2014-09-05 19:58:54 -04:00
parent c1d286fc9a
commit a996659225

View file

@ -29,6 +29,10 @@ EXCHANGES = ["BitcoinAverage",
"LocalBitcoins", "LocalBitcoins",
"Winkdex"] "Winkdex"]
EXCH_SUPPORT_HIST = [("CoinDesk", "USD"),
("Winkdex", "USD"),
("BitcoinVenezuela", "ARS"),
("BitcoinVenezuela", "VEF")]
class Exchanger(threading.Thread): class Exchanger(threading.Thread):
@ -41,7 +45,6 @@ class Exchanger(threading.Thread):
self.query_rates = threading.Event() self.query_rates = threading.Event()
self.use_exchange = self.parent.config.get('use_exchange', "Blockchain") self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
self.parent.exchanges = EXCHANGES self.parent.exchanges = EXCHANGES
self.parent.currencies = ["EUR","GBP","USD","PLN"]
self.parent.win.emit(SIGNAL("refresh_exchanges_combo()")) self.parent.win.emit(SIGNAL("refresh_exchanges_combo()"))
self.parent.win.emit(SIGNAL("refresh_currencies_combo()")) self.parent.win.emit(SIGNAL("refresh_currencies_combo()"))
self.is_running = False self.is_running = False
@ -69,18 +72,6 @@ class Exchanger(threading.Thread):
quote_currencies = self.quote_currencies.copy() quote_currencies = self.quote_currencies.copy()
if quote_currency not in quote_currencies: if quote_currency not in quote_currencies:
return None return None
if self.use_exchange == "CoinDesk":
try:
resp_rate = self.get_json('api.coindesk.com', "/v1/bpi/currentprice/" + str(quote_currency) + ".json")
except Exception:
return
return btc_amount * decimal.Decimal(str(resp_rate["bpi"][str(quote_currency)]["rate_float"]))
elif self.use_exchange == "itBit":
try:
resp_rate = self.get_json('www.itbit.com', "/api/feeds/ticker/XBT" + str(quote_currency))
except Exception:
return
return btc_amount * decimal.Decimal(str(resp_rate["bid"]))
return btc_amount * decimal.Decimal(str(quote_currencies[quote_currency])) return btc_amount * decimal.Decimal(str(quote_currencies[quote_currency]))
def stop(self): def stop(self):
@ -125,6 +116,14 @@ class Exchanger(threading.Thread):
quote_currencies = {} quote_currencies = {}
for cur in resp_currencies: for cur in resp_currencies:
quote_currencies[str(cur["currency"])] = 0.0 quote_currencies[str(cur["currency"])] = 0.0
current_cur = self.parent.config.get("currency", "EUR")
if current_cur in quote_currencies:
try:
resp_rate = self.get_json('api.coindesk.com', "/v1/bpi/currentprice/" + str(current_cur) + ".json")
quote_currencies[str(current_cur)] = decimal.Decimal(str(resp_rate["bpi"][str(current_cur)]["rate_float"]))
except Exception:
return
with self.lock: with self.lock:
self.quote_currencies = quote_currencies self.quote_currencies = quote_currencies
self.parent.set_currencies(quote_currencies) self.parent.set_currencies(quote_currencies)
@ -134,6 +133,13 @@ class Exchanger(threading.Thread):
quote_currencies = {} quote_currencies = {}
for cur in available_currencies: for cur in available_currencies:
quote_currencies[cur] = 0.0 quote_currencies[cur] = 0.0
current_cur = self.parent.config.get("currency", "EUR")
if current_cur in available_currencies:
try:
resp_rate = self.get_json('api.itbit.com', "/v1/markets/XBT" + str(current_cur) + "/ticker")
quote_currencies[str(current_cur)] = decimal.Decimal(str(resp_rate["lastPrice"]))
except Exception:
return
with self.lock: with self.lock:
self.quote_currencies = quote_currencies self.quote_currencies = quote_currencies
self.parent.set_currencies(quote_currencies) self.parent.set_currencies(quote_currencies)
@ -260,7 +266,6 @@ class Exchanger(threading.Thread):
except KeyError: except KeyError:
pass pass
self.parent.set_currencies(quote_currencies) self.parent.set_currencies(quote_currencies)
# print "updating exchange rate", self.quote_currencies["USD"]
def update_lb(self): def update_lb(self):
try: try:
@ -311,9 +316,6 @@ class Exchanger(threading.Thread):
self.parent.set_currencies(quote_currencies) self.parent.set_currencies(quote_currencies)
def get_currencies(self):
return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
def _lookup_rate(self, response, quote_id): def _lookup_rate(self, response, quote_id):
return decimal.Decimal(str(response[str(quote_id)]["15m"])) return decimal.Decimal(str(response[str(quote_id)]["15m"]))
def _lookup_rate_cb(self, response, quote_id): def _lookup_rate_cb(self, response, quote_id):
@ -499,7 +501,7 @@ class Plugin(BasePlugin):
except KeyError: except KeyError:
tx_BTCVEN_val = _("No data") tx_BTCVEN_val = _("No data")
if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex": if cur_exchange in ["CoinDesk", "Winkdex"]:
item.setText(5, tx_USD_val) item.setText(5, tx_USD_val)
elif cur_exchange == "BitcoinVenezuela": elif cur_exchange == "BitcoinVenezuela":
item.setText(5, tx_BTCVEN_val) item.setText(5, tx_BTCVEN_val)
@ -527,10 +529,7 @@ class Plugin(BasePlugin):
combo_ex = QComboBox() combo_ex = QComboBox()
hist_checkbox = QCheckBox() hist_checkbox = QCheckBox()
hist_checkbox.setEnabled(False) hist_checkbox.setEnabled(False)
if self.config.get('history_rates', 'unchecked') == 'unchecked': hist_checkbox.setChecked(self.config.get('history_rates', 'unchecked') != 'unchecked')
hist_checkbox.setChecked(False)
else:
hist_checkbox.setChecked(True)
ok_button = QPushButton(_("OK")) ok_button = QPushButton(_("OK"))
def on_change(x): def on_change(x):
@ -541,15 +540,10 @@ class Plugin(BasePlugin):
if cur_request != self.fiat_unit(): if cur_request != self.fiat_unit():
self.config.set_key('currency', cur_request, True) self.config.set_key('currency', cur_request, True)
cur_exchange = self.config.get('use_exchange', "Blockchain") cur_exchange = self.config.get('use_exchange', "Blockchain")
if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"): if (cur_exchange, cur_request) in EXCH_SUPPORT_HIST:
hist_checkbox.setEnabled(True)
elif cur_request == "VEF" and (cur_exchange == "BitcoinVenezuela"):
hist_checkbox.setEnabled(True)
elif cur_request == "ARS" and (cur_exchange == "BitcoinVenezuela"):
hist_checkbox.setEnabled(True) hist_checkbox.setEnabled(True)
else: else:
hist_checkbox.setChecked(False) disable_check()
hist_checkbox.setEnabled(False)
self.win.update_status() self.win.update_status()
try: try:
self.fiat_button self.fiat_button
@ -570,16 +564,8 @@ class Plugin(BasePlugin):
combo.clear() combo.clear()
self.exchanger.query_rates.set() self.exchanger.query_rates.set()
cur_currency = self.fiat_unit() cur_currency = self.fiat_unit()
if cur_request == "CoinDesk" or cur_request == "Winkdex": if (cur_request, cur_currency) in EXCH_SUPPORT_HIST:
if cur_currency == "USD": hist_checkbox.setEnabled(True)
hist_checkbox.setEnabled(True)
else:
disable_check()
elif cur_request == "BitcoinVenezuela":
if cur_currency == "VEF" or cur_currency == "ARS":
hist_checkbox.setEnabled(True)
else:
disable_check()
else: else:
disable_check() disable_check()
set_currencies(combo) set_currencies(combo)
@ -598,16 +584,12 @@ class Plugin(BasePlugin):
def set_hist_check(hist_checkbox): def set_hist_check(hist_checkbox):
cur_exchange = self.config.get('use_exchange', "Blockchain") cur_exchange = self.config.get('use_exchange', "Blockchain")
if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex": hist_checkbox.setEnabled(cur_exchange in ["CoinDesk", "Winkdex", "BitcoinVenezuela"])
hist_checkbox.setEnabled(True)
elif cur_exchange == "BitcoinVenezuela":
hist_checkbox.setEnabled(True)
else:
hist_checkbox.setEnabled(False)
def set_currencies(combo): def set_currencies(combo):
current_currency = self.fiat_unit()
try: try:
combo.blockSignals(True)
current_currency = self.fiat_unit()
combo.clear() combo.clear()
except Exception: except Exception:
return return
@ -616,6 +598,7 @@ class Plugin(BasePlugin):
index = self.currencies.index(current_currency) index = self.currencies.index(current_currency)
except Exception: except Exception:
index = 0 index = 0
combo.blockSignals(False)
combo.setCurrentIndex(index) combo.setCurrentIndex(index)
def set_exchanges(combo_ex): def set_exchanges(combo_ex):
@ -631,6 +614,8 @@ class Plugin(BasePlugin):
combo_ex.setCurrentIndex(index) combo_ex.setCurrentIndex(index)
def ok_clicked(): def ok_clicked():
if self.config.get('use_exchange', "Blockchain") in ["CoinDesk", "itBit"]:
self.exchanger.query_rates.set()
d.accept(); d.accept();
set_exchanges(combo_ex) set_exchanges(combo_ex)