[modules/stock] Revert to using stock API

Many thanks to @mschwartz for pointing out how to!

see #453
This commit is contained in:
Tobias Witek 2019-10-14 21:19:39 +02:00
parent e1f8ed6806
commit e6d36ffd96

View file

@ -9,7 +9,6 @@ Requires the following python packages:
Parameters: Parameters:
* stock.symbols : Comma-separated list of symbols to fetch * stock.symbols : Comma-separated list of symbols to fetch
* stock.change : Should we fetch change in stock value (defaults to True) * stock.change : Should we fetch change in stock value (defaults to True)
* stock.token : Your API token registered at https://worldtradingdata.com
""" """
import bumblebee.input import bumblebee.input
@ -29,7 +28,6 @@ class Module(bumblebee.engine.Module):
) )
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._token = self.parameter('token', None)
self._value = None self._value = None
self.interval(60) self.interval(60)
@ -39,19 +37,18 @@ class Module(bumblebee.engine.Module):
return 'n/a' return 'n/a'
data = json.loads(self._value) data = json.loads(self._value)
for symbol in data['data']: for symbol in data['quoteResponse']['result']:
val = 'day_change' if self._change else 'price' valkey = 'regularMarketChange' if self._change else 'regularMarketPrice'
results.append('{} {}{}'.format(symbol['symbol'], symbol[val], symbol['currency'])) sym = 'n/a' if not 'symbol' in symbol else symbol['symbol']
currency = 'USD' if not 'currency' in symbol else symbol['currency']
val = 'n/a' if not valkey in symbol else '{:.2f}'.format(symbol[valkey])
results.append('{} {} {}'.format(sym, val, currency))
return u' '.join(results) return u' '.join(results)
def fetch(self): def fetch(self):
if not self._token:
logging.error('please specify a token')
return None
if self._symbols: if self._symbols:
url = 'https://api.worldtradingdata.com/api/v1/stock?' url = 'https://query1.finance.yahoo.com/v7/finance/quote?symbols='
url += 'api_token={}'.format(self._token) url += self._symbols + '&fields=regularMarketPrice,currency,regularMarketChange'
url += '&symbol={}'.format(self._symbols)
return requests.get(url).text.strip() return requests.get(url).text.strip()
else: else:
logging.error('unable to retrieve stock exchange rate') logging.error('unable to retrieve stock exchange rate')