[modules/stock] update to latest API
This commit is contained in:
parent
6b0e73db9b
commit
63a2881be8
1 changed files with 25 additions and 26 deletions
|
@ -11,51 +11,50 @@ Parameters:
|
||||||
* stock.change : Should we fetch change in stock value (defaults to True)
|
* stock.change : Should we fetch change in stock value (defaults to True)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import bumblebee.input
|
|
||||||
import bumblebee.output
|
|
||||||
import bumblebee.engine
|
|
||||||
import bumblebee.util
|
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import requests
|
import urllib.request
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
import core.module
|
||||||
def __init__(self, engine, config):
|
import core.widget
|
||||||
super(Module, self).__init__(engine, config,
|
import core.decorators
|
||||||
bumblebee.output.Widget(full_text=self.value)
|
|
||||||
)
|
import util.format
|
||||||
self._symbols = self.parameter('symbols', '')
|
|
||||||
self._change = bumblebee.util.asbool(self.parameter('change', True))
|
class Module(core.module.Module):
|
||||||
self._value = None
|
@core.decorators.every(hours=1)
|
||||||
self.interval_factor(60)
|
def __init__(self, config):
|
||||||
self.interval(60)
|
super().__init__(config, core.widget.Widget(self.value))
|
||||||
|
|
||||||
|
self.__symbols = self.parameter('symbols', '')
|
||||||
|
self.__change = util.format.asbool(self.parameter('change', True))
|
||||||
|
self.__value = None
|
||||||
|
|
||||||
def value(self, widget):
|
def value(self, widget):
|
||||||
results = []
|
results = []
|
||||||
if not self._value:
|
if not self.__value:
|
||||||
return 'n/a'
|
return 'n/a'
|
||||||
data = json.loads(self._value)
|
data = json.loads(self.__value)
|
||||||
|
|
||||||
for symbol in data['quoteResponse']['result']:
|
for symbol in data['quoteResponse']['result']:
|
||||||
valkey = 'regularMarketChange' if self._change else 'regularMarketPrice'
|
valkey = 'regularMarketChange' if self.__change else 'regularMarketPrice'
|
||||||
sym = 'n/a' if not 'symbol' in symbol else symbol['symbol']
|
sym = symbol.get('symbol', 'n/a')
|
||||||
currency = 'USD' if not 'currency' in symbol else symbol['currency']
|
currency = symbol.get('currency', 'USD')
|
||||||
val = 'n/a' if not valkey in symbol else '{:.2f}'.format(symbol[valkey])
|
val = 'n/a' if not valkey in symbol else '{:.2f}'.format(symbol[valkey])
|
||||||
results.append('{} {} {}'.format(sym, val, currency))
|
results.append('{} {} {}'.format(sym, val, currency))
|
||||||
return u' '.join(results)
|
return u' '.join(results)
|
||||||
|
|
||||||
def fetch(self):
|
def fetch(self):
|
||||||
if self._symbols:
|
if self.__symbols:
|
||||||
url = 'https://query1.finance.yahoo.com/v7/finance/quote?symbols='
|
url = 'https://query1.finance.yahoo.com/v7/finance/quote?symbols='
|
||||||
url += self._symbols + '&fields=regularMarketPrice,currency,regularMarketChange'
|
url += self.__symbols + '&fields=regularMarketPrice,currency,regularMarketChange'
|
||||||
return requests.get(url).text.strip()
|
return urllib.request.urlopen(url).read().strip()
|
||||||
else:
|
else:
|
||||||
logging.error('unable to retrieve stock exchange rate')
|
logging.error('unable to retrieve stock exchange rate')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self):
|
||||||
self._value = self.fetch()
|
self.__value = self.fetch()
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue