From 5daed9eb6e4b39b04ac10d1cea2db3c106019acc Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 13 Jun 2022 01:39:07 +0800 Subject: [PATCH] added commodities --- stockTicker.py | 192 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 188 insertions(+), 4 deletions(-) diff --git a/stockTicker.py b/stockTicker.py index 82d1207..1c66d35 100755 --- a/stockTicker.py +++ b/stockTicker.py @@ -68,19 +68,19 @@ class StockTicker(): 'Daily Forecast':self.getDailyWeatherImage, 'Current Weather': self.getTodayWeatherImage, 'Sports (Team Stats)':lambda : self.getLeagueTableImage('premier_league'), 'Sports (Past Games)': lambda:self.getLeagueImage('NBA', 'past'), 'Sports (Upcoming Games)': lambda : self.getLeagueImage('NHL', 'future'), 'Sports (Live Games)': lambda: self.getLeagueImage('NBA', 'live'), - 'News':self.getNewsImage, 'Custom Messages': self.getUserMessages, + 'News':self.getNewsImage, 'Custom Messages': self.getUserMessages, 'Commodities': self.getCommoditiesImage, 'Stocks Prof': self.getStockProfessional, 'Crypto Prof': self.getCryptoProfessional, 'Forex Prof': self.getForexProfessional, - 'Current Weather Prof': self.getTodayWeatherProfessional, 'News Prof':self.getNewsProfessional} + 'Current Weather Prof': self.getTodayWeatherProfessional, 'News Prof':self.getNewsProfessional, 'Commodities Prof':self.getCommoditiesProfessional} self.JSONs = {'Stocks': 'csv/stocks_settings.json', 'Crypto': 'csv/crypto_settings.json', 'Forex': 'csv/forex_settings.json', - 'Daily Forecast':'csv/daily_weather.json', 'Current Weather': 'csv/current_weather.json', + 'Daily Forecast':'csv/daily_weather.json', 'Current Weather': 'csv/current_weather.json', 'Commodities':'csv/commodities_settings.json', 'Sports (Team Stats)': 'csv/league_tables.json', 'Sports (Past Games)': 'csv/past_games.json', 'Sports (Upcoming Games)': 'csv/upcoming_games.json', 'Sports (Live Games)': 'csv/live_games.json', 'News':'csv/news_settings.json', 'Custom Images': 'csv/image_settings.json', 'Custom GIFs': 'csv/GIF_settings.json', 'Custom Messages': 'csv/message_settings.json', 'Stocks Prof': 'csv/stocks_settings.json', 'Crypto Prof': 'csv/crypto_settings.json', 'Forex Prof': 'csv/forex_settings.json', - 'Current Weather Prof': 'csv/current_weather.json', 'News Prof':'csv/news_settings.json'} + 'Current Weather Prof': 'csv/current_weather.json', 'News Prof':'csv/news_settings.json', 'Commodities Prof':'csv/commodities_settings.json'} def openImage(self, image_file): @@ -1064,6 +1064,184 @@ class StockTicker(): finalDisplayImage = self.stitchImage(image_list) self.blank = Image.new('RGB', (10, 32)) return finalDisplayImage + + def getCommoditiesImage(self): + + f = open('csv/commodities_settings.json', 'r') + all_commodities_settings = json.load(f) + f.close() + + if all_commodities_settings['title']: + title_img = self.openImage('feature_titles/commodities.png') + image_list = [title_img] + image_list.append(self.blank) + else: + image_list = [] + + + commodity_info = all_commodities_settings['symbols'] + symbols = list(commodity_info.keys()) + + for i, symbol in enumerate(symbols): + + try: + info = commodity_info[symbol] + + change = float(info['24hr_change']) #TEXT + ticker = symbol #TEXT + unit = info['unit'] + ticker = symbol + '(' + unit + ')' #TEXT + arrow, change = self.getArrow(change) + + percent_change = '%.2f' % abs(float(info['percent_change'])) + '%' + + point_change2 = abs(change) + point_changefinal = '{0:.10f}'.format(point_change2).rstrip("0") + point_change = str(point_changefinal) + + + current = float(info["current"]) + current_final = '{0:.10f}'.format(current).rstrip("0") + current = str(current_final) + + + + if not all_commodities_settings['percent']: + percent_change = False + if not all_commodities_settings['point']: + point_change = False + + + + + midFrame = self.textToImage(ticker, current, arrow, percent_change, point_change) #IMAGE THE TEXT + + if all_commodities_settings['logos']: + try: + logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'commodities') + + logo = self.openImage(os.path.join(logos_path, ticker + '.png')) + stitchedCommodity = self.stitchImage([logo,midFrame]) + except Exception as e: + + stitchedCommodity = midFrame + else: + stitchedCommodity = midFrame + + image_list.append(stitchedCommodity) + + + image_list.append(self.blank) + except Exception as e: + + logf = open('log.txt', "a") + exc_type, exc_obj, exc_tb = sys.exc_info() + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] + logf.write(str(e)) + logf.write('. file: ' + fname) + logf.write('. line: ' + str(exc_tb.tb_lineno)) + logf.write('. type: ' + str(exc_type)) + logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) + logf.close() + + + + finalDisplayImage = self.stitchImage(image_list) + + + + return finalDisplayImage + + + def getCommoditiesProfessional(self): + self.blank = Image.new('RGB', (0, 16)) + + f = open('csv/commodities_settings.json', 'r') + all_commodities_settings = json.load(f) + f.close() + + if all_commodities_settings['title']: + title_img = self.openImage('feature_titles/small_feature_titles/commodities.png') + image_list = [title_img, Image.new('RGB', (5, 16))] + image_list.append(self.blank) + else: + image_list = [] + + + commodity_info = all_commodities_settings['symbols'] + symbols = list(commodity_info.keys()) + + for i, symbol in enumerate(symbols): + + try: + info = commodity_info[symbol] + + change = float(info['24hr_change']) #TEXT + unit = info['unit'] + ticker = symbol + '(' + unit + ')' #TEXT + + + + arrow, change = self.getArrow(change, professional=True) + + if all_commodities_settings["percent"]: + change = '%.2f' % abs(float(info['percent_change'])) + '%' + else: + point_change2 = abs(change) + point_changefinal = '{0:.10f}'.format(point_change2).rstrip("0") + change = str(point_changefinal) + + + current = float(info["current"]) + current_final = '{0:.10f}'.format(current).rstrip("0") + current = str(current_final) + + midFrame = self.textToImageProf(ticker, current, change, arrow, font=ImageFont.load("./fonts/6x10.pil")) #IMAGE THE TEXT + + if all_commodities_settings['logos']: + try: + try: #load the tiny logo + logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'tiny_commodities') + + logo = Image.open(os.path.join(logos_path, ticker + '.png')) + + except: # load the big logo and scale it + + logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'commodities') + + logo = Image.open(os.path.join(logos_path, ticker + '.png')) + # half the size of the logo + width, height = logo.size + + logo = logo.resize((int(width/2), int(height/2))) + stitchedCommodity = self.stitchImage([logo,midFrame]) + except Exception as e: + + stitchedCommodity = midFrame + else: + stitchedCommodity = midFrame + + image_list.append(stitchedCommodity) + + + image_list.append(self.blank) + + except Exception as e: + logf = open('log.txt', "a") + exc_type, exc_obj, exc_tb = sys.exc_info() + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] + logf.write(str(e)) + logf.write('. file: ' + fname) + logf.write('. line: ' + str(exc_tb.tb_lineno)) + logf.write('. type: ' + str(exc_type)) + logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) + logf.close() + + finalDisplayImage = self.stitchImage(image_list) + + + self.blank = Image.new('RGB', (10, 32)) + return finalDisplayImage def getForexImage(self): @@ -2683,6 +2861,7 @@ class StockTicker(): news = self.getNewsProfessional() stock = self.getStockProfessional() weather = self.getTodayWeatherProfessional() + commodities = self.getCommoditiesProfessional() x_offset = 0 news.paste(weather, (x_offset, 16)) @@ -2693,6 +2872,8 @@ class StockTicker(): x_offset += stock.size[0] news.paste(forex, (x_offset, 16)) x_offset += forex.size[0] + news.paste(commodities, (x_offset, 16)) + x_offset += commodities.size[0] self.double_buffer = self.matrix.CreateFrameCanvas() while True: kill = stock_ticker.scrollImage(news, offset_x = 128) @@ -2764,6 +2945,9 @@ class StockTicker(): elif msg == 't': #legue tble self.scrollFunctionsAnimated(['league_table', 'league_table'],animation = 'traditional') + elif msg == 'CO': #commodities + self.scrollFunctionsAnimated(['commodities', 'commodities'],animation = 'traditional') + elif msg == 'A': #everything #userSettings = ['display_gif', 'text', 'display_image', 'stocks', 'crypto', 'forex', 'today_weather', 'daily_weather', 'league_table', 'league_games', 'news'] # these wil be read from csv, just for demo