data from frontend saved to json files

This commit is contained in:
Neythen
2021-09-19 19:19:09 +01:00
parent 7f70594aaf
commit ec619563b1
23 changed files with 177 additions and 36 deletions

138
server.py
View File

@@ -19,7 +19,8 @@ from multiprocessing import Process
from subprocess import Popen, PIPE
import numpy as np
#stock_ticker = StockTicker()
api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
print('API CALLER NOT STARTED')
#api_caller = pexpect.spawn("sudo -E python3 api_caller.py")
command = 300
@@ -321,12 +322,141 @@ def display_format():
@app.route("/feature_settings", methods = ['PUT', 'POST', 'GET'])
def feature_settings():
data = str(request.data)
print('feature settings')
print(data)
data = str(request.data.decode('utf-8'))
input_settings = json.loads(data)
print(input_settings)
feature = input_settings['feature']
if feature in ['Stocks', 'Crypto', 'Forex']:
save_trade_settings(input_settings)
elif feature in ['Current Weather', 'Daily Forecast']:
save_weather_settings(input_settings)
elif feature == 'News':
save_news_settings(input_settings)
elif 'Sports' in feature:
save_sports_settings(input_settings)
elif feature in ['Custom GIFs', 'Custom Images']:
save_image_settings(input_settings)
return index()
def combine_dict(current_settings, input_symbols, current_key):
# removes keys not in input from current_settings[current_key] and adds keys not in current from input
current_symbols = list(current_settings[current_key].keys())
# add any stock that arent current in the settings
for IS in input_symbols:
if IS not in current_symbols:
current_settings[current_key][IS] = -1
# remove stocks not in settings
for CS in current_symbols:
if CS not in input_symbols:
del current_settings[current_key][CS]
return current_settings
def save_trade_settings(input_settings):
filename = input_settings['feature'].lower() + '_settings.json'
current_settings = json.load(open('csv/' + filename, 'r'))
current_settings['speed'] = input_settings['speed'].lower()
current_settings['animation'] = input_settings['animation'].lower()
current_settings['percent'] = input_settings['percent']
current_settings['point'] = input_settings['point']
current_settings['no_logos'] = input_settings['no_logos']
current_settings['chart'] = input_settings['chart']
current_settings['title'] = input_settings['title']
current_settings = combine_dict(current_settings, input_settings['symbols'], 'symbols')
json.dump(current_settings, open('csv/' + filename, 'w+'))
def save_weather_settings(input_settings):
filename = 'current_weather.json' if input_settings['feature'] == 'Current Weather' else 'daily_weather.json'
current_settings = json.load(open('csv/' + filename, 'r'))
current_settings['speed'] = input_settings['speed'].lower()
current_settings['animation'] = input_settings['animation'].lower()
current_settings['temp'] = input_settings['temp'].lower()
current_settings['wind_speed'] = input_settings['wind_speed'].lower()
current_settings['colour'] = input_settings['colour'].lower()
current_settings['city_colour'] = input_settings['city_colour'].lower()
current_settings['title'] = input_settings['title']
if input_settings['feature'] == 'Daily Forecast':
current_settings['current_weather'] = input_settings['current_weather']
current_settings = combine_dict(current_settings, input_settings['cities'], 'cities')
json.dump(current_settings, open('csv/' + filename, 'w+'))
def save_news_settings(input_settings):
filename = 'news_settings.json'
current_settings = json.load(open('csv/' + filename, 'r'))
current_settings['speed'] = input_settings['speed'].lower()
current_settings['animation'] = input_settings['animation'].lower()
current_settings['title'] = input_settings['title']
current_settings['category'] = input_settings['category'].lower()
current_settings['country'] = input_settings['country']
current_settings = combine_dict(current_settings, input_settings['sources'], 'sources')
json.dump(current_settings, open('csv/' + filename, 'w+'))
def save_sports_settings(input_settings):
feature = input_settings['feature']
if feature == 'Sports (Upcoming Games)':
filename = 'upcoming_games.json'
elif feature == 'Sports (Past Games)':
filename = 'past_games.json'
elif feature == 'Sports (Live Games)':
filename = 'live_games.json'
elif feature == 'Sports (Team Stats)':
filename = 'team_stats.json'
current_settings = json.load(open('csv/' + filename, 'r'))
current_settings['speed'] = input_settings['speed'].lower()
current_settings['animation'] = input_settings['animation'].lower()
current_settings['title'] = input_settings['title']
current_settings = combine_dict(current_settings, input_settings['leagues'], 'leagues')
json.dump(current_settings, open('csv/' + filename, 'w+'))
# for images and GIFs
def save_image_settings(input_settings):
filename = 'image_settings.json' if input_settings['feature'] == 'Custom Images' else 'GIF_settings.json'
current_settings = input_settings
current_settings['speed'] = input_settings['speed'].lower()
current_settings['animation'] = input_settings['animation'].lower()
current_settings['title'] = input_settings['title']
del current_settings['feature']
json.dump(current_settings, open('csv/' + filename, 'w+'))
@app.route("/stop")
def stop():
print('stop')