[modules/stock] Use www.worldtradingdata.com

Since yahoo.com doesn't seem to offer a free API anymore, use
www.worldtradingdata.com instead. NOTE: This needs an API token!
This commit is contained in:
Tobias Witek 2019-10-14 20:44:28 +02:00
parent 4a28355507
commit e1f8ed6806

View file

@ -1,7 +1,7 @@
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# pylint: disable=C0111,R0903 # pylint: disable=C0111,R0903
"""Display a stock quote from yahoo finance. """Display a stock quote from worldtradingdata.com
Requires the following python packages: Requires the following python packages:
* requests * requests
@ -9,7 +9,7 @@ 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.currencies : List of symbols to go with the values (default $) * stock.token : Your API token registered at https://worldtradingdata.com
""" """
import bumblebee.input import bumblebee.input
@ -17,6 +17,7 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import bumblebee.util import bumblebee.util
import json
import requests import requests
import logging import logging
@ -28,36 +29,29 @@ 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._currencies = self.parameter('currencies', None) self._token = self.parameter('token', None)
self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._value = None
self._value = self.fetch()
self.interval(60) self.interval(60)
if not self._currencies:
self._currencies = '$' * len(self._symbols)
# The currencies could be unicode, like the € symbol. Convert to a unicode object.
if hasattr(self._currencies, 'decode'):
self._currencies = self._currencies.decode('utf-8', 'ignore')
def value(self, widget): def value(self, widget):
results = [] results = []
if not self._value: if not self._value:
return 'n/a' return 'n/a'
for i, val in enumerate(self._value.split('\n')): data = json.loads(self._value)
try:
currency_symbol = self._currencies[i] for symbol in data['data']:
except: val = 'day_change' if self._change else 'price'
currency_symbol = '$' results.append('{} {}{}'.format(symbol['symbol'], symbol[val], symbol['currency']))
results.append('%s%s' % (currency_symbol, val))
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 = self._baseurl url = 'https://api.worldtradingdata.com/api/v1/stock?'
url += '?s=%s&f=l1' % self._symbols url += 'api_token={}'.format(self._token)
if self._change: url += '&symbol={}'.format(self._symbols)
url += 'c1'
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')