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
from datetime import datetime
APIkey = "c24qddqad3ickpckgg80"
sandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g"
finnhubClient = finnhub.Client(api_key=APIkey)
import datetime as dt
NY_zone = pytz.timezone('America/New_York')
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():
NY_time = datetime.now(NY_zone)
def readCSV():
if opening < NY_time < closing:
apiCalledError = False
stock_info = []
symbols = []
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
for row in CSV:
symbols = []
stock_info = {}
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
print(row)
try:
symbol, current_price, opening_price = row
symbols.append(symbol)
stock_info[symbol] = [current_price, opening_price]
except:
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]
stock_info[symbol] = []
CSV = open('csv/tickers.csv', 'w+')
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()
print('API called successfully')
f.close()
return symbols, stock_info
def emptyInfo(symbols, stock_info):
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()
print('API called successfully')
except Exception as e:
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__':
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
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)

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
NFLX,499.625,504.99
GOOG,2362.47,2368.42
TSLA,670.29,681.06
AAPL,128.6,129.2
INTC,56.775,56.96
TXN,181.845,181.01
HPQ,34.565,34.24
HOG,49.65,48.33
LUV,61.36,60.77
WMT,140.85,140.71
BJ,45.465,45.17
ETSY,182.205,190.99
G,47.15,47.25
GDDY,82.125,83.24
GNRC,319.07,318.9
PEP,144.35,143.715
name,current,opening
MSFT,249.35,246.45
NFLX,498.645,495.99
GOOG,2376.26,2350.64
TSLA,663.1871,680.76
AAPL,129.68,127.89
INTC,57.095,56.615
TXN,184.04,181.93
HPQ,34.975,34.42
HOG,48.895,49.36
LUV,60.65,60.97
WMT,140.925,140
BJ,46.68,45.74
ETSY,157.86,167.01
G,47.115,47.17
GDDY,81.28,80.11
GNRC,317.81,315.29
PEP,145.47,144.47
STMYELP,0,0
XRAY,67.73,68.92
ZTS,173,174.2
XRAY,67.36,66.6
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())
#(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.sendline('S') # run by default
#stock_ticker = StockTicker()
@ -101,11 +101,13 @@ def Speed():
f = open('csv/settings.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for line in CSV:
_, brightness = line
f.close()
f = open('csv/settings.csv', 'w+')
f.write('speed,brightness\n')
f.write(str(speed) + ',' + brightness)
f.close()
global LastCommand
@ -121,11 +123,13 @@ def Brightness():
f = open('csv/settings.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for line in CSV:
speed, _ = line
f.close()
f = open('csv/settings.csv', 'w+')
f.write('speed,brightness\n')
f.write(str(speed) + ',' + str(brightness))
f.close()

View File

@ -285,7 +285,7 @@ class StockTicker():
self.stock_info = {}
f = open('csv/tickers.csv', 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
print(row)
try:
@ -377,8 +377,8 @@ if __name__ == '__main__':
#t = time.time()
#api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
#print('time to call api', time.time()-t)
stock_ticker.getFullStockImage(1)
stock_ticker.displayMatrix()
#stock_ticker.getFullStockImage(1)
#stock_ticker.displayMatrix()
while True:

Binary file not shown.