api key functionality added

This commit is contained in:
Your Name
2022-01-31 19:11:01 +00:00
parent 4c0eaef642
commit 051b048848
2 changed files with 75 additions and 86 deletions

View File

@@ -40,41 +40,9 @@ def updateUpdate(NY_time):
f.write(NY_str + '\n') f.write(NY_str + '\n')
f.close() f.close()
def updateStocksFinhubb():
max_stocks = 200
finnhubsandboxAPIkey = "sandbox_c24qddqad3ickpckgg8g" #Finnhub
finnhubAPIkey = "c24qddqad3ickpckgg80" #Finnhub
finnhubClient = finnhub.Client(api_key=finnhubAPIkey)
symbols, stock_info = readJSON('csv/tickers.csv', max_stocks)
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+') def updateStocks(api_key):
CSV.write('name,current,opening\n')
for i, symbol in enumerate(symbols):
CSV.write(symbol + ',' + str(current_prices[i]) + ',' + str(opening_prices[i]) + '\n')
CSV.close()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logf.write(str(e))
logf.write('. file: ' + fname)
logf.write('. line: ' + str(exc_tb.tb_lineno))
logf.write('. type: ' + str(exc_type))
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateStocks():
try: try:
@@ -93,6 +61,8 @@ def updateStocks():
for symbol in symbols: for symbol in symbols:
url += symbol + ',' url += symbol + ','
url += '&apiKey=' + api_key
response = requests.get(url) response = requests.get(url)
data = response.json() data = response.json()
@@ -102,7 +72,7 @@ def updateStocks():
for stock in data: for stock in data:
stock_info[stock['symbol']] = {'current': stock['price'], 'opening': float(stock['price']) - float(stock['change_since'])} stock_info[stock['symbol']] = {'current': stock['price'], 'opening': float(stock['price']) - float(stock['change_since'])}
print(stock_info)
all_stocks_settings['symbols'] = stock_info all_stocks_settings['symbols'] = stock_info
@@ -120,7 +90,7 @@ def updateStocks():
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateCrypto(): def updateCrypto(api_key):
#cypto_info['symbol, base'].keys() = ['current','24hr change'] #cypto_info['symbol, base'].keys() = ['current','24hr change']
@@ -143,6 +113,7 @@ def updateCrypto():
for i,s in enumerate(symbols): for i,s in enumerate(symbols):
url += bases[i] + '-' + s + ',' url += bases[i] + '-' + s + ','
url = url[:-1] #remove last comma url = url[:-1] #remove last comma
url += '&apiKey=' + api_key
print(url) print(url)
response = requests.get(url) response = requests.get(url)
data = response.json() data = response.json()
@@ -158,7 +129,7 @@ def updateCrypto():
json.dump(all_crypto_settings, open('csv/crypto_settings.json', 'w+')) json.dump(all_crypto_settings, open('csv/crypto_settings.json', 'w+'))
except Exception as e: except Exception as e:
raise e
exc_type, exc_obj, exc_tb = sys.exc_info() exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logf.write(str(e)) logf.write(str(e))
@@ -167,7 +138,7 @@ def updateCrypto():
logf.write('. type: ' + str(exc_type)) logf.write('. type: ' + str(exc_type))
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateForex(): def updateForex(api_key):
@@ -192,6 +163,7 @@ def updateForex():
for i,s in enumerate(symbols): for i,s in enumerate(symbols):
url += s + '-' + bases[i] + ',' url += s + '-' + bases[i] + ','
url = url[:-1] #remove last comma url = url[:-1] #remove last comma
url += '&apiKey=' + api_key
response = requests.get(url) response = requests.get(url)
data = response.json() data = response.json()
@@ -221,7 +193,7 @@ def updateForex():
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateNews(): def updateNews(api_key):
#'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?category=technology' #'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?category=technology'
#'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?country=GB' #'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?country=GB'
@@ -233,22 +205,19 @@ def updateNews():
all_settings = json.load(open('csv/news_settings.json', 'r')) all_settings = json.load(open('csv/news_settings.json', 'r'))
try:
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?country={}'.format(all_settings['country']) url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/news?country={}'.format(all_settings['country'])
response = requests.get(url)
data = response.json() url += '&apiKey=' + api_key
response = requests.get(url)
data = response.json()
#load user settings
headlines = data
headline_sources = [headline['source'] for headline in headlines] #load user settings
except Exception as e: headlines = data
headline_sources = [headline['source'] for headline in headlines]
print('news ettings not used', e)
#if no settings just get top headlines
headlines = newsapi.get_top_headlines()['articles']
headline_sources = [headline['source']['name'] for headline in headlines]
headline_titles = [headline['title'] for headline in headlines] headline_titles = [headline['title'] for headline in headlines]
@@ -263,7 +232,7 @@ def updateNews():
except Exception as e: except Exception as e:
print(response)
exc_type, exc_obj, exc_tb = sys.exc_info() exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logf.write(str(e)) logf.write(str(e))
@@ -273,9 +242,9 @@ def updateNews():
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateWeather(): def updateWeather(api_key):
max_cities = 30 max_cities = 30
api_key = 'a9476947fa1a2f712076453bec4a0df5'
try: try:
gn = geocoders.GeoNames(username='fintic') gn = geocoders.GeoNames(username='fintic')
@@ -377,7 +346,7 @@ def updateWeather():
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateLeagueTables(): def updateLeagueTables(api_key):
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?stats=' url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?stats='
try: try:
@@ -391,6 +360,7 @@ def updateLeagueTables():
for league in leagues: for league in leagues:
url += league + ',' url += league + ','
url = url[:-1] # remove last comma url = url[:-1] # remove last comma
url += '&apiKey=' + api_key
r = requests.get(url) r = requests.get(url)
all_data = r.json() all_data = r.json()
@@ -417,10 +387,11 @@ def updateLeagueTables():
teams.append(team) teams.append(team)
leagues_info[league.upper()] = teams leagues_info[league.upper()] = teams
all_settings['leagues'] = leagues_info all_settings['leagues'] = leagues_info
json.dump(all_settings, open( "csv/league_tables.json".format(league), 'w+' )) json.dump(all_settings, open( "csv/league_tables.json".format(league), 'w+' ))
except Exception as e: except Exception as e:
raise e
exc_type, exc_obj, exc_tb = sys.exc_info() exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logf.write(str(e)) logf.write(str(e))
@@ -430,7 +401,7 @@ def updateLeagueTables():
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateLeagueEvents(time): def updateLeagueEvents(api_key, time):
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?{}='.format(time) url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?{}='.format(time)
@@ -443,7 +414,7 @@ def updateLeagueEvents(time):
elif time == 'livescore': elif time == 'livescore':
f = open('csv/live_games.json') f = open('csv/live_games.json')
try: try:
@@ -455,12 +426,13 @@ def updateLeagueEvents(time):
for league in leagues: for league in leagues:
url += league + ',' url += league + ','
url = url[:-1] # remove last comma url = url[:-1] # remove last comma
url += '&apiKey=' + api_key
r = requests.get(url) r = requests.get(url)
all_data = r.json() all_data = r.json()
print(all_data['NFL'][0]) print(all_data)
events = [] events = []
@@ -501,14 +473,14 @@ def updateLeagueEvents(time):
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
def updateSports(): def updateSports(api_key):
#read user settings to decide which sprots to update #read user settings to decide which sprots to update
updateLeagueTables() updateLeagueTables(api_key)
#updateLeagueEvents('livescore') updateLeagueEvents(api_key,'livescore')
#updateLeagueEvents('past') updateLeagueEvents(api_key,'past')
#updateLeagueEvents('upcoming') updateLeagueEvents(api_key,'upcoming')
@@ -533,12 +505,12 @@ def checkStocks(last_update, update_frequency):
if diff >= update_frequency: if diff >= update_frequency:
updated = True updated = True
updateStocks()
elif emptyInfo(symbols, stock_info): # if theres any empty stocks elif emptyInfo(symbols, stock_info): # if theres any empty stocks
updated = True updated = True
updateStocks()
else: else:
@@ -549,18 +521,18 @@ def checkStocks(last_update, update_frequency):
if last_update < yday_closing: if last_update < yday_closing:
updated = True updated = True
updateStocks()
return updated return updated
def updateAll(): def updateAll(api_key):
updateStocks() updateStocks(api_key)
updateCrypto() updateCrypto(api_key)
updateForex() updateForex(api_key)
updateNews() updateNews(api_key)
updateSports() updateSports(api_key)
updateWeather() updateWeather(api_key)
if __name__ == '__main__': if __name__ == '__main__':
@@ -569,7 +541,7 @@ if __name__ == '__main__':
t = time.time() t = time.time()
newsapi = NewsApiClient(api_key='cf08652bd17647b89aaf469a1a8198a9')
update_frequencies = {'stocks':1, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 120} #minutes update_frequencies = {'stocks':1, 'crypto':5, 'forex':60, 'news':120, 'weather': 120, 'sports': 120} #minutes
@@ -588,8 +560,24 @@ if __name__ == '__main__':
#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 #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) #json.dump(update_times, f)
#f.close() #f.close()
f = open('api_keys.txt')
api_keys = f.readlines()
api_key = api_keys[0].strip()
try:
weather_key = api_keys[1].strip()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logf.write(str(e))
logf.write('. file: ' + fname)
logf.write('. line: ' + str(exc_tb.tb_lineno))
logf.write('. type: ' + str(exc_type))
logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
try: try:
f = open('csv/last_updates.json', 'r') f = open('csv/last_updates.json', 'r')
last_updates = json.load(f) last_updates = json.load(f)
@@ -608,7 +596,7 @@ if __name__ == '__main__':
msg = getInput() msg = getInput()
if msg == 'A': if msg == 'A':
updateAll() updateAll(api_key)
#stocks #stocks
stock_time = datetime.strptime(last_updates['stocks'], "%d/%m/%Y %H:%M:%S") stock_time = datetime.strptime(last_updates['stocks'], "%d/%m/%Y %H:%M:%S")
@@ -616,7 +604,7 @@ if __name__ == '__main__':
if checkStocks(stock_time, stock_frequency) or msg == 's': if checkStocks(stock_time, stock_frequency) or msg == 's':
stock_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") stock_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
last_updates['stocks'] = stock_time last_updates['stocks'] = stock_time
updateStocks() updateStocks(api_key)
# crypto # crypto
crypto_time = datetime.strptime(last_updates['crypto'], "%d/%m/%Y %H:%M:%S") crypto_time = datetime.strptime(last_updates['crypto'], "%d/%m/%Y %H:%M:%S")
@@ -626,7 +614,7 @@ if __name__ == '__main__':
diff = (NY_time - crypto_time).total_seconds()/60 #minutes diff = (NY_time - crypto_time).total_seconds()/60 #minutes
if diff >= update_frequencies['crypto'] or msg == 'c': if diff >= update_frequencies['crypto'] or msg == 'c':
crypto_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") crypto_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
updateCrypto() updateCrypto(api_key)
last_updates['crypto'] = crypto_time last_updates['crypto'] = crypto_time
@@ -638,7 +626,7 @@ if __name__ == '__main__':
diff = (NY_time - weather_time).total_seconds()/60 #minutes diff = (NY_time - weather_time).total_seconds()/60 #minutes
if diff >= update_frequencies['weather'] or msg == 'w': if diff >= update_frequencies['weather'] or msg == 'w':
weather_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") weather_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
updateWeather() updateWeather(weather_key)
last_updates['weather'] = weather_time last_updates['weather'] = weather_time
@@ -650,7 +638,7 @@ if __name__ == '__main__':
diff = (NY_time - news_time).total_seconds()/60 #minutes diff = (NY_time - news_time).total_seconds()/60 #minutes
if diff >= update_frequencies['news'] or msg == 'n': if diff >= update_frequencies['news'] or msg == 'n':
news_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") news_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
updateNews() updateNews(api_key)
last_updates['news'] = news_time last_updates['news'] = news_time
# sports # sports
@@ -661,7 +649,7 @@ if __name__ == '__main__':
diff = (NY_time - sports_time).total_seconds()/60 #minutes diff = (NY_time - sports_time).total_seconds()/60 #minutes
if diff >= update_frequencies['sports'] or msg == 'S': if diff >= update_frequencies['sports'] or msg == 'S':
sports_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") sports_time = NY_time.strftime("%d/%m/%Y %H:%M:%S")
updateSports() updateSports(api_key)
last_updates['sports'] = sports_time last_updates['sports'] = sports_time
#forex updates once every 24hours at 1700 CET #forex updates once every 24hours at 1700 CET
@@ -680,7 +668,7 @@ if __name__ == '__main__':
if forex_time < yday_update or msg == 'f' or (diff >= update_frequencies['forex'] and forex_open): if forex_time < yday_update or msg == 'f' or (diff >= update_frequencies['forex'] and forex_open):
forex_time = CET_time.strftime("%d/%m/%Y %H:%M:%S") forex_time = CET_time.strftime("%d/%m/%Y %H:%M:%S")
last_updates['forex'] = forex_time last_updates['forex'] = forex_time
updateForex() updateForex(api_key)
json.dump(last_updates, open('csv/last_updates.json', 'w+')) json.dump(last_updates, open('csv/last_updates.json', 'w+'))

View File

@@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
touch log.txt touch log.txt
touch api_keys.txt
mkdir user_uploads mkdir user_uploads