[module/stock] Error message if stock service unavailable

If the stock rate cannot be retrieved for some reason, print an error
message in the log and present the value as "n/a".

fixes #219
This commit is contained in:
Tobias Witek 2018-01-07 19:37:31 +01:00
parent 13d27d4b81
commit a6b7e329c7

View file

@ -19,13 +19,15 @@ import bumblebee.util
import requests import requests
import logging
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(engine, config, super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.value) bumblebee.output.Widget(full_text=self.value)
) )
self._symbols = self.parameter("symbols", "") self._symbols = self.parameter('symbols', '')
self._change = bumblebee.util.asbool(self.parameter("change", True)) self._change = bumblebee.util.asbool(self.parameter('change', True))
self._currencies = self.parameter('currencies', None) self._currencies = self.parameter('currencies', None)
self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv'
self._value = self.fetch() self._value = self.fetch()
@ -34,11 +36,13 @@ class Module(bumblebee.engine.Module):
self._currencies = '$' * len(self._symbols) self._currencies = '$' * len(self._symbols)
# The currencies could be unicode, like the € symbol. Convert to a unicode object. # The currencies could be unicode, like the € symbol. Convert to a unicode object.
if hasattr(self._currencies, "decode"): if hasattr(self._currencies, 'decode'):
self._currencies = self._currencies.decode("utf-8", "ignore") self._currencies = self._currencies.decode('utf-8', 'ignore')
def value(self, widget): def value(self, widget):
results = [] results = []
if not self._value:
return 'n/a'
for i, val in enumerate(self._value.split('\n')): for i, val in enumerate(self._value.split('\n')):
try: try:
currency_symbol = self._currencies[i] currency_symbol = self._currencies[i]
@ -55,7 +59,8 @@ class Module(bumblebee.engine.Module):
url += 'c1' url += 'c1'
return requests.get(url).text.strip() return requests.get(url).text.strip()
else: else:
return '' logging.error('unable to retrieve stock exchange rate')
return None
def update(self, widgets): def update(self, widgets):
self._value = self.fetch() self._value = self.fetch()