From a996659225257a7d9f1f345b97f0ab24e732915c Mon Sep 17 00:00:00 2001 From: Michael Wozniak Date: Fri, 5 Sep 2014 19:58:54 -0400 Subject: [PATCH] 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 --- plugins/exchange_rate.py | 77 ++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/plugins/exchange_rate.py b/plugins/exchange_rate.py index 2ec3df542..65a6edd3a 100644 --- a/plugins/exchange_rate.py +++ b/plugins/exchange_rate.py @@ -29,6 +29,10 @@ EXCHANGES = ["BitcoinAverage", "LocalBitcoins", "Winkdex"] +EXCH_SUPPORT_HIST = [("CoinDesk", "USD"), + ("Winkdex", "USD"), + ("BitcoinVenezuela", "ARS"), + ("BitcoinVenezuela", "VEF")] class Exchanger(threading.Thread): @@ -41,7 +45,6 @@ class Exchanger(threading.Thread): self.query_rates = threading.Event() self.use_exchange = self.parent.config.get('use_exchange', "Blockchain") 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_currencies_combo()")) self.is_running = False @@ -69,18 +72,6 @@ class Exchanger(threading.Thread): quote_currencies = self.quote_currencies.copy() if quote_currency not in quote_currencies: 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])) def stop(self): @@ -125,6 +116,14 @@ class Exchanger(threading.Thread): quote_currencies = {} for cur in resp_currencies: 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: self.quote_currencies = quote_currencies self.parent.set_currencies(quote_currencies) @@ -134,6 +133,13 @@ class Exchanger(threading.Thread): quote_currencies = {} for cur in available_currencies: 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: self.quote_currencies = quote_currencies self.parent.set_currencies(quote_currencies) @@ -260,7 +266,6 @@ class Exchanger(threading.Thread): except KeyError: pass self.parent.set_currencies(quote_currencies) - # print "updating exchange rate", self.quote_currencies["USD"] def update_lb(self): try: @@ -311,9 +316,6 @@ class Exchanger(threading.Thread): 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): return decimal.Decimal(str(response[str(quote_id)]["15m"])) def _lookup_rate_cb(self, response, quote_id): @@ -499,7 +501,7 @@ class Plugin(BasePlugin): except KeyError: 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) elif cur_exchange == "BitcoinVenezuela": item.setText(5, tx_BTCVEN_val) @@ -527,10 +529,7 @@ class Plugin(BasePlugin): combo_ex = QComboBox() hist_checkbox = QCheckBox() hist_checkbox.setEnabled(False) - if self.config.get('history_rates', 'unchecked') == 'unchecked': - hist_checkbox.setChecked(False) - else: - hist_checkbox.setChecked(True) + hist_checkbox.setChecked(self.config.get('history_rates', 'unchecked') != 'unchecked') ok_button = QPushButton(_("OK")) def on_change(x): @@ -541,15 +540,10 @@ class Plugin(BasePlugin): if cur_request != self.fiat_unit(): self.config.set_key('currency', cur_request, True) cur_exchange = self.config.get('use_exchange', "Blockchain") - if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"): - hist_checkbox.setEnabled(True) - elif cur_request == "VEF" and (cur_exchange == "BitcoinVenezuela"): - hist_checkbox.setEnabled(True) - elif cur_request == "ARS" and (cur_exchange == "BitcoinVenezuela"): + if (cur_exchange, cur_request) in EXCH_SUPPORT_HIST: hist_checkbox.setEnabled(True) else: - hist_checkbox.setChecked(False) - hist_checkbox.setEnabled(False) + disable_check() self.win.update_status() try: self.fiat_button @@ -570,16 +564,8 @@ class Plugin(BasePlugin): combo.clear() self.exchanger.query_rates.set() cur_currency = self.fiat_unit() - if cur_request == "CoinDesk" or cur_request == "Winkdex": - if cur_currency == "USD": - 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() + if (cur_request, cur_currency) in EXCH_SUPPORT_HIST: + hist_checkbox.setEnabled(True) else: disable_check() set_currencies(combo) @@ -598,16 +584,12 @@ class Plugin(BasePlugin): def set_hist_check(hist_checkbox): cur_exchange = self.config.get('use_exchange', "Blockchain") - if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex": - hist_checkbox.setEnabled(True) - elif cur_exchange == "BitcoinVenezuela": - hist_checkbox.setEnabled(True) - else: - hist_checkbox.setEnabled(False) + hist_checkbox.setEnabled(cur_exchange in ["CoinDesk", "Winkdex", "BitcoinVenezuela"]) def set_currencies(combo): - current_currency = self.fiat_unit() try: + combo.blockSignals(True) + current_currency = self.fiat_unit() combo.clear() except Exception: return @@ -616,6 +598,7 @@ class Plugin(BasePlugin): index = self.currencies.index(current_currency) except Exception: index = 0 + combo.blockSignals(False) combo.setCurrentIndex(index) def set_exchanges(combo_ex): @@ -631,6 +614,8 @@ class Plugin(BasePlugin): combo_ex.setCurrentIndex(index) def ok_clicked(): + if self.config.get('use_exchange', "Blockchain") in ["CoinDesk", "itBit"]: + self.exchanger.query_rates.set() d.accept(); set_exchanges(combo_ex)