market opening time check added

This commit is contained in:
Neythen 2021-05-05 20:26:56 +01:00
parent fa91bc4a9b
commit aaeb9e4de2
5 changed files with 78 additions and 61 deletions

View File

@ -1,41 +1,55 @@
import finnhub import finnhub
import time import time
import csv import csv
import pytz
from datetime import datetime
APIkey = "c24qddqad3ickpckgg80" APIkey = "c24qddqad3ickpckgg80"
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
finnhubClient = finnhub.Client(api_key=APIkey) finnhubClient = finnhub.Client(api_key=APIkey)
NY_zone = pytz.timezone('America/New_York')
NY_time = datetime.now(NY_zone)
opening = NY_time.replace(hour=9, minute=30, second=0, microsecond=0)
closing = NY_time.replace(hour=16, minute=0, second=0, microsecond=0)
def getStockPrices(): def getStockPrices():
apiCalledError = False
stock_info = []
symbols = [] if opening < NY_time < closing:
f = open('csv/tickers.csv', 'r') apiCalledError = False
CSV = csv.reader(f) stock_info = []
for row in CSV:
symbol = row[0]
symbols.append(symbol)
f.close()
try:
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
current_prices = [quote['c'] for quote in quotes]
opening_prices = [quote['o'] for quote in quotes]
CSV = open('csv/tickers.csv', 'w+') symbols = []
for i, symbol in enumerate(symbols): f = open('csv/tickers.csv', 'r')
symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n' CSV = csv.reader(f)
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close() for row in CSV:
print('API called successfully') symbol = row[0]
symbols.append(symbol)
f.close()
try:
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
current_prices = [quote['c'] for quote in quotes]
opening_prices = [quote['o'] for quote in quotes]
CSV = open('csv/tickers.csv', 'w+')
except Exception as e: for i, symbol in enumerate(symbols):
print("Could not fetch data - API CALLS REACHED? - Will display old image") symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n'
print(e) CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
apiCalledError = True CSV.close()
return stock_info, apiCalledError print('API called successfully')
except Exception as e:
print("Could not fetch data - API CALLS REACHED? - Will display old image")
print(e)
apiCalledError = True
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,20 +1,20 @@
MSFT,248.15,249.06 MSFT,246.695,249.06
NFLX,502.34,504.99 NFLX,499.625,504.99
GOOG,2366.54,2368.42 GOOG,2362.47,2368.42
TSLA,676.818,681.06 TSLA,670.29,681.06
AAPL,129.675,129.2 AAPL,128.6,129.2
INTC,56.94,56.96 INTC,56.775,56.96
TXN,182.07,181.01 TXN,181.845,181.01
HPQ,34.315,34.24 HPQ,34.565,34.24
HOG,49.62,48.33 HOG,49.65,48.33
LUV,61.46,60.77 LUV,61.36,60.77
WMT,140.6,140.71 WMT,140.85,140.71
BJ,45.18,45.17 BJ,45.465,45.17
ETSY,187.8,190.99 ETSY,182.205,190.99
G,47.02,47.25 G,47.15,47.25
GDDY,82.985,83.24 GDDY,82.125,83.24
GNRC,320.425,318.9 GNRC,319.07,318.9
PEP,143.65,143.715 PEP,144.35,143.715
STMYELP,0,0 STMYELP,0,0
XRAY,67.68,68.92 XRAY,67.73,68.92
ZTS,172.4995,174.2 ZTS,173,174.2

1 MSFT 248.15 246.695 249.06
2 NFLX 502.34 499.625 504.99
3 GOOG 2366.54 2362.47 2368.42
4 TSLA 676.818 670.29 681.06
5 AAPL 129.675 128.6 129.2
6 INTC 56.94 56.775 56.96
7 TXN 182.07 181.845 181.01
8 HPQ 34.315 34.565 34.24
9 HOG 49.62 49.65 48.33
10 LUV 61.46 61.36 60.77
11 WMT 140.6 140.85 140.71
12 BJ 45.18 45.465 45.17
13 ETSY 187.8 182.205 190.99
14 G 47.02 47.15 47.25
15 GDDY 82.985 82.125 83.24
16 GNRC 320.425 319.07 318.9
17 PEP 143.65 144.35 143.715
18 STMYELP 0 0 0
19 XRAY 67.68 67.73 68.92
20 ZTS 172.4995 173 174.2

BIN
final.ppm

Binary file not shown.

View File

@ -152,16 +152,7 @@ class StockTicker():
elif current_img == 2: elif current_img == 2:
current_img = 1 current_img = 1
offset_x = 0 offset_x = 0
#Get the logo from file that is the same as ticker name
def getLogo(self, Ticker):
logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
try:
Logo = Image.open(os.path.join(logos_path, Ticker + '.png'))
except:
print('Could not find logo for: ' + Ticker + ' using default')
Logo = Image.open(os.path.join(logos_path, 'default.png'))
return Logo
#Using change between min and day price give appropriate arrow #Using change between min and day price give appropriate arrow
#and set the overall change colour #and set the overall change colour
@ -250,12 +241,24 @@ class StockTicker():
ticker = symbol #TEXT ticker = symbol #TEXT
current = '%.2f' % float(info[0]) #TEXT current = '%.2f' % float(info[0]) #TEXT
logo = self.getLogo(symbol)
arrow, change = self.getArrow(change) arrow, change = self.getArrow(change)
change = '%.2f' % change change = '%.2f' % change
midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT
stitchedStock = self.stitchImage([logo,midFrame])
try:
logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
print(os.path.join(logos_path, ticker + '.png'))
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
stitchedStock = self.stitchImage([logo,midFrame])
except:
print('no logo for ' + ticker)
stitchedStock = midFrame
image_list.append(self.blank) image_list.append(self.blank)
image_list.append(stitchedStock) image_list.append(stitchedStock)
@ -361,13 +364,13 @@ if __name__ == '__main__':
stock_ticker.process_msg(brightness) stock_ticker.process_msg(brightness)
stock_ticker.process_msg(speed) stock_ticker.process_msg(speed)
print(stock_ticker.delay, stock_ticker.brightness)
#t = time.time() #t = time.time()
#api_caller = pexpect.spawn("sudo -E python3 api_caller.py") #api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
#print('time to call api', time.time()-t) #print('time to call api', time.time()-t)
#stock_ticker.getFullStockImage() stock_ticker.getFullStockImage()
#stock_ticker.displayMatrix() stock_ticker.displayMatrix()
while True: while True: