1
0
mirror of https://gitlab.com/triple-hops-brewed/raspberry-pi-stock-ticker.git synced 2025-06-16 15:17:21 +00:00

code now grabs stocks and make matrix

This commit is contained in:
Anson 2019-07-18 17:17:55 -07:00
parent faf029b9d9
commit 621474ca43

View File

@ -10,7 +10,7 @@ from keys import IEX_TOKEN
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
font_file = "matrix\\fonts\\9x18.bdf" font_file = "matrix\\fonts\\9x15.bdf"
font = Font(font_file) font = Font(font_file)
matrix_shape = (16, 32) # rows, cols matrix_shape = (16, 32) # rows, cols
stocks = ["tsla", "psec", "aapl"] stocks = ["tsla", "psec", "aapl"]
@ -26,6 +26,17 @@ options.rows, options.cols = matrix_shape
red = (255, 51, 51) red = (255, 51, 51)
green = (51, 255, 51) green = (51, 255, 51)
white = (255, 255, 255) 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): def symbolData(symbol: str):
@ -39,23 +50,23 @@ def symbolData(symbol: str):
response = requests.get(IEXurl) response = requests.get(IEXurl)
if response.status_code is 200: if response.status_code is 200:
IEXData = response.json() 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"] name=IEXData["companyName"], price=IEXData["latestPrice"]
) )
# Determine wording of change text # Determine wording of change text
change = round(IEXData["changePercent"] * 100, 2) change = round(IEXData["changePercent"] * 100, 2)
if change > 0: 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 color = green
elif change < 0: 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 color = red
else: else:
message += ", the stock hasn't shown any movement today." message += ", the stock hasn't shown any movement today."
color = white color = white
else: else:
message = "The symbol: {symbol} was not found.".format(symbol=symbol) message = "The symbol: {symbol} was not found. ".format(symbol=symbol)
color = white color = white
return (message, color) return (message, color)
@ -76,6 +87,7 @@ def run(pos):
try: try:
print("Press CTRL-C to stop.") print("Press CTRL-C to stop.")
pos = 0 pos = 0
matrix = stockMatrix(symbols)
while True: while True:
run(pos) run(pos)
pos += 1 pos += 1
@ -85,8 +97,8 @@ except KeyboardInterrupt:
sys.exit(0) sys.exit(0)
def color_message(message, color): def matrix_message(message, color, rows):
message_arr = font.word(message) message_arr = fit_array(font.word(message), rows)
r = np.multiply(np.full(message_arr.shape, color[0]), message_arr) r = np.multiply(np.full(message_arr.shape, color[0]), message_arr)
g = np.multiply(np.full(message_arr.shape, color[1]), 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 return array
def fit_array(array, shape, operation="centered", fill_value=0): def fit_array(array, rows, operation="centered", fill_value=0):
rows = np.subtract(shape, array.shape)[0] rows = rows - array.shape[0]
cols = shape[1] cols = array.shape[1]
offset = (rows, cols) offset = (rows, cols)
if operation is "centered": if operation is "centered":
@ -111,7 +123,6 @@ def fit_array(array, shape, operation="centered", fill_value=0):
bottom = rows // 2 bottom = rows // 2
bottom_fill = np.full((bottom, cols), fill_value) bottom_fill = np.full((bottom, cols), fill_value)
return np.concatenate((top_fill, array, bottom_fill), axis=0) return np.concatenate((top_fill, array, bottom_fill), axis=0)
elif operation is "top": elif operation is "top":
@ -124,3 +135,4 @@ def fit_array(array, shape, operation="centered", fill_value=0):
else: else:
raise Exception("Invalid Operation. Must be either centered, top, or bottom.") raise Exception("Invalid Operation. Must be either centered, top, or bottom.")