From 2f0ec542cd5fbc72791369cc6556cbd1b7b7281e Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 10 Jul 2019 22:40:10 -0700 Subject: [PATCH] first functional version of the stock ticker --- README.md | 15 +++++++++++ stock.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 stock.py diff --git a/README.md b/README.md index 06b4c4a..519312d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # Raspberry Pi Stock Ticker +## Setup Instructions + +Install dietpi normally + +```bash + git clone https://github.com/hzeller/rpi-rgb-led-matrix.git + cd ./rpi-rgb-led-matrix/ + sudo apt-get update && sudo apt-get install python3-dev git pip build-essential -y + make build-python PYTHON=python3 + sudo make install-python PYTHON=python3 + cd ./bindings/python/samples/ + wget https://gitlab.com/snippets/1874042/raw -O stock.py + pip3 install requests==2.22.0 + sudo python3 ./stock.py +``` diff --git a/stock.py b/stock.py new file mode 100644 index 0000000..97f95aa --- /dev/null +++ b/stock.py @@ -0,0 +1,74 @@ +# Display a runtext with double-buffering. +from rgbmatrix import graphics, RGBMatrix, RGBMatrixOptions +import time +import sys +import requests + +options = RGBMatrixOptions() +options.rows = 16 +options.cols = 32 +options.chain_length = 1 +options.parallel = 1 +options.hardware_mapping = "regular" + + +def symbolData(symbol: str): + """ + Takes a list of symbol and returns a dictionary of strings with information about the symbol. + """ + IEX_TOKEN = "TOKEN HERE" + IEXurl = "https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX}".format( + symbol=symbol, IEX=IEX_TOKEN + ) + + response = requests.get(IEXurl) + if response.status_code is 200: + IEXData = response.json() + 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) + elif change < 0: + message += ", the stock is currently **down {change}%**".format( + change=change + ) + else: + message += ", the stock hasn't shown any movement today." + else: + message = "The symbol: {symbol} was not found.".format(symbol=symbol) + + return message + + +def run(): + matrix = RGBMatrix(options=options) + offscreen_canvas = matrix.CreateFrameCanvas() + font = graphics.Font() + font.LoadFont("../../../fonts/7x13.bdf") + textColor = graphics.Color(255, 255, 0) + pos = offscreen_canvas.width + my_text = "loading" + + while True: + offscreen_canvas.Clear() + len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text) + pos -= 1 + if pos + len < 0: + my_text = symbolData("tsla") + pos = offscreen_canvas.width + + time.sleep(0.05) + offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas) + + +try: + print("Press CTRL-C to stop.") + while True: + run() +except KeyboardInterrupt: + sys.exit(0) +