2017-05-09 19:23:18 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Display a stock quote from yahoo finance.
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* stock.symbols : Comma-separated list of symbols to fetch
|
|
|
|
* stock.change : Should we fetch change in stock value (defaults to True)
|
2017-05-10 21:38:44 +02:00
|
|
|
* stock.currencies : List of symbols to go with the values (default $)
|
2017-05-09 19:23:18 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
2017-07-08 06:44:08 +02:00
|
|
|
import bumblebee.util
|
|
|
|
|
2017-05-09 19:23:18 +02:00
|
|
|
import requests
|
|
|
|
|
2018-01-07 19:37:31 +01:00
|
|
|
import logging
|
|
|
|
|
2017-05-09 19:23:18 +02:00
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(engine, config,
|
|
|
|
bumblebee.output.Widget(full_text=self.value)
|
|
|
|
)
|
2018-01-07 19:37:31 +01:00
|
|
|
self._symbols = self.parameter('symbols', '')
|
|
|
|
self._change = bumblebee.util.asbool(self.parameter('change', True))
|
2017-05-10 21:38:44 +02:00
|
|
|
self._currencies = self.parameter('currencies', None)
|
2017-05-09 19:23:18 +02:00
|
|
|
self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv'
|
|
|
|
self._value = self.fetch()
|
|
|
|
|
2017-05-10 21:38:44 +02:00
|
|
|
if not self._currencies:
|
|
|
|
self._currencies = '$' * len(self._symbols)
|
|
|
|
|
|
|
|
# The currencies could be unicode, like the € symbol. Convert to a unicode object.
|
2018-01-07 19:37:31 +01:00
|
|
|
if hasattr(self._currencies, 'decode'):
|
|
|
|
self._currencies = self._currencies.decode('utf-8', 'ignore')
|
2017-05-10 21:38:44 +02:00
|
|
|
|
2017-05-09 19:23:18 +02:00
|
|
|
def value(self, widget):
|
2017-05-10 21:38:44 +02:00
|
|
|
results = []
|
2018-01-07 19:37:31 +01:00
|
|
|
if not self._value:
|
|
|
|
return 'n/a'
|
2017-05-10 21:38:44 +02:00
|
|
|
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))
|
|
|
|
return u' '.join(results)
|
2017-05-09 19:23:18 +02:00
|
|
|
|
|
|
|
def fetch(self):
|
|
|
|
if self._symbols:
|
|
|
|
url = self._baseurl
|
|
|
|
url += '?s=%s&f=l1' % self._symbols
|
|
|
|
if self._change:
|
|
|
|
url += 'c1'
|
|
|
|
return requests.get(url).text.strip()
|
|
|
|
else:
|
2018-01-07 19:37:31 +01:00
|
|
|
logging.error('unable to retrieve stock exchange rate')
|
|
|
|
return None
|
2017-05-09 19:23:18 +02:00
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
self._value = self.fetch()
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|