api call conditions improved

This commit is contained in:
Neythen
2021-05-06 20:59:27 +01:00
parent 18841c817f
commit 3d6fb3d722
8 changed files with 139 additions and 69 deletions

View File

@@ -4,58 +4,122 @@ import csv
import pytz import pytz
from datetime import datetime from datetime import datetime
APIkey = "c24qddqad3ickpckgg80" import datetime as dt
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
finnhubClient = finnhub.Client(api_key=APIkey)
NY_zone = pytz.timezone('America/New_York') def readCSV():
symbols = []
opening = NY_time.replace(hour=9, minute=30, second=0, microsecond=0) stock_info = {}
closing = NY_time.replace(hour=16, minute=0, second=0, microsecond=0) f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
print(row)
try:
def getStockPrices(): symbol, current_price, opening_price = row
NY_time = datetime.now(NY_zone) symbols.append(symbol)
stock_info[symbol] = [current_price, opening_price]
except:
if opening < NY_time < closing:
apiCalledError = False
stock_info = []
symbols = []
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
for row in CSV:
symbol = row[0] symbol = row[0]
symbols.append(symbol) symbols.append(symbol)
f.close() stock_info[symbol] = []
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+') f.close()
for i, symbol in enumerate(symbols):
symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n' return symbols, stock_info
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close() def emptyInfo(symbols, stock_info):
print('API called successfully') update = False
for symbol in symbols:
if len(stock_info[symbol]) == 1: # stock with no info
update = True
return update
def updateUpdate(NY_time):
NY_str = NY_time.strftime("%d/%m/%Y %H:%M:%S")
f = open('csv/last_update.csv', 'w+')
f.write(NY_str + '\n')
f.close()
def updateStockPrices(symbols):
apiCalledError = False
stock_info = []
symbols = []
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
next(CSV) #read through headers
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+')
CSV.write('name,current,opening\n')
for i, symbol in enumerate(symbols):
symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n'
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close()
except Exception as e: print('API called successfully')
print("Could not fetch data - API CALLS REACHED? - Will display old image")
print(e)
apiCalledError = True
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__':
APIkey = "c24qddqad3ickpckgg80"
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
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)
NY_time = datetime.now(NY_zone)
sleeptime = 2 #minutes sleeptime = 2 #minutes
while True: while True:
getStockPrices() NY_time = datetime.now(NY_zone)
symbols, stock_info = readCSV()
print(type(NY_time))
if opening < NY_time < closing: # we need to do real time updating
updateStockPrices(symbols)
updateUpdate(NY_time)
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
updateStockPrices(symbols)
updateUpdate(NY_time)
else:
# update if last update was before the previous days closing
f = open('csv/last_update.csv', 'r')
CSV = csv.reader(f)
last_update_str = next(CSV)[0]
print(last_update_str)
last_update = datetime.strptime(last_update_str, "%d/%m/%Y %H:%M:%S")
yday_closing = closing - dt.timedelta(days=1)
yday_str = yday_closing.strftime("%d/%m/%Y %H:%M:%S")
yday_closing = datetime.strptime(yday_str, "%d/%m/%Y %H:%M:%S")
print(last_update < yday_closing)
if last_update < yday_closing:
updateStockPrices(symbols)
updateUpdate(NY_time)
time.sleep(sleeptime*60) time.sleep(sleeptime*60)

View File

@@ -1 +1,2 @@
m,4 speed,brightness
f,7
1 m speed 4 brightness
2 f 7

View File

@@ -1,20 +1,21 @@
MSFT,246.695,249.06 name,current,opening
NFLX,499.625,504.99 MSFT,249.35,246.45
GOOG,2362.47,2368.42 NFLX,498.645,495.99
TSLA,670.29,681.06 GOOG,2376.26,2350.64
AAPL,128.6,129.2 TSLA,663.1871,680.76
INTC,56.775,56.96 AAPL,129.68,127.89
TXN,181.845,181.01 INTC,57.095,56.615
HPQ,34.565,34.24 TXN,184.04,181.93
HOG,49.65,48.33 HPQ,34.975,34.42
LUV,61.36,60.77 HOG,48.895,49.36
WMT,140.85,140.71 LUV,60.65,60.97
BJ,45.465,45.17 WMT,140.925,140
ETSY,182.205,190.99 BJ,46.68,45.74
G,47.15,47.25 ETSY,157.86,167.01
GDDY,82.125,83.24 G,47.115,47.17
GNRC,319.07,318.9 GDDY,81.28,80.11
PEP,144.35,143.715 GNRC,317.81,315.29
PEP,145.47,144.47
STMYELP,0,0 STMYELP,0,0
XRAY,67.73,68.92 XRAY,67.36,66.6
ZTS,173,174.2 ZTS,167.11,167.25
1 MSFT name 246.695 current 249.06 opening
2 NFLX MSFT 499.625 249.35 504.99 246.45
3 GOOG NFLX 2362.47 498.645 2368.42 495.99
4 TSLA GOOG 670.29 2376.26 681.06 2350.64
5 AAPL TSLA 128.6 663.1871 129.2 680.76
6 INTC AAPL 56.775 129.68 56.96 127.89
7 TXN INTC 181.845 57.095 181.01 56.615
8 HPQ TXN 34.565 184.04 34.24 181.93
9 HOG HPQ 49.65 34.975 48.33 34.42
10 LUV HOG 61.36 48.895 60.77 49.36
11 WMT LUV 140.85 60.65 140.71 60.97
12 BJ WMT 45.465 140.925 45.17 140
13 ETSY BJ 182.205 46.68 190.99 45.74
14 G ETSY 47.15 157.86 47.25 167.01
15 GDDY G 82.125 47.115 83.24 47.17
16 GNRC GDDY 319.07 81.28 318.9 80.11
17 PEP GNRC 144.35 317.81 143.715 315.29
18 PEP 145.47 144.47
19 STMYELP STMYELP 0 0 0 0
20 XRAY XRAY 67.73 67.36 68.92 66.6
21 ZTS ZTS 173 167.11 174.2 167.25

BIN
final.ppm

Binary file not shown.

View File

@@ -34,7 +34,7 @@ ALLOWED_EXTENSIONS = {'csv', 'png'}
#print(ticker.stdout.readlines()) #print(ticker.stdout.readlines())
#(output, errs) #(output, errs)
time.sleep(10) # let api calls finish #time.sleep(10) # let api calls finish
ticker = pexpect.spawn("sudo -E python3 stockTicker.py") ticker = pexpect.spawn("sudo -E python3 stockTicker.py")
ticker.sendline('S') # run by default ticker.sendline('S') # run by default
#stock_ticker = StockTicker() #stock_ticker = StockTicker()
@@ -101,11 +101,13 @@ def Speed():
f = open('csv/settings.csv', 'r') f = open('csv/settings.csv', 'r')
CSV = csv.reader(f) CSV = csv.reader(f)
next(CSV)
for line in CSV: for line in CSV:
_, brightness = line _, brightness = line
f.close() f.close()
f = open('csv/settings.csv', 'w+') f = open('csv/settings.csv', 'w+')
f.write('speed,brightness\n')
f.write(str(speed) + ',' + brightness) f.write(str(speed) + ',' + brightness)
f.close() f.close()
global LastCommand global LastCommand
@@ -121,11 +123,13 @@ def Brightness():
f = open('csv/settings.csv', 'r') f = open('csv/settings.csv', 'r')
CSV = csv.reader(f) CSV = csv.reader(f)
next(CSV)
for line in CSV: for line in CSV:
speed, _ = line speed, _ = line
f.close() f.close()
f = open('csv/settings.csv', 'w+') f = open('csv/settings.csv', 'w+')
f.write('speed,brightness\n')
f.write(str(speed) + ',' + str(brightness)) f.write(str(speed) + ',' + str(brightness))
f.close() f.close()

View File

@@ -285,7 +285,7 @@ class StockTicker():
self.stock_info = {} self.stock_info = {}
f = open('csv/tickers.csv', 'r') f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f) CSV = csv.reader(f)
next(CSV)
for row in CSV: for row in CSV:
print(row) print(row)
try: try:
@@ -377,8 +377,8 @@ if __name__ == '__main__':
#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(1) #stock_ticker.getFullStockImage(1)
stock_ticker.displayMatrix() #stock_ticker.displayMatrix()
while True: while True:

Binary file not shown.