Improved error handling for exchange requests

This commit is contained in:
Neil Booth 2015-09-07 00:23:59 +09:00
parent c0b93c83ab
commit 154cdd1697
1 changed files with 23 additions and 17 deletions

View File

@ -46,34 +46,40 @@ class ExchangeBase(PrintError):
def name(self): def name(self):
return self.__class__.__name__ return self.__class__.__name__
def update(self, ccy): def update_safe(self, ccy):
self.print_error("getting fx quotes for", ccy)
try: try:
self.print_error("getting fx quotes for", ccy)
self.quotes = self.get_rates(ccy) self.quotes = self.get_rates(ccy)
self.print_error("received fx quotes") self.print_error("received fx quotes")
self.sig.emit(SIGNAL('fx_quotes')) self.sig.emit(SIGNAL('fx_quotes'))
except: except Exception, e:
traceback.print_exc(file=sys.stderr) self.print_error("failed fx quotes:", e)
self.print_error("failed to get fx quotes")
def history_ccys(self): def update(self, ccy):
return [] t = Thread(target=self.update_safe, args=(ccy,))
t.setDaemon(True)
t.start()
def set_history(self, ccy, history): def get_historical_rates_safe(self, ccy):
'''History is a map of "%Y-%m-%d" strings to values''' try:
self.history[ccy] = history self.print_error("requesting fx history for", ccy)
self.print_error("received fx history for", ccy) self.history[ccy] = self.historical_rates(ccy)
self.sig.emit(SIGNAL("fx_history")) self.print_error("received fx history for", ccy)
self.sig.emit(SIGNAL("fx_history"))
except Exception, e:
self.print_error("failed fx history:", e)
def get_historical_rates(self, ccy): def get_historical_rates(self, ccy):
result = self.history.get(ccy) result = self.history.get(ccy)
if not result and ccy in self.history_ccys(): if not result and ccy in self.history_ccys():
self.print_error("requesting historical rates for", ccy) t = Thread(target=self.get_historical_rates_safe, args=(ccy,))
t = Thread(target=self.historical_rates, args=(ccy,))
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()
return result return result
def history_ccys(self):
return []
def historical_rate(self, ccy, d_t): def historical_rate(self, ccy, d_t):
return self.history.get(ccy, {}).get(d_t.strftime('%Y-%m-%d')) return self.history.get(ccy, {}).get(d_t.strftime('%Y-%m-%d'))
@ -173,7 +179,7 @@ class CoinDesk(ExchangeBase):
query = ('/v1/bpi/historical/close.json?start=%s&end=%s' query = ('/v1/bpi/historical/close.json?start=%s&end=%s'
% (start, end)) % (start, end))
json = self.get_json('api.coindesk.com', query) json = self.get_json('api.coindesk.com', query)
self.set_history(ccy, json['bpi']) return json['bpi']
class itBit(ExchangeBase): class itBit(ExchangeBase):
def get_rates(self, ccy): def get_rates(self, ccy):
@ -201,8 +207,8 @@ class Winkdex(ExchangeBase):
json = self.get_json('winkdex.com', json = self.get_json('winkdex.com',
"/api/v0/series?start_time=1342915200") "/api/v0/series?start_time=1342915200")
history = json['series'][0]['results'] history = json['series'][0]['results']
self.set_history(ccy, dict([(h['timestamp'][:10], h['price'] / 100.0) return dict([(h['timestamp'][:10], h['price'] / 100.0)
for h in history])) for h in history])
class Plugin(BasePlugin, ThreadJob): class Plugin(BasePlugin, ThreadJob):