From 621474ca43314065e8ac9d404da0d28e079236b8 Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 18 Jul 2019 17:17:55 -0700 Subject: [PATCH] code now grabs stocks and make matrix --- stock.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/stock.py b/stock.py index 02ae15f..0e80256 100644 --- a/stock.py +++ b/stock.py @@ -10,7 +10,7 @@ from keys import IEX_TOKEN from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics -font_file = "matrix\\fonts\\9x18.bdf" +font_file = "matrix\\fonts\\9x15.bdf" font = Font(font_file) matrix_shape = (16, 32) # rows, cols stocks = ["tsla", "psec", "aapl"] @@ -26,6 +26,17 @@ options.rows, options.cols = matrix_shape red = (255, 51, 51) green = (51, 255, 51) white = (255, 255, 255) +blank = np.zeros((matrix_shape[0], matrix_shape[1], 3)) + + +def stockMatrix(symbols: list, matrix=blank): + array = blank + for symbol in symbols: + message, color = symbolData(symbol) + mess_arr = matrix_message(message, color, matrix_shape[0]) + + array = np.concatenate((array, mess_arr), axis=1) + return array def symbolData(symbol: str): @@ -39,23 +50,23 @@ def symbolData(symbol: str): response = requests.get(IEXurl) if response.status_code is 200: IEXData = response.json() - message = "The current stock price of {name} is $**{price}**".format( + message = "The current stock price of {name} is {price}".format( name=IEXData["companyName"], price=IEXData["latestPrice"] ) # Determine wording of change text change = round(IEXData["changePercent"] * 100, 2) if change > 0: - message += ", the stock is currently up {change}%".format(change=change) + message += ", the stock is currently up {change}% ".format(change=change) color = green elif change < 0: - message += ", the stock is currently down {change}%".format(change=change) + message += ", the stock is currently down {change}% ".format(change=change) color = red else: message += ", the stock hasn't shown any movement today." color = white else: - message = "The symbol: {symbol} was not found.".format(symbol=symbol) + message = "The symbol: {symbol} was not found. ".format(symbol=symbol) color = white return (message, color) @@ -76,6 +87,7 @@ def run(pos): try: print("Press CTRL-C to stop.") pos = 0 + matrix = stockMatrix(symbols) while True: run(pos) pos += 1 @@ -85,8 +97,8 @@ except KeyboardInterrupt: sys.exit(0) -def color_message(message, color): - message_arr = font.word(message) +def matrix_message(message, color, rows): + message_arr = fit_array(font.word(message), rows) r = np.multiply(np.full(message_arr.shape, color[0]), message_arr) g = np.multiply(np.full(message_arr.shape, color[1]), message_arr) @@ -100,9 +112,9 @@ def color_message(message, color): return array -def fit_array(array, shape, operation="centered", fill_value=0): - rows = np.subtract(shape, array.shape)[0] - cols = shape[1] +def fit_array(array, rows, operation="centered", fill_value=0): + rows = rows - array.shape[0] + cols = array.shape[1] offset = (rows, cols) if operation is "centered": @@ -111,7 +123,6 @@ def fit_array(array, shape, operation="centered", fill_value=0): bottom = rows // 2 bottom_fill = np.full((bottom, cols), fill_value) - return np.concatenate((top_fill, array, bottom_fill), axis=0) elif operation is "top": @@ -124,3 +135,4 @@ def fit_array(array, shape, operation="centered", fill_value=0): else: raise Exception("Invalid Operation. Must be either centered, top, or bottom.") +