mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
exchange rates: factorize getter functions
This commit is contained in:
parent
a42743ca2d
commit
0b0fb45f62
1 changed files with 60 additions and 217 deletions
|
@ -98,9 +98,13 @@ class Exchanger(threading.Thread):
|
||||||
"Winkdex": self.update_wd,
|
"Winkdex": self.update_wd,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
update_rates[self.use_exchange]()
|
rates = update_rates[self.use_exchange]()
|
||||||
except KeyError:
|
except Exception as e:
|
||||||
return
|
self.parent.print_error(e)
|
||||||
|
rates = {}
|
||||||
|
with self.lock:
|
||||||
|
self.quote_currencies = rates
|
||||||
|
self.parent.set_currencies(rates)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.is_running = True
|
self.is_running = True
|
||||||
|
@ -111,28 +115,15 @@ class Exchanger(threading.Thread):
|
||||||
|
|
||||||
|
|
||||||
def update_cd(self):
|
def update_cd(self):
|
||||||
try:
|
resp_currencies = self.get_json('api.coindesk.com', "/v1/bpi/supported-currencies.json")
|
||||||
resp_currencies = self.get_json('api.coindesk.com', "/v1/bpi/supported-currencies.json")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing coindesk")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
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")
|
current_cur = self.parent.config.get("currency", "EUR")
|
||||||
if current_cur in quote_currencies:
|
if current_cur in quote_currencies:
|
||||||
try:
|
resp_rate = self.get_json('api.coindesk.com', "/v1/bpi/currentprice/" + str(current_cur) + ".json")
|
||||||
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"]))
|
||||||
quote_currencies[str(current_cur)] = decimal.Decimal(str(resp_rate["bpi"][str(current_cur)]["rate_float"]))
|
return quote_currencies
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_ib(self):
|
def update_ib(self):
|
||||||
available_currencies = ["USD", "EUR", "SGD"]
|
available_currencies = ["USD", "EUR", "SGD"]
|
||||||
|
@ -141,242 +132,94 @@ class Exchanger(threading.Thread):
|
||||||
quote_currencies[cur] = 0.0
|
quote_currencies[cur] = 0.0
|
||||||
current_cur = self.parent.config.get("currency", "EUR")
|
current_cur = self.parent.config.get("currency", "EUR")
|
||||||
if current_cur in available_currencies:
|
if current_cur in available_currencies:
|
||||||
try:
|
resp_rate = self.get_json('api.itbit.com', "/v1/markets/XBT" + str(current_cur) + "/ticker")
|
||||||
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"]))
|
||||||
quote_currencies[str(current_cur)] = decimal.Decimal(str(resp_rate["lastPrice"]))
|
return quote_currencies
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing itbit")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_wd(self):
|
def update_wd(self):
|
||||||
try:
|
winkresp = self.get_json('winkdex.com', "/api/v0/price")
|
||||||
winkresp = self.get_json('winkdex.com', "/api/v0/price")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing winkdex")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {"USD": 0.0}
|
quote_currencies = {"USD": 0.0}
|
||||||
usdprice = decimal.Decimal(str(winkresp["price"]))/decimal.Decimal("100.0")
|
usdprice = decimal.Decimal(str(winkresp["price"]))/decimal.Decimal("100.0")
|
||||||
try:
|
quote_currencies["USD"] = usdprice
|
||||||
quote_currencies["USD"] = usdprice
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_cv(self):
|
def update_cv(self):
|
||||||
try:
|
jsonresp = self.get_json('www.cavirtex.com', "/api/CAD/ticker.json")
|
||||||
jsonresp = self.get_json('www.cavirtex.com', "/api/CAD/ticker.json")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing cavirtex")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {"CAD": 0.0}
|
quote_currencies = {"CAD": 0.0}
|
||||||
cadprice = jsonresp["last"]
|
cadprice = jsonresp["last"]
|
||||||
try:
|
quote_currencies["CAD"] = decimal.Decimal(str(cadprice))
|
||||||
quote_currencies["CAD"] = decimal.Decimal(str(cadprice))
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_bm(self):
|
def update_bm(self):
|
||||||
try:
|
jsonresp = self.get_json('www.bitmarket.pl', "/json/BTCPLN/ticker.json")
|
||||||
jsonresp = self.get_json('www.bitmarket.pl', "/json/BTCPLN/ticker.json")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing bitmarket")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {"PLN": 0.0}
|
quote_currencies = {"PLN": 0.0}
|
||||||
pln_price = jsonresp["last"]
|
pln_price = jsonresp["last"]
|
||||||
try:
|
quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
|
||||||
quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_bx(self):
|
def update_bx(self):
|
||||||
try:
|
jsonresp = self.get_json('pln.bitcurex.com', "/data/ticker.json")
|
||||||
jsonresp = self.get_json('pln.bitcurex.com', "/data/ticker.json")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing bitcurex")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {"PLN": 0.0}
|
quote_currencies = {"PLN": 0.0}
|
||||||
pln_price = jsonresp["last"]
|
pln_price = jsonresp["last"]
|
||||||
try:
|
quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
|
||||||
quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_CNY(self):
|
def update_CNY(self):
|
||||||
try:
|
jsonresp = self.get_json('data.btcchina.com', "/data/ticker")
|
||||||
jsonresp = self.get_json('data.btcchina.com', "/data/ticker")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing btcchina")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {"CNY": 0.0}
|
quote_currencies = {"CNY": 0.0}
|
||||||
cnyprice = jsonresp["ticker"]["last"]
|
cnyprice = jsonresp["ticker"]["last"]
|
||||||
try:
|
quote_currencies["CNY"] = decimal.Decimal(str(cnyprice))
|
||||||
quote_currencies["CNY"] = decimal.Decimal(str(cnyprice))
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_bp(self):
|
def update_bp(self):
|
||||||
try:
|
jsonresp = self.get_json('bitpay.com', "/api/rates")
|
||||||
jsonresp = self.get_json('bitpay.com', "/api/rates")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing bitpay")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
for r in jsonresp:
|
||||||
for r in jsonresp:
|
quote_currencies[str(r["code"])] = decimal.Decimal(r["rate"])
|
||||||
quote_currencies[str(r["code"])] = decimal.Decimal(r["rate"])
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_cb(self):
|
def update_cb(self):
|
||||||
try:
|
jsonresp = self.get_json('coinbase.com', "/api/v1/currencies/exchange_rates")
|
||||||
jsonresp = self.get_json('coinbase.com', "/api/v1/currencies/exchange_rates")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing coinbase")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
for r in jsonresp:
|
||||||
for r in jsonresp:
|
if r[:7] == "btc_to_":
|
||||||
if r[:7] == "btc_to_":
|
quote_currencies[r[7:].upper()] = self._lookup_rate_cb(jsonresp, r)
|
||||||
quote_currencies[r[7:].upper()] = self._lookup_rate_cb(jsonresp, r)
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
|
|
||||||
def update_bc(self):
|
def update_bc(self):
|
||||||
try:
|
jsonresp = self.get_json('blockchain.info', "/ticker")
|
||||||
jsonresp = self.get_json('blockchain.info', "/ticker")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing blockchain")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
for r in jsonresp:
|
||||||
for r in jsonresp:
|
quote_currencies[r] = self._lookup_rate(jsonresp, r)
|
||||||
quote_currencies[r] = self._lookup_rate(jsonresp, r)
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_lb(self):
|
def update_lb(self):
|
||||||
try:
|
jsonresp = self.get_json('localbitcoins.com', "/bitcoinaverage/ticker-all-currencies/")
|
||||||
jsonresp = self.get_json('localbitcoins.com', "/bitcoinaverage/ticker-all-currencies/")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing localbitcoins")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
for r in jsonresp:
|
||||||
for r in jsonresp:
|
quote_currencies[r] = self._lookup_rate_lb(jsonresp, r)
|
||||||
quote_currencies[r] = self._lookup_rate_lb(jsonresp, r)
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
|
|
||||||
def update_bv(self):
|
def update_bv(self):
|
||||||
try:
|
jsonresp = self.get_json('api.bitcoinvenezuela.com', "/")
|
||||||
jsonresp = self.get_json('api.bitcoinvenezuela.com', "/")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing bitcoinvenezuela")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
for r in jsonresp["BTC"]:
|
||||||
for r in jsonresp["BTC"]:
|
quote_currencies[r] = Decimal(jsonresp["BTC"][r])
|
||||||
quote_currencies[r] = Decimal(jsonresp["BTC"][r])
|
return quote_currencies
|
||||||
|
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
print ("KeyError")
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
|
|
||||||
def update_bpl(self):
|
def update_bpl(self):
|
||||||
try:
|
jsonresp = self.get_json('btcparalelo.com', "/api/price")
|
||||||
jsonresp = self.get_json('btcparalelo.com', "/api/price")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing btcparalelo")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
|
|
||||||
quote_currencies = {}
|
quote_currencies = {}
|
||||||
try:
|
quote_currencies = {"VEF": Decimal(jsonresp["price"])}
|
||||||
quote_currencies = {"VEF": Decimal(jsonresp["price"])}
|
return quote_currencies
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
print ("KeyError")
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
def update_ba(self):
|
|
||||||
try:
|
|
||||||
jsonresp = self.get_json('api.bitcoinaverage.com', "/ticker/global/all")
|
|
||||||
except SSLError:
|
|
||||||
print("SSL Error when accesing bitcoinaverage")
|
|
||||||
return
|
|
||||||
except Exception:
|
|
||||||
return
|
|
||||||
quote_currencies = {}
|
|
||||||
try:
|
|
||||||
for r in jsonresp:
|
|
||||||
if not r == "timestamp":
|
|
||||||
quote_currencies[r] = self._lookup_rate_ba(jsonresp, r)
|
|
||||||
with self.lock:
|
|
||||||
self.quote_currencies = quote_currencies
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self.parent.set_currencies(quote_currencies)
|
|
||||||
|
|
||||||
|
def update_ba(self):
|
||||||
|
jsonresp = self.get_json('api.bitcoinaverage.com', "/ticker/global/all")
|
||||||
|
quote_currencies = {}
|
||||||
|
for r in jsonresp:
|
||||||
|
if not r == "timestamp":
|
||||||
|
quote_currencies[r] = self._lookup_rate_ba(jsonresp, r)
|
||||||
|
return quote_currencies
|
||||||
|
|
||||||
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"]))
|
||||||
|
|
Loading…
Add table
Reference in a new issue