[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:
parent
4a28355507
commit
e1f8ed6806
1 changed files with 16 additions and 22 deletions
|
@ -1,7 +1,7 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
# pylint: disable=C0111,R0903
|
||||
|
||||
"""Display a stock quote from yahoo finance.
|
||||
"""Display a stock quote from worldtradingdata.com
|
||||
|
||||
Requires the following python packages:
|
||||
* requests
|
||||
|
@ -9,7 +9,7 @@ Requires the following python packages:
|
|||
Parameters:
|
||||
* stock.symbols : Comma-separated list of symbols to fetch
|
||||
* 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
|
||||
|
@ -17,6 +17,7 @@ import bumblebee.output
|
|||
import bumblebee.engine
|
||||
import bumblebee.util
|
||||
|
||||
import json
|
||||
import requests
|
||||
|
||||
import logging
|
||||
|
@ -28,36 +29,29 @@ class Module(bumblebee.engine.Module):
|
|||
)
|
||||
self._symbols = self.parameter('symbols', '')
|
||||
self._change = bumblebee.util.asbool(self.parameter('change', True))
|
||||
self._currencies = self.parameter('currencies', None)
|
||||
self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv'
|
||||
self._value = self.fetch()
|
||||
self._token = self.parameter('token', None)
|
||||
self._value = None
|
||||
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):
|
||||
results = []
|
||||
if not self._value:
|
||||
return 'n/a'
|
||||
for i, val in enumerate(self._value.split('\n')):
|
||||
try:
|
||||
currency_symbol = self._currencies[i]
|
||||
except:
|
||||
currency_symbol = '$'
|
||||
results.append('%s%s' % (currency_symbol, val))
|
||||
data = json.loads(self._value)
|
||||
|
||||
for symbol in data['data']:
|
||||
val = 'day_change' if self._change else 'price'
|
||||
results.append('{} {}{}'.format(symbol['symbol'], symbol[val], symbol['currency']))
|
||||
return u' '.join(results)
|
||||
|
||||
def fetch(self):
|
||||
if not self._token:
|
||||
logging.error('please specify a token')
|
||||
return None
|
||||
if self._symbols:
|
||||
url = self._baseurl
|
||||
url += '?s=%s&f=l1' % self._symbols
|
||||
if self._change:
|
||||
url += 'c1'
|
||||
url = 'https://api.worldtradingdata.com/api/v1/stock?'
|
||||
url += 'api_token={}'.format(self._token)
|
||||
url += '&symbol={}'.format(self._symbols)
|
||||
return requests.get(url).text.strip()
|
||||
else:
|
||||
logging.error('unable to retrieve stock exchange rate')
|
||||
|
|
Loading…
Reference in a new issue