api call conditions improved
This commit is contained in:
parent
18841c817f
commit
3d6fb3d722
Binary file not shown.
154
api_caller.py
154
api_caller.py
@ -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)
|
||||
|
@ -1 +1,2 @@
|
||||
m,4
|
||||
speed,brightness
|
||||
f,7
|
||||
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
|
||||
|
@ -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:
|
||||
|
BIN
stockTicker.pyc
BIN
stockTicker.pyc
Binary file not shown.
Loading…
Reference in New Issue
Block a user