bumblebee-status/bumblebee_status/modules/contrib/stock.py

78 lines
2.4 KiB
Python
Raw Normal View History

2020-04-19 09:44:25 +02:00
# -*- coding: UTF-8 -*-
# pylint: disable=C0111,R0903
"""Display a stock quote from finance.yahoo.com
2020-04-19 09:44:25 +02:00
Parameters:
* stock.symbols : Comma-separated list of symbols to fetch
* stock.apikey : API key created on https://alphavantage.co
* stock.url : URL to use, defaults to "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={apikey}"
* stock.fields : Fields from the response to show, defaults to "01. symbol,05. price,10. change percent"
contributed by `msoulier <https://github.com/msoulier>`_ - many thanks!
2020-04-19 09:44:25 +02:00
"""
import json
2020-04-19 09:49:06 +02:00
import urllib.request
2020-04-19 09:44:25 +02:00
import logging
2020-04-19 09:49:06 +02:00
import core.module
import core.widget
import core.decorators
import util.format
def flatten(d, result):
for k, v in d.items():
if type(v) is dict:
flatten(v, result)
else:
result[k] = v
2020-04-19 09:49:06 +02:00
class Module(core.module.Module):
@core.decorators.every(hours=1)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.value))
2020-04-19 09:49:06 +02:00
self.__symbols = self.parameter("symbols", "")
self.__apikey = self.parameter("apikey", None)
self.__fields = self.parameter("fields", "01. symbol,05. price,10. change percent").split(",")
self.__url = self.parameter("url", "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={apikey}")
self.__change = util.format.asbool(self.parameter("change", True))
self.__values = []
2020-04-19 09:44:25 +02:00
def value(self, widget):
result = ""
for value in self.__values:
res = {}
flatten(value, res)
for field in self.__fields:
result += res.get(field, "n/a") + " "
result = result[:-1]
return result
2020-04-19 09:44:25 +02:00
def fetch(self):
results = []
2020-04-19 09:49:06 +02:00
if self.__symbols:
for symbol in self.__symbols.split(","):
url = self.__url.format(symbol=symbol, apikey=self.__apikey)
try:
results.append(json.loads(urllib.request.urlopen(url).read().strip()))
except urllib.request.URLError:
logging.error("unable to open stock exchange url")
return []
2020-04-19 09:44:25 +02:00
else:
logging.error("unable to retrieve stock exchange rate")
return []
return results
2020-04-19 09:44:25 +02:00
2020-04-19 09:49:06 +02:00
def update(self):
self.__values = self.fetch()
2020-04-19 09:44:25 +02:00
2020-04-19 09:44:25 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4