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:
parent
faf029b9d9
commit
621474ca43
28
stock.py
28
stock.py
@ -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,7 +50,7 @@ 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"]
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -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.")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user