I have a stock ticker and I want it to display static text when market is closed. Im not sure how to do this in Python.
I tried this one Python - Working out if time now is between two times but it did not work for my example.
from samplebase import SampleBase
from rgbmatrix import graphics
import time
import requests
from bs4 import BeautifulSoup as bs
from datetime import datetime
class RunText(SampleBase):
def run(self):
while True:
offscreen_canvas = self.matrix.CreateFrameCanvas()
pos = offscreen_canvas.width
while True:
now = time.strftime("%H:%M:%S", time.localtime(time.time()))
print("Script Start: ") + str(now)
res = requests.get('https://thecache.io/stocks/index.php')
soup = bs(res.content, 'lxml')
price = soup.select_one('#GME').text
price1 = soup.select_one('#etsy').text
price2 = soup.select_one('#aapl').text
font = graphics.Font()
font.LoadFont("../../../fonts/5x7.bdf")
textColor = graphics.Color(255, 255, 0)
stock2 = graphics.Color(139, 0, 0)
stock3 = graphics.Color(64, 0, 255)
my_text = " GME: " + price
my_text1 = "ETSY: " + price1
my_text2 = "AAPL: " + price2
offscreen_canvas.Clear()
len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text)
len2 = graphics.DrawText(offscreen_canvas, font, pos, 18, stock2, my_text1)
len3 = graphics.DrawText(offscreen_canvas, font, pos, 26, stock3, my_text2)
pos = 1
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
#Main function
if __name__ == "__main__":
run_text = RunText()
# time.sleep(0.08)
if (not run_text.process()):
run_text.print_help()
Basically i dont want to hit the API when stock is closed. Thanks for your help.