From c53038b8edddba8211d7675450fcd7eca1a6708a Mon Sep 17 00:00:00 2001 From: Neythen Treloar Date: Sat, 5 Mar 2022 14:03:11 +0000 Subject: [PATCH] trial of new databasecaller method --- database_caller.py | 69 +++++++++++++------------ server.py | 124 +++++++++++---------------------------------- startup.sh | 2 +- 3 files changed, 68 insertions(+), 127 deletions(-) diff --git a/database_caller.py b/database_caller.py index e2db9d7..86a3874 100755 --- a/database_caller.py +++ b/database_caller.py @@ -43,7 +43,7 @@ def updateUpdate(NY_time): -def updateStocks(api_key): +def updateStocks(api_key, logf): try: @@ -95,8 +95,8 @@ def updateStocks(api_key): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateCrypto(api_key): - +def updateCrypto(api_key, logf): + #cypto_info['symbol, base'].keys() = ['current','24hr change'] try: @@ -122,6 +122,7 @@ def updateCrypto(api_key): print(url) response = requests.get(url) data = response.json() + print(url) print(data) @@ -138,11 +139,12 @@ def updateCrypto(api_key): coin_info[symbol.upper() + ',' + base.upper()] = {'current': d['price'], '24hr_change': d['price_over_24hr'], 'percent_change': d['percent_over_24hr']} all_crypto_settings['symbols'] = coin_info - - json.dump(all_crypto_settings, open('csv/crypto_settings.json', 'w+')) + f = open('csv/crypto_settings.json', 'w+') + json.dump(all_crypto_settings, f) + f.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)) @@ -151,7 +153,7 @@ def updateCrypto(api_key): 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 updateForex(api_key): +def updateForex(api_key, logf): @@ -206,7 +208,7 @@ def updateForex(api_key): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateNews(api_key): +def updateNews(api_key, logf): #'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' @@ -263,7 +265,7 @@ def updateNews(api_key): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateWeather(api_key): +def updateWeather(api_key, logf): max_cities = 30 try: @@ -363,7 +365,7 @@ def updateWeather(api_key): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateLeagueTables(api_key): +def updateLeagueTables(api_key, logf): url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?stats=' try: @@ -428,7 +430,7 @@ def updateLeagueTables(api_key): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateLeagueEvents(api_key, time): +def updateLeagueEvents(api_key, time, logf): url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/sports?{}='.format(time) @@ -507,14 +509,14 @@ def updateLeagueEvents(api_key, time): logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -def updateSports(api_key): +def updateSports(api_key, logf): #read user settings to decide which sprots to update - updateLeagueTables(api_key) + updateLeagueTables(api_key, logf) - updateLeagueEvents(api_key,'livescore') - updateLeagueEvents(api_key,'past') - updateLeagueEvents(api_key,'upcoming') + updateLeagueEvents(api_key,'livescore', logf) + updateLeagueEvents(api_key,'past', logf) + updateLeagueEvents(api_key,'upcoming', logf) @@ -560,19 +562,19 @@ def checkStocks(last_update, update_frequency): return updated -def updateAll(api_key, weather_key): - updateStocks(api_key) +def updateAll(api_key, weather_key, logf): + updateStocks(api_key, logf) - updateCrypto(api_key) + updateCrypto(api_key, logf) - updateForex(api_key) + updateForex(api_key, logf) - updateNews(api_key) + updateNews(api_key, logf) - updateSports(api_key) + updateSports(api_key, logf) if weather_key: - updateWeather(weather_key) + updateWeather(weather_key, logf) if __name__ == '__main__': @@ -584,7 +586,7 @@ if __name__ == '__main__': - update_frequencies = {'stocks':1, 'crypto':1, 'forex':60, 'news':120, 'weather': 120, 'sports': 1440} #minutes + update_frequencies = {'stocks':1, 'crypto':1, 'forex':1, 'news':1, 'weather': 1, 'sports': 1} #minutes NY_zone = pytz.timezone('America/New_York') CET_zone = pytz.timezone('EST') @@ -641,7 +643,7 @@ if __name__ == '__main__': msg = getInput() if msg == 'A': - update_process = Process(target = updateAll, args = (api_key,weather_key)) + update_process = Process(target = updateAll, args = (api_key,weather_key, logf)) update_process.start() update_processes.append(update_process) stock_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") @@ -663,7 +665,7 @@ if __name__ == '__main__': stock_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") last_updates['stocks'] = stock_time #updateStocks(api_key) - update_process = Process(target = updateStocks, args = (api_key,)) + update_process = Process(target = updateStocks, args = (api_key,logf)) update_process.start() update_processes.append(update_process) @@ -678,8 +680,10 @@ if __name__ == '__main__': if diff >= update_frequencies['crypto'] or msg == 'c': crypto_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") #updateCrypto(api_key) + print('UPDATING CRYPTO') + last_updates['crypto'] = crypto_time - update_process = Process(target = updateCrypto, args = (api_key,)) + update_process = Process(target = updateCrypto, args = (api_key,logf)) update_process.start() update_processes.append(update_process) @@ -695,7 +699,7 @@ if __name__ == '__main__': #updateWeather(weather_key) last_updates['weather'] = weather_time - update_process = Process(target = updateWeather, args = (weather_key,)) + update_process = Process(target = updateWeather, args = (weather_key,logf)) update_process.start() update_processes.append(update_process) @@ -710,7 +714,7 @@ if __name__ == '__main__': news_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") #updateNews(api_key) last_updates['news'] = news_time - update_process = Process(target = updateNews, args = (api_key,)) + update_process = Process(target = updateNews, args = (api_key,logf)) update_process.start() update_processes.append(update_process) @@ -722,7 +726,7 @@ if __name__ == '__main__': sports_time = NY_time.strftime("%d/%m/%Y %H:%M:%S") #updateSports(api_key) last_updates['sports'] = sports_time - update_process = Process(target = updateSports, args = (api_key,)) + update_process = Process(target = updateSports, args = (api_key,logf)) update_process.start() update_processes.append(update_process) @@ -743,7 +747,7 @@ if __name__ == '__main__': forex_time = CET_time.strftime("%d/%m/%Y %H:%M:%S") last_updates['forex'] = forex_time #updateForex(api_key) - update_process = Process(target = updateForex, args = (api_key,)) + update_process = Process(target = updateForex, args = (api_key,logf)) update_process.start() update_processes.append(update_process) @@ -753,9 +757,11 @@ if __name__ == '__main__': if not process.is_alive(): process.join() process.terminate() + update_processes.remove(process) 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)) @@ -764,5 +770,6 @@ if __name__ == '__main__': 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]))) + diff --git a/server.py b/server.py index f5eff78..0ac5298 100755 --- a/server.py +++ b/server.py @@ -29,11 +29,11 @@ import sys #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py") -api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") +#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") time.sleep(3) -api_caller.sendline('A') +#api_caller.sendline('A') #os.system("sudo ./check_update.sh") @@ -82,7 +82,8 @@ def process_file(path, filename): class DummyProcess(): def close(self): return True - + + app = Flask(__name__) @app.route("/", methods=['GET', 'POST']) @@ -156,6 +157,8 @@ def index(): 'api_keys':api_keys, 'wifi_SSID':wifi_SSID } + + #print('database caller:', api_caller.isalive()) return render_template('index.html', **templateData) @@ -196,21 +199,21 @@ def start(): global displaying_screensaver global ticker - global api_caller + #global api_caller global professional global ticker_stopped if displaying_screensaver: screensaver_p.close() ticker = pexpect.spawn("sudo -E python3 stockTicker.py") #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py") - api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") + #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") displaying_screensaver = False if ticker_stopped: ticker = pexpect.spawn("sudo -E python3 stockTicker.py") ticker_stopped = False - api_caller.sendline('A') + #api_caller.sendline('A') ticker.sendline('K') @@ -222,7 +225,7 @@ def stop(): print('stop') global displaying_screensaver global ticker - global api_caller + #global api_caller global professional global ticker_stopped @@ -245,7 +248,7 @@ def stop(): screensaver_p.close() ticker = pexpect.spawn("sudo -E python3 stockTicker.py") #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py") - api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") + #api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py") displaying_screensaver = False return index() @@ -496,7 +499,7 @@ def saveWeatherAPIKey(): def screensaver(): global displaying_screensaver global ticker - global api_caller + #global api_caller global screensaver_p data = str(request.data) @@ -504,7 +507,7 @@ def screensaver(): if displaying_screensaver: screensaver_p.close() else: - api_caller.close() + #api_caller.close() ticker.close() @@ -550,11 +553,12 @@ def combine_dict(current_settings, input_symbols, current_key): return new_settings def save_trade_settings(input_settings): - + filename = input_settings['feature'].lower() + '_settings.json' - - current_settings = json.load(open('csv/' + filename, 'r')) + f = open('csv/' + filename, 'r') + current_settings = json.load(f) + f.close() current_settings['speed'] = input_settings['speed'].lower() current_settings['animation'] = input_settings['animation'].lower() @@ -566,14 +570,18 @@ def save_trade_settings(input_settings): current_settings = combine_dict(current_settings, input_settings['symbols'], 'symbols') - json.dump(current_settings, open('csv/' + filename, 'w+')) - + f = open('csv/' + filename, 'w+') + json.dump(current_settings, f) + f.close() if input_settings['feature'].lower() == 'stocks': - api_caller.sendline('s') + #api_caller.sendline('s') + pass elif input_settings['feature'].lower() == 'crypto': - api_caller.sendline('c') + #api_caller.sendline('c') + pass elif input_settings['feature'].lower() == 'forex': - api_caller.sendline('f') + #api_caller.sendline('f') + pass def save_weather_settings(input_settings): @@ -605,7 +613,7 @@ def save_weather_settings(input_settings): current_settings = combine_dict(current_settings, input_settings['locations'], 'locations') json.dump(current_settings, open('csv/' + filename, 'w+')) - api_caller.sendline('w') + #api_caller.sendline('w') def save_news_settings(input_settings): filename = 'news_settings.json' @@ -628,7 +636,7 @@ def save_news_settings(input_settings): print(current_settings) json.dump(current_settings, open('csv/' + filename, 'w+')) - api_caller.sendline('n') + #api_caller.sendline('n') def save_sports_settings(input_settings): @@ -654,7 +662,7 @@ def save_sports_settings(input_settings): json.dump(current_settings, open('csv/' + filename, 'w+')) - api_caller.sendline('S') + #api_caller.sendline('S') # for images and GIFs def save_image_settings(input_settings): @@ -702,81 +710,7 @@ def shutdown(): -@app.route("/matrix") -def matrix(): - - if "Run Stocks" in request.form: - ticker.sendline('K') - ticker.sendline('S') - elif "Run Crypto" in request.form: - ticker.sendline('K') - ticker.sendline('C') - - elif "Run Forex" in request.form: - ticker.sendline('K') - ticker.sendline('F') - - elif "Run News" in request.form: - ticker.sendline('K') - ticker.sendline('N') - - elif "Run Weather" in request.form: - - - ticker.sendline('K') - ticker.sendline('W') - - elif "Run Daily Weather" in request.form: - - ticker.sendline('K') - ticker.sendline('D') - - elif "Past NHL" in request.form: - ticker.sendline('K') - ticker.sendline('P') - - elif "Future NHL" in request.form: - ticker.sendline('K') - ticker.sendline('l') - - elif "Live NHL" in request.form: - ticker.sendline('K') - ticker.sendline('L') - - elif "Premier league table" in request.form: - - ticker.sendline('K') - ticker.sendline('t') - - elif "Professional" in request.form: - - ticker.sendline('K') - ticker.sendline('b') - - elif "All" in request.form: - - ticker.sendline('K') - ticker.sendline('A') - - elif "Multiple" in request.form: - - ticker.sendline('K') - ticker.sendline('+') - - elif "Stop Display" in request.form: - - - LastCommand = 'Stop display at next checkpoint' - ticker.sendline('K') - - elif "Shutdown the pi" in request.form: - - try: - os.system("sudo shutdown now") - except: - print("couldn't shutdown") - return index() if __name__ == "__main__": diff --git a/startup.sh b/startup.sh index c5cc6a8..bca32b3 100755 --- a/startup.sh +++ b/startup.sh @@ -1,3 +1,3 @@ #!/bin/sh cd /home/pi/fintic -sudo -E python3 server.py +sudo -E taskset -c 3 python3 database_caller.py & sudo -E python3 server.py