Clean up exchange handling in FX plugin
This commit is contained in:
parent
4e458b5639
commit
5be16ad5ca
|
@ -41,10 +41,6 @@ class Exchanger(ThreadJob):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.quote_currencies = None
|
self.quote_currencies = None
|
||||||
self.timeout = 0
|
self.timeout = 0
|
||||||
self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
|
|
||||||
self.parent.exchanges = EXCHANGES
|
|
||||||
#self.parent.win.emit(SIGNAL("refresh_exchanges_combo()"))
|
|
||||||
#self.parent.win.emit(SIGNAL("refresh_currencies_combo()"))
|
|
||||||
|
|
||||||
def get_json(self, site, get_string):
|
def get_json(self, site, get_string):
|
||||||
resp = requests.request('GET', 'https://' + site + get_string, headers={"User-Agent":"Electrum"})
|
resp = requests.request('GET', 'https://' + site + get_string, headers={"User-Agent":"Electrum"})
|
||||||
|
@ -59,7 +55,6 @@ class Exchanger(ThreadJob):
|
||||||
return btc_amount * Decimal(str(quote_currencies[quote_currency]))
|
return btc_amount * Decimal(str(quote_currencies[quote_currency]))
|
||||||
|
|
||||||
def update_rate(self):
|
def update_rate(self):
|
||||||
self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
|
|
||||||
update_rates = {
|
update_rates = {
|
||||||
"BitcoinAverage": self.update_ba,
|
"BitcoinAverage": self.update_ba,
|
||||||
"BitcoinVenezuela": self.update_bv,
|
"BitcoinVenezuela": self.update_bv,
|
||||||
|
@ -77,7 +72,7 @@ class Exchanger(ThreadJob):
|
||||||
"Winkdex": self.update_wd,
|
"Winkdex": self.update_wd,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
rates = update_rates[self.use_exchange]()
|
rates = update_rates[self.parent.exchange]()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.parent.print_error(e)
|
self.parent.print_error(e)
|
||||||
rates = {}
|
rates = {}
|
||||||
|
@ -169,8 +164,8 @@ class Plugin(BasePlugin):
|
||||||
|
|
||||||
def __init__(self,a,b):
|
def __init__(self,a,b):
|
||||||
BasePlugin.__init__(self,a,b)
|
BasePlugin.__init__(self,a,b)
|
||||||
|
self.exchange = self.config.get('use_exchange', "Blockchain")
|
||||||
self.currencies = [self.fiat_unit()]
|
self.currencies = [self.fiat_unit()]
|
||||||
self.exchanges = [self.config.get('use_exchange', "Blockchain")]
|
|
||||||
# Do price discovery
|
# Do price discovery
|
||||||
self.exchanger = Exchanger(self)
|
self.exchanger = Exchanger(self)
|
||||||
self.win = None
|
self.win = None
|
||||||
|
@ -243,7 +238,6 @@ class Plugin(BasePlugin):
|
||||||
|
|
||||||
def create_fiat_balance_text(self, btc_balance):
|
def create_fiat_balance_text(self, btc_balance):
|
||||||
quote_currency = self.fiat_unit()
|
quote_currency = self.fiat_unit()
|
||||||
self.exchanger.use_exchange = self.config.get("use_exchange", "Blockchain")
|
|
||||||
cur_rate = self.exchanger.exchange(Decimal("1.0"), quote_currency)
|
cur_rate = self.exchanger.exchange(Decimal("1.0"), quote_currency)
|
||||||
if cur_rate is None:
|
if cur_rate is None:
|
||||||
quote_text = ""
|
quote_text = ""
|
||||||
|
@ -261,7 +255,6 @@ class Plugin(BasePlugin):
|
||||||
tx_list[tx_hash] = {'value': value, 'timestamp': timestamp }
|
tx_list[tx_hash] = {'value': value, 'timestamp': timestamp }
|
||||||
|
|
||||||
self.tx_list = tx_list
|
self.tx_list = tx_list
|
||||||
self.cur_exchange = self.config.get('use_exchange', "Blockchain")
|
|
||||||
self.set_network(wallet.network)
|
self.set_network(wallet.network)
|
||||||
t = threading.Thread(target=self.request_history_rates, args=())
|
t = threading.Thread(target=self.request_history_rates, args=())
|
||||||
t.setDaemon(True)
|
t.setDaemon(True)
|
||||||
|
@ -284,17 +277,17 @@ class Plugin(BasePlugin):
|
||||||
return
|
return
|
||||||
maxtimestr = datetime.datetime.now().strftime('%Y-%m-%d')
|
maxtimestr = datetime.datetime.now().strftime('%Y-%m-%d')
|
||||||
|
|
||||||
if self.cur_exchange == "CoinDesk":
|
if self.exchange == "CoinDesk":
|
||||||
try:
|
try:
|
||||||
self.resp_hist = self.exchanger.get_json('api.coindesk.com', "/v1/bpi/historical/close.json?start=" + mintimestr + "&end=" + maxtimestr)
|
self.resp_hist = self.exchanger.get_json('api.coindesk.com', "/v1/bpi/historical/close.json?start=" + mintimestr + "&end=" + maxtimestr)
|
||||||
except Exception:
|
except Exception:
|
||||||
return
|
return
|
||||||
elif self.cur_exchange == "Winkdex":
|
elif self.exchange == "Winkdex":
|
||||||
try:
|
try:
|
||||||
self.resp_hist = self.exchanger.get_json('winkdex.com', "/api/v0/series?start_time=1342915200")['series'][0]['results']
|
self.resp_hist = self.exchanger.get_json('winkdex.com', "/api/v0/series?start_time=1342915200")['series'][0]['results']
|
||||||
except Exception:
|
except Exception:
|
||||||
return
|
return
|
||||||
elif self.cur_exchange == "BitcoinVenezuela":
|
elif self.exchange == "BitcoinVenezuela":
|
||||||
cur_currency = self.fiat_unit()
|
cur_currency = self.fiat_unit()
|
||||||
if cur_currency == "VEF":
|
if cur_currency == "VEF":
|
||||||
try:
|
try:
|
||||||
|
@ -336,13 +329,13 @@ class Plugin(BasePlugin):
|
||||||
pass
|
pass
|
||||||
tx_time = int(tx_info['timestamp'])
|
tx_time = int(tx_info['timestamp'])
|
||||||
tx_value = Decimal(str(tx_info['value'])) / COIN
|
tx_value = Decimal(str(tx_info['value'])) / COIN
|
||||||
if self.cur_exchange == "CoinDesk":
|
if self.exchange == "CoinDesk":
|
||||||
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
|
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
|
||||||
try:
|
try:
|
||||||
tx_fiat_val = "%.2f %s" % (tx_value * Decimal(self.resp_hist['bpi'][tx_time_str]), "USD")
|
tx_fiat_val = "%.2f %s" % (tx_value * Decimal(self.resp_hist['bpi'][tx_time_str]), "USD")
|
||||||
except KeyError:
|
except KeyError:
|
||||||
tx_fiat_val = "%.2f %s" % (self.btc_rate * Decimal(str(tx_info['value']))/COIN , "USD")
|
tx_fiat_val = "%.2f %s" % (self.btc_rate * Decimal(str(tx_info['value']))/COIN , "USD")
|
||||||
elif self.cur_exchange == "Winkdex":
|
elif self.exchange == "Winkdex":
|
||||||
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d') + "T16:00:00-04:00"
|
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d') + "T16:00:00-04:00"
|
||||||
try:
|
try:
|
||||||
tx_rate = self.resp_hist[[x['timestamp'] for x in self.resp_hist].index(tx_time_str)]['price']
|
tx_rate = self.resp_hist[[x['timestamp'] for x in self.resp_hist].index(tx_time_str)]['price']
|
||||||
|
@ -351,7 +344,7 @@ class Plugin(BasePlugin):
|
||||||
tx_fiat_val = "%.2f %s" % (self.btc_rate * Decimal(tx_info['value'])/COIN , "USD")
|
tx_fiat_val = "%.2f %s" % (self.btc_rate * Decimal(tx_info['value'])/COIN , "USD")
|
||||||
except KeyError:
|
except KeyError:
|
||||||
tx_fiat_val = _("No data")
|
tx_fiat_val = _("No data")
|
||||||
elif self.cur_exchange == "BitcoinVenezuela":
|
elif self.exchange == "BitcoinVenezuela":
|
||||||
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
|
tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
|
||||||
try:
|
try:
|
||||||
num = self.resp_hist[tx_time_str].replace(',','')
|
num = self.resp_hist[tx_time_str].replace(',','')
|
||||||
|
@ -381,6 +374,8 @@ class Plugin(BasePlugin):
|
||||||
layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
|
layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
|
||||||
combo = QComboBox()
|
combo = QComboBox()
|
||||||
combo_ex = QComboBox()
|
combo_ex = QComboBox()
|
||||||
|
combo_ex.addItems(EXCHANGES)
|
||||||
|
combo_ex.setCurrentIndex(combo_ex.findText(self.exchange))
|
||||||
hist_checkbox = QCheckBox()
|
hist_checkbox = QCheckBox()
|
||||||
hist_checkbox.setEnabled(False)
|
hist_checkbox.setEnabled(False)
|
||||||
hist_checkbox.setChecked(self.config.get('history_rates', 'unchecked') != 'unchecked')
|
hist_checkbox.setChecked(self.config.get('history_rates', 'unchecked') != 'unchecked')
|
||||||
|
@ -393,8 +388,7 @@ class Plugin(BasePlugin):
|
||||||
return
|
return
|
||||||
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")
|
if (self.exchange, cur_request) in EXCH_SUPPORT_HIST:
|
||||||
if (cur_exchange, cur_request) in EXCH_SUPPORT_HIST:
|
|
||||||
hist_checkbox.setEnabled(True)
|
hist_checkbox.setEnabled(True)
|
||||||
else:
|
else:
|
||||||
disable_check()
|
disable_check()
|
||||||
|
@ -410,15 +404,16 @@ class Plugin(BasePlugin):
|
||||||
hist_checkbox.setChecked(False)
|
hist_checkbox.setChecked(False)
|
||||||
hist_checkbox.setEnabled(False)
|
hist_checkbox.setEnabled(False)
|
||||||
|
|
||||||
def on_change_ex(x):
|
def on_change_ex(exchange):
|
||||||
cur_request = str(self.exchanges[x])
|
exchange = str(exchange)
|
||||||
if cur_request != self.config.get('use_exchange', "Blockchain"):
|
if exchange != self.exchange:
|
||||||
self.config.set_key('use_exchange', cur_request, True)
|
self.exchange = exchange
|
||||||
|
self.config.set_key('use_exchange', exchange, True)
|
||||||
self.currencies = []
|
self.currencies = []
|
||||||
combo.clear()
|
combo.clear()
|
||||||
self.timeout = 0
|
self.timeout = 0
|
||||||
cur_currency = self.fiat_unit()
|
cur_currency = self.fiat_unit()
|
||||||
if (cur_request, cur_currency) in EXCH_SUPPORT_HIST:
|
if (exchange, cur_currency) in EXCH_SUPPORT_HIST:
|
||||||
hist_checkbox.setEnabled(True)
|
hist_checkbox.setEnabled(True)
|
||||||
else:
|
else:
|
||||||
disable_check()
|
disable_check()
|
||||||
|
@ -435,8 +430,7 @@ class Plugin(BasePlugin):
|
||||||
self.win.history_list.setColumnCount(6)
|
self.win.history_list.setColumnCount(6)
|
||||||
|
|
||||||
def set_hist_check(hist_checkbox):
|
def set_hist_check(hist_checkbox):
|
||||||
cur_exchange = self.config.get('use_exchange', "Blockchain")
|
hist_checkbox.setEnabled(self.exchange in ["CoinDesk", "Winkdex", "BitcoinVenezuela"])
|
||||||
hist_checkbox.setEnabled(cur_exchange in ["CoinDesk", "Winkdex", "BitcoinVenezuela"])
|
|
||||||
|
|
||||||
def set_currencies(combo):
|
def set_currencies(combo):
|
||||||
try:
|
try:
|
||||||
|
@ -453,24 +447,11 @@ class Plugin(BasePlugin):
|
||||||
combo.blockSignals(False)
|
combo.blockSignals(False)
|
||||||
combo.setCurrentIndex(index)
|
combo.setCurrentIndex(index)
|
||||||
|
|
||||||
def set_exchanges(combo_ex):
|
|
||||||
try:
|
|
||||||
combo_ex.clear()
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
combo_ex.addItems(self.exchanges)
|
|
||||||
try:
|
|
||||||
index = self.exchanges.index(self.config.get('use_exchange', "Blockchain"))
|
|
||||||
except Exception:
|
|
||||||
index = 0
|
|
||||||
combo_ex.setCurrentIndex(index)
|
|
||||||
|
|
||||||
def ok_clicked():
|
def ok_clicked():
|
||||||
if self.config.get('use_exchange', "Blockchain") in ["CoinDesk", "itBit"]:
|
if self.exchange in ["CoinDesk", "itBit"]:
|
||||||
self.timeout = 0
|
self.timeout = 0
|
||||||
d.accept();
|
d.accept();
|
||||||
|
|
||||||
set_exchanges(combo_ex)
|
|
||||||
set_currencies(combo)
|
set_currencies(combo)
|
||||||
set_hist_check(hist_checkbox)
|
set_hist_check(hist_checkbox)
|
||||||
combo.currentIndexChanged.connect(on_change)
|
combo.currentIndexChanged.connect(on_change)
|
||||||
|
|
Loading…
Reference in New Issue