api call limits and transitions
This commit is contained in:
238
api_caller.py
238
api_caller.py
@@ -96,8 +96,13 @@ def updateUpdate(NY_time):
|
||||
f.write(NY_str + '\n')
|
||||
f.close()
|
||||
|
||||
def updateStockPrices(symbols):
|
||||
def updateStockPrices():
|
||||
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
|
||||
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
|
||||
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||
|
||||
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
try:
|
||||
quotes = [finnhubClient.quote(symbol) for symbol in symbols]
|
||||
current_prices = [quote['c'] for quote in quotes]
|
||||
@@ -120,7 +125,11 @@ def updateStockPrices(symbols):
|
||||
apiCalledError = True
|
||||
|
||||
|
||||
def updateStockPricesIEX(symbols):
|
||||
def updateStockPricesIEX():
|
||||
iexAPIkey = 'pk_68ef6a15902c41f887f0b544a0ca17cf' #IEX
|
||||
iexSandboxAPIkey = 'Tpk_0078dff413ef4f979137f7111452dc4b'
|
||||
max_stocks = 200
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
try:
|
||||
symbols_str = ','.join(symbols)
|
||||
method = 'GET'
|
||||
@@ -179,7 +188,10 @@ def updateStockPricesIEX(symbols):
|
||||
print(e)
|
||||
|
||||
|
||||
def updateCrypto(coins, coin_info, unique_bases):
|
||||
def updateCrypto():
|
||||
coingecko_client = CoinGeckoAPI()
|
||||
|
||||
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_crypto)
|
||||
try:
|
||||
response = coingecko_client.get_price(ids=','.join(coins), vs_currencies = unique_bases, include_24hr_change=True)
|
||||
|
||||
@@ -225,7 +237,9 @@ def updateNews():
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def updateWeather(api_key):
|
||||
def updateWeather():
|
||||
|
||||
api_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
|
||||
try:
|
||||
f = open( "csv/weather_location.txt", 'r' )
|
||||
location = f.read()
|
||||
@@ -277,8 +291,8 @@ def updateWeather(api_key):
|
||||
print(e)
|
||||
|
||||
|
||||
def updateCurrencies(api_key):
|
||||
|
||||
def updateForex():
|
||||
api_key = '862dbb6d1101ce0c5136'
|
||||
try:
|
||||
base = 'USD'
|
||||
yesterday = datetime.now() - timedelta(1)
|
||||
@@ -400,106 +414,97 @@ def updateLeagueEvents(api_key, league_id, time):
|
||||
league = 'NFL'
|
||||
|
||||
json.dump(events, open( "csv/sports/{}/{}_games.json".format(league, time), 'w+' ))
|
||||
|
||||
|
||||
def updateSports():
|
||||
#read user settings to decide which sprots to update
|
||||
api_key = '97436974'
|
||||
|
||||
|
||||
|
||||
|
||||
def updateSports(api_key):
|
||||
prem_id = '4328' #prem
|
||||
NHL_id = '4380'
|
||||
NBA_id = '4387' #prem
|
||||
NFL_id = '4391'
|
||||
|
||||
for i in [NHL_id, NBA_id, NFL_id]:
|
||||
for i in [NHL_id]:
|
||||
updateLeagueEvents(api_key, i, 'live')
|
||||
updateLeagueEvents(api_key, i, 'past')
|
||||
updateLeagueEvents(api_key, i, 'future')
|
||||
|
||||
|
||||
updateLeagueTable(api_key, NHL_id)
|
||||
|
||||
updateLeagueTable(api_key, prem_id)
|
||||
|
||||
|
||||
|
||||
|
||||
'https://www.thesportsdb.com/api/v1/json/{}/eventsnext.php?id=133602'.format(api_key) # next five events by team ID (paid) use this for upcoming team games
|
||||
|
||||
|
||||
|
||||
|
||||
#url = 'https://www.thesportsdb.com/api/v1/json/{}/eventsseason.php?id=4328&s=2020-2021'.format(api_key) # all past events in premier league
|
||||
|
||||
|
||||
|
||||
url = 'https://www.thesportsdb.com/api/v2/json/{}/livescore.php?l=4380'.format(api_key) #live scores
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def checkStocks(last_update, update_frequency):
|
||||
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
|
||||
opening = NY_time.replace(hour=9, minute=30, second=0, microsecond=0).replace(tzinfo=None)
|
||||
closing = NY_time.replace(hour=16, minute=0, second=0, microsecond=0).replace(tzinfo=None)
|
||||
|
||||
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
|
||||
updated = False
|
||||
|
||||
diff = (NY_time - last_update).total_seconds()/60 #minutes
|
||||
if opening < NY_time < closing and datetime.today().weekday() < 5: # we need to do real time updating
|
||||
|
||||
|
||||
if diff >= update_frequency:
|
||||
updated = True
|
||||
updateStockPrices()
|
||||
|
||||
|
||||
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
|
||||
updated = True
|
||||
updateStockPrices()
|
||||
|
||||
|
||||
else:
|
||||
# update if last update was before the previous days closing
|
||||
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")
|
||||
|
||||
if last_update < yday_closing:
|
||||
updated = True
|
||||
updateStockPrices(symbols)
|
||||
|
||||
return updated
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
|
||||
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
||||
|
||||
|
||||
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
|
||||
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
|
||||
stock_time = 2 #minutes
|
||||
crypto_time = 10 #minutes
|
||||
news_time = 30 #minutes
|
||||
weather_time = 10 #minutes
|
||||
|
||||
# TODO: different update times for stocks, weather and news
|
||||
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||
max_stocks = 200
|
||||
max_crypto = 100
|
||||
|
||||
iexAPIkey = 'pk_68ef6a15902c41f887f0b544a0ca17cf' #IEX
|
||||
iexSandboxAPIkey = 'Tpk_0078dff413ef4f979137f7111452dc4b'
|
||||
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
|
||||
|
||||
#updateStockPricesIEX(symbols)
|
||||
update_frequencies = {'stocks':2, 'crypto':10, 'news':60, 'weather': 10} #minutes
|
||||
|
||||
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
|
||||
|
||||
coingecko_client = CoinGeckoAPI()
|
||||
|
||||
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)
|
||||
CET_zone = pytz.timezone('Europe/Berlin')
|
||||
|
||||
NY_time = datetime.now(NY_zone)
|
||||
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
updateStockPrices(symbols)
|
||||
updateUpdate(NY_time)
|
||||
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_crypto)
|
||||
CET_time = datetime.now(CET_zone)
|
||||
|
||||
weather_key = 'bd5d5096a5ba30bbcfb57ead42ab3fee'
|
||||
NY_str = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
CET_str = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
|
||||
currency_key = '862dbb6d1101ce0c5136'
|
||||
sports_key = '97436974'
|
||||
#f = open('csv/last_updates.json', 'w+')
|
||||
#update_times = {'stocks':NY_str, 'crypto':NY_str, 'news':NY_str, 'weather': NY_str, 'forex': CET_str} # all in NY time apart from forex in CET
|
||||
#json.dump(update_times, f)
|
||||
#f.close()
|
||||
|
||||
updateSports(sports_key)
|
||||
|
||||
|
||||
|
||||
|
||||
updateCurrencies(currency_key)
|
||||
|
||||
|
||||
updateWeather( weather_key)
|
||||
|
||||
updateCrypto(coins, coin_info, unique_bases)
|
||||
|
||||
updateNews()
|
||||
f = open('csv/last_updates.json', 'r')
|
||||
last_updates = json.load(f)
|
||||
f.close()
|
||||
|
||||
t = time.time()
|
||||
|
||||
@@ -507,56 +512,67 @@ if __name__ == '__main__':
|
||||
|
||||
try:
|
||||
while True:
|
||||
|
||||
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
|
||||
|
||||
msg = getInput()
|
||||
if msg == 'R' or time.time() - t > stock_time*60:
|
||||
|
||||
#stocks
|
||||
stock_time = datetime.strptime(last_updates['stocks'], "%d/%m/%Y %H:%M:%S")
|
||||
stock_frequency = update_frequencies['stocks']
|
||||
if checkStocks(stock_time, stock_frequency):
|
||||
stock_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
last_updates['stocks'] = stock_time
|
||||
|
||||
# crypto
|
||||
crypto_time = datetime.strptime(last_updates['crypto'], "%d/%m/%Y %H:%M:%S")
|
||||
crypto_frequency = update_frequencies['crypto']
|
||||
|
||||
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
|
||||
diff = (NY_time - crypto_time).total_seconds()/60 #minutes
|
||||
if diff >= update_frequencies['crypto']:
|
||||
crypto_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
updateCrypto()
|
||||
last_updates['crypto'] = crypto_time
|
||||
|
||||
|
||||
# weather
|
||||
weather_time = datetime.strptime(last_updates['weather'], "%d/%m/%Y %H:%M:%S")
|
||||
weather_frequency = update_frequencies['weather']
|
||||
|
||||
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
|
||||
diff = (NY_time - weather_time).total_seconds()/60 #minutes
|
||||
if diff >= update_frequencies['weather']:
|
||||
weather_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
updateWeather()
|
||||
last_updates['weather'] = weather_time
|
||||
|
||||
|
||||
|
||||
|
||||
coins, coin_info, unique_bases = readCryptoCSV('csv/crypto.csv', max_stocks)
|
||||
|
||||
updateCrypto(coins, coin_info, unique_bases)
|
||||
updateCurrencies(currency_key)
|
||||
# weather
|
||||
news_time = datetime.strptime(last_updates['news'], "%d/%m/%Y %H:%M:%S")
|
||||
news_frequency = update_frequencies['news']
|
||||
|
||||
NY_time = datetime.now(NY_zone).replace(tzinfo=None)
|
||||
diff = (NY_time - weather_time).total_seconds()/60 #minutes
|
||||
if diff >= update_frequencies['news']:
|
||||
news_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
updateNews()
|
||||
updateWeather(weather_location, weather_key)
|
||||
updateSports(sports_key)
|
||||
last_updates['news'] = news_time
|
||||
|
||||
NY_time = datetime.now(NY_zone)
|
||||
symbols, stock_info = readCSV('csv/tickers.csv', max_stocks)
|
||||
if opening < NY_time < closing and datetime.today().weekday() < 5: # we need to do real time updating
|
||||
print('market open')
|
||||
|
||||
updateStockPrices(symbols)
|
||||
updateUpdate(NY_time)
|
||||
|
||||
elif emptyInfo(symbols, stock_info): # if theres any empty stocks
|
||||
|
||||
updateStockPrices(symbols)
|
||||
updateUpdate(NY_time)
|
||||
#forex updates once every 24hours at 1700 CET
|
||||
|
||||
# update if last update was before the previous days closing
|
||||
forex_time = datetime.strptime(last_updates['forex'], "%d/%m/%Y %H:%M:%S")
|
||||
CET_time = datetime.now(CET_zone)
|
||||
yday_update = (CET_time.replace(hour=17, minute=00, second=0, microsecond=0) - dt.timedelta(days=1)).replace(tzinfo=None)
|
||||
|
||||
if forex_time < yday_update:
|
||||
forex_time = CET_time.strftime("%d/%m/%Y %H:%M:%S")
|
||||
last_updates['forex'] = forex_time
|
||||
updateForex()
|
||||
|
||||
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]
|
||||
|
||||
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")
|
||||
|
||||
|
||||
|
||||
if last_update < yday_closing:
|
||||
|
||||
updateStockPrices(symbols)
|
||||
|
||||
updateUpdate(NY_time)
|
||||
t = time.time()
|
||||
except Exception as e:
|
||||
logf.write(srt(e))
|
||||
logf.write(str(e))
|
||||
print(e)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user