fintic-tracker/server.py

886 lines
26 KiB
Python
Raw Normal View History

2021-11-29 15:20:54 +00:00
# Copyright (C) 2021 Neythen Treloar neythen@fintic.io
2021-04-26 18:51:21 +00:00
#
# This file is part of stockTicker project for justinodunn.
#
# stockTicker can not be copied and/or distributed without the express
2021-11-29 15:20:54 +00:00
# permission of Neythen Treloar
2021-04-26 18:51:21 +00:00
from flask import Flask, render_template, request
from stockTicker import StockTicker
2021-04-26 18:51:21 +00:00
from werkzeug.utils import secure_filename
import os
import datetime
import threading
import csv
import pexpect
2021-05-05 21:14:04 +00:00
import time
2021-05-27 19:10:57 +00:00
import json
from multiprocessing import Process
2022-01-24 18:40:06 +00:00
import subprocess
from subprocess import Popen, PIPE
import numpy as np
2021-12-10 17:06:47 +00:00
import copy
2022-01-24 18:40:06 +00:00
import urllib.request
2022-02-28 16:38:24 +00:00
import sys
#stock_ticker = StockTicker()
2022-04-14 17:03:50 +00:00
import traceback
2021-12-22 09:43:36 +00:00
2021-12-29 13:05:45 +00:00
#open('log.txt', 'w').close() #wipe logs
2021-12-22 09:43:36 +00:00
2022-02-28 16:38:24 +00:00
2022-01-24 18:40:06 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
2022-03-06 18:36:03 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
#time.sleep(3)
2022-02-28 16:38:45 +00:00
2022-03-01 18:27:45 +00:00
2022-03-05 14:03:11 +00:00
#api_caller.sendline('A')
2022-02-28 16:38:45 +00:00
2022-03-01 18:27:45 +00:00
2022-03-08 13:36:23 +00:00
#pexpect.spawn("./check_update.sh")
2022-02-28 16:38:24 +00:00
2021-11-17 19:34:13 +00:00
displaying_screensaver = False
2021-12-29 13:05:45 +00:00
uploading = False
2021-11-17 19:34:13 +00:00
screensaver_p = None
ticker_stopped = False
2022-04-07 17:35:38 +00:00
2022-04-14 17:03:50 +00:00
try:
f = open('csv/display_settings.json', 'r')
professional = json.load(f)[0] == "Professional"
f.close()
except:
professional = False
2021-04-26 18:51:21 +00:00
command = 300
tickerList = 0
DelayTime = 20
LastCommand = ''
2022-04-07 17:35:38 +00:00
speedTime = 25
2021-04-26 18:51:21 +00:00
LOGO_FOLDER = 'logos/'
CSV_FOLDER = 'csv/new/'
ALLOWED_EXTENSIONS = {'csv', 'png'}
ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
2022-02-15 18:33:32 +00:00
time.sleep(2) # give the ticker time to initialise
2022-02-14 18:48:27 +00:00
2022-04-07 18:10:41 +00:00
try:
2022-04-14 17:03:50 +00:00
f = open('csv/system_info.json', 'r')
2022-04-07 18:10:41 +00:00
system_info = json.load(f)
2022-04-10 12:06:03 +00:00
except Exception as e:
2022-04-07 18:10:41 +00:00
system_info = {"update_available": False, "first_boot": False}
2022-04-14 17:03:50 +00:00
f = open('csv/system_info.json', 'w')
2022-04-07 18:10:41 +00:00
json.dump(system_info, f)
2022-04-14 17:03:50 +00:00
system_info = json.load(f)
2022-04-07 17:35:38 +00:00
f.close()
2022-02-14 18:48:27 +00:00
2022-02-15 18:33:32 +00:00
ticker.sendline('*') # run startup gif by default
time.sleep(8)
2022-02-14 18:48:27 +00:00
if system_info['first_boot']: # let startup message display
2022-02-15 18:33:32 +00:00
ticker.sendline('-')
2022-02-14 18:48:27 +00:00
system_info['first_boot'] = False
2022-04-07 17:35:38 +00:00
f = open('csv/system_info.json', 'w')
json.dump(system_info,f)
f.close()
2022-02-15 18:33:32 +00:00
else:
ticker.sendline('A') # run by default
2021-07-08 18:42:21 +00:00
2021-04-26 18:51:21 +00:00
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_file(path, filename):
2021-12-14 15:38:09 +00:00
default_csv = csv.writer(open('csv/tickers.csv', 'w+'))
2021-04-26 18:51:21 +00:00
new_csv = csv.reader(open((path), 'r'))
for row in new_csv:
default_csv.writerow(row)
2021-12-06 20:10:14 +00:00
class DummyProcess():
def close(self):
return True
2022-03-05 14:03:11 +00:00
2021-04-26 18:51:21 +00:00
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def index():
2021-11-22 19:08:14 +00:00
global command
all_features = ['Current Weather','Daily Forecast','News', 'Sports (Upcoming Games)','Sports (Past Games)','Sports (Live Games)',
2022-06-12 17:24:31 +00:00
'Sports (Team Stats)','Custom Images', 'Custom GIFs', 'Custom Messages', 'Stocks', 'Crypto', 'Forex', 'Commodities']
2021-11-22 19:08:14 +00:00
global professional
2022-02-26 17:00:19 +00:00
2022-04-07 17:35:38 +00:00
f = open('csv/display_settings.json', 'r')
feature_settings = json.load(f)
f.close()
2021-11-22 19:08:14 +00:00
if not professional:
2022-04-07 17:35:38 +00:00
f = open('csv/display_settings.json', 'r')
currently_displaying = json.load(f)[1]
f.close()
not_displaying = [f for f in all_features if f not in currently_displaying[0]]
not_displaying2 = [f for f in all_features if f not in currently_displaying[0]]
2021-11-22 19:08:14 +00:00
elif professional:
2022-04-07 17:35:38 +00:00
f = open('csv/display_settings.json', 'r')
currently_displaying = json.load(f)[1]
f.close()
not_displaying = [f for f in all_features if f not in currently_displaying[0]]
not_displaying2 = [f for f in all_features if f not in currently_displaying[1]]
2022-03-02 14:49:44 +00:00
with open('api_keys.txt', 'r') as f:
api_key2 = f.readlines()
2021-11-22 19:08:14 +00:00
2022-03-02 15:13:13 +00:00
with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'r') as f:
wifiline = f.readlines()
2021-11-22 19:08:14 +00:00
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
logos_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos')
LogoList = os.listdir(logos_path)
2022-04-07 17:35:38 +00:00
f = open('csv/stocks_settings.json', 'r')
stocks_settings = json.load(f)
f.close()
2022-06-12 17:24:31 +00:00
f = open('csv/commodities_settings.json', 'r')
commodities_settings = json.load(f)
f.close()
2022-04-07 17:35:38 +00:00
f= open('csv/crypto_settings.json', 'r')
crypto_settings = json.load(f)
f.close()
f = open('csv/forex_settings.json', 'r')
forex_settings = json.load(f)
f.close()
f = open('csv/current_weather.json', 'r')
current_weather= json.load(f)
f.close()
f = open('csv/daily_weather.json', 'r')
daily_weather = json.load(f)
f.close()
f = open('csv/news_settings.json', 'r')
news_settings = json.load(f)
f.close()
f = open('csv/upcoming_games.json', 'r')
upcoming_games = json.load(f)
f.close()
f = open('csv/live_games.json', 'r')
live_games = json.load(f)
f.close()
f = open('csv/past_games.json', 'r')
past_games = json.load(f)
f.close()
f = open('csv/league_tables.json', 'r')
team_stats = json.load(f)
f.close()
f = open('csv/image_settings.json', 'r')
image_settings = json.load(f)
f.close()
f = open('csv/GIF_settings.json', 'r')
GIF_settings = json.load(f)
f.close()
f = open('csv/message_settings.json', 'r')
message_settings = json.load(f)
f.close()
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
2022-04-11 17:33:28 +00:00
2022-03-07 17:43:34 +00:00
try: # incase this doesnt exist
api_keys = api_key2[1]
except:
api_keys = ''
try:
wifi_SSID = wifiline[5][6:].replace('"','')
except:
wifi_SSID = ''
2022-06-11 16:01:44 +00:00
try:
wifi_PSK = wifiline[6][5:].replace('"','')
except:
wifi_PSK = ''
2021-11-22 19:08:14 +00:00
templateData = {
2022-02-27 09:52:37 +00:00
'system_info':system_info,
2021-09-20 19:46:15 +00:00
'currently_displaying': currently_displaying,
'not_displaying': not_displaying,
'not_displaying2': not_displaying2,
2021-09-20 19:46:15 +00:00
'stocks_settings': stocks_settings,
2022-06-12 17:24:31 +00:00
'commodities_settings': commodities_settings,
2021-09-20 19:46:15 +00:00
'crypto_settings': crypto_settings,
'forex_settings': forex_settings,
'current_weather': current_weather,
'daily_weather': daily_weather,
'news_settings': news_settings,
'upcoming_games': upcoming_games,
'past_games': past_games,
'live_games': live_games,
'team_stats': team_stats,
'image_settings':image_settings,
'GIF_settings':GIF_settings,
2021-11-22 19:08:14 +00:00
'message_settings':message_settings,
2021-12-06 20:10:14 +00:00
'professional':professional,
2022-03-02 14:49:44 +00:00
'general_settings':general_settings,
2022-03-02 15:13:13 +00:00
'api_keys':api_keys,
2022-06-11 16:02:10 +00:00
'wifi_SSID':wifi_SSID,
'wifi_PSK':wifi_PSK
2021-04-26 18:51:21 +00:00
}
2022-03-05 14:03:11 +00:00
2022-04-07 17:35:38 +00:00
2021-11-22 19:08:14 +00:00
return render_template('index.html', **templateData)
2021-04-26 18:51:21 +00:00
2021-12-10 17:06:47 +00:00
def save_displaying(input_settings):
2022-04-07 17:35:38 +00:00
global professional
2022-04-07 17:35:38 +00:00
2022-06-12 17:24:31 +00:00
all_settings = ['Stocks', 'Crypto', 'Forex', 'Commodities', 'Current Weather', 'Daily Forecast', 'News', 'Sports (Upcoming Games)', 'Sports (Past Games)',
'Sports (Live Games)', 'Sports (Team Stats)', 'Custom Images', 'Custom GIFs', 'Custom Messages']
professional = len(input_settings) == 2
if professional:
2022-06-12 17:24:31 +00:00
all_settings = ['Stocks', 'Crypto', 'Forex', 'Commodities', 'Current Weather', 'News']
2021-12-10 17:06:47 +00:00
positions = []
display_settings = []
2021-12-10 17:06:47 +00:00
2021-11-22 19:19:26 +00:00
if professional:
2021-11-22 19:19:26 +00:00
input_settings[0] = [i for i in input_settings[0] if i in all_settings]
input_settings[1] = [i for i in input_settings[1] if i in all_settings]
2021-12-10 17:06:47 +00:00
2021-11-22 19:08:14 +00:00
s = "Professional" if professional else "Standard"
display_settings = [s] + [input_settings]
2022-04-14 17:03:50 +00:00
with open('csv/display_settings.json', 'w') as f:
json.dump(list(display_settings), f)
2021-12-10 17:06:47 +00:00
@app.route ("/start", methods = ['PUT', 'POST'])
def start():
2022-02-15 19:50:20 +00:00
2021-12-10 17:06:47 +00:00
global displaying_screensaver
global ticker
2022-03-05 14:03:11 +00:00
#global api_caller
2021-12-10 17:06:47 +00:00
global professional
global ticker_stopped
2021-12-10 17:06:47 +00:00
if displaying_screensaver:
screensaver_p.close()
ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
2022-02-21 18:38:05 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
2022-03-05 14:03:11 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
2021-12-10 17:06:47 +00:00
displaying_screensaver = False
if ticker_stopped:
ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
ticker_stopped = False
2022-03-05 14:03:11 +00:00
#api_caller.sendline('A')
ticker.sendline('K')
ticker.sendline('A')
return index()
2021-11-18 17:50:50 +00:00
@app.route("/stop")
def stop():
2022-04-07 17:35:38 +00:00
2021-11-18 17:50:50 +00:00
global displaying_screensaver
2021-12-06 20:10:14 +00:00
global ticker
2022-03-05 14:03:11 +00:00
#global api_caller
2021-12-06 20:10:14 +00:00
global professional
global ticker_stopped
2022-04-10 12:17:47 +00:00
2021-11-18 17:50:50 +00:00
if not displaying_screensaver:
2022-04-10 12:17:47 +00:00
ticker.sendline('K')
2022-02-22 12:41:30 +00:00
time.sleep(0.1) # give time for leds to turn off
ticker.close()
2021-11-18 17:50:50 +00:00
else:
screensaver_p.close()
if not ticker_stopped:
2022-02-22 12:41:30 +00:00
time.sleep(0.1) # give time for leds to turn off
ticker.close()
ticker_stopped = True
2021-12-29 13:05:45 +00:00
2021-11-18 17:50:50 +00:00
if displaying_screensaver:
screensaver_p.close()
2022-06-21 17:03:15 +00:00
#ticker = pexpect.spawn("sudo -E python3 -W ignore stockTicker.py")
2022-02-21 18:38:05 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 api_caller.py")
2022-03-05 14:03:11 +00:00
#api_caller = pexpect.spawn("sudo -E taskset -c 3 python3 database_caller.py")
2021-11-18 17:50:50 +00:00
displaying_screensaver = False
2021-11-18 17:50:50 +00:00
return index()
2021-11-20 15:30:31 +00:00
@app.route("/update", methods=['PUT','POST'])
def update():
2022-04-11 17:33:28 +00:00
2022-04-10 12:06:03 +00:00
2022-04-14 17:03:50 +00:00
2022-04-10 12:06:03 +00:00
try:
2022-04-14 17:03:50 +00:00
f = open('csv/system_info.json', 'r')
2022-04-10 12:06:03 +00:00
system_info = json.load(f)
except Exception as e:
system_info = {"update_available": False, "first_boot": False}
system_info['update_available'] = False
2022-04-14 17:03:50 +00:00
f = open('csv/system_info.json', 'w')
2022-04-10 12:06:03 +00:00
json.dump(system_info, f)
f.close()
2022-04-11 17:33:28 +00:00
2022-03-07 18:24:24 +00:00
os.system("./update.sh")
os.system("sudo reboot now")
2021-12-01 16:47:08 +00:00
return index()
2021-11-20 15:30:31 +00:00
2022-02-26 15:50:10 +00:00
@app.route("/restart")
def restart():
os.system("sudo reboot now")
return index()
@app.route("/reset")
def reset():
2022-04-07 17:35:38 +00:00
os.system("sudo ./setup_config_files.sh")
2022-06-24 07:46:11 +00:00
os.system("sudo reboot now")
return index()
2021-12-10 17:06:47 +00:00
@app.route("/save", methods = ['PUT', 'POST', 'GET'])
def save():
2022-04-07 17:35:38 +00:00
2021-12-29 13:05:45 +00:00
global uploading
2021-09-16 19:23:04 +00:00
2021-09-19 18:19:09 +00:00
data = str(request.data.decode('utf-8'))
2021-12-29 13:05:45 +00:00
2021-09-19 18:19:09 +00:00
input_settings = json.loads(data)
2021-12-15 11:06:54 +00:00
2022-04-07 17:35:38 +00:00
2021-12-29 13:05:45 +00:00
2021-12-10 17:06:47 +00:00
save_displaying(input_settings['displaying'])
input_settings= input_settings['feature_settings']
2021-09-19 18:19:09 +00:00
feature = input_settings['feature']
2022-06-12 17:24:31 +00:00
if feature in ['Stocks', 'Crypto', 'Forex', 'Commodities']:
2021-09-19 18:19:09 +00:00
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']:
2021-12-29 13:05:45 +00:00
images = request.files
names = list(request.files.keys())
for name in names:
2022-04-07 17:35:38 +00:00
2021-12-29 13:05:45 +00:00
images[name].save('user_uploads/' +name)
2021-09-19 18:19:09 +00:00
save_image_settings(input_settings)
elif feature == 'Custom Messages':
save_message_settings(input_settings)
2021-09-16 19:23:04 +00:00
return index()
2021-09-19 18:19:09 +00:00
2021-10-07 19:40:43 +00:00
# saves files uploaded to the webpage for images and GIFs
@app.route("/upload", methods = ['PUT', 'POST', 'GET'])
def upload():
2022-04-07 17:35:38 +00:00
2021-12-29 13:05:45 +00:00
global uploading
uploading = True
2021-10-07 19:40:43 +00:00
try:
2021-12-22 09:43:36 +00:00
2021-10-07 19:40:43 +00:00
images = request.files
names = list(request.files.keys())
2021-12-21 17:21:04 +00:00
2021-10-07 19:40:43 +00:00
for name in names:
images[name].save('user_uploads/' +name)
except Exception as e:
2022-04-07 17:35:38 +00:00
2022-03-02 10:42:50 +00:00
uploading = False
2021-10-07 19:40:43 +00:00
return index()
2021-11-20 15:30:31 +00:00
2021-12-21 17:21:04 +00:00
def remove_old_uploads():
#remove old files
2022-04-07 17:35:38 +00:00
f = open('csv/image_settings.json', 'r')
image_settings = json.load(f)
f.close()
f = open('csv/GIF_settings.json', 'r')
GIF_settings = json.load(f)
f.close()
2021-12-21 17:21:04 +00:00
for filename in os.listdir('user_uploads'):
if filename not in image_settings['images'] and filename not in GIF_settings['images']:
os.remove('user_uploads/'+filename)
2021-11-20 15:30:31 +00:00
2021-11-18 17:50:50 +00:00
@app.route("/brightness", methods=['PUT','POST'])
def brightness():
global brightness
data= request.data.decode('utf-8')
settings = json.loads(data)
2022-04-07 17:35:38 +00:00
2021-11-18 17:50:50 +00:00
brightness =settings['brightness']
2022-04-14 17:03:50 +00:00
brightness = max(min(int(brightness), 10), 1)
ticker.sendline(str(brightness-1))
2022-04-07 17:35:38 +00:00
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
2021-12-08 17:00:42 +00:00
general_settings['brightness'] = int(brightness)
2022-04-14 17:03:50 +00:00
f = open('csv/general_settings.json', 'w')
2022-04-07 17:35:38 +00:00
json.dump(general_settings, f)
f.close()
2021-11-18 17:50:50 +00:00
return index()
2021-10-07 19:40:43 +00:00
2021-11-18 19:41:50 +00:00
def edit_wpa_sup(country, ssid, pwd):
current_wpa = open('/etc/wpa_supplicant/wpa_supplicant.conf')
wpa_lines = current_wpa.readlines()
wpa_lines[2] = 'country={}\n'.format(country)
current_wpa.close()
2021-12-15 09:05:53 +00:00
#remove this line to append to end instead of overwriting all networks
2021-12-16 14:41:39 +00:00
wpa_lines = wpa_lines[0:3]
2022-04-07 17:35:38 +00:00
2021-12-15 09:05:53 +00:00
2021-12-16 14:49:34 +00:00
# create new file from scratch
wpa_lines = []
2021-12-16 15:03:30 +00:00
wpa_lines.append('ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n')
wpa_lines.append('update_config=1\n')
2021-12-16 14:56:15 +00:00
wpa_lines.append('country={}\n'.format(country))
2021-12-16 14:49:34 +00:00
2021-11-18 19:41:50 +00:00
wpa_lines.append('\n')
wpa_lines.append('network={\n')
wpa_lines.append('\tssid="{}"\n'.format(ssid))
wpa_lines.append('\tpsk="{}"\n'.format(pwd))
wpa_lines.append('}\n')
wpa_string = ''.join(wpa_lines)
2022-04-07 17:35:38 +00:00
2022-04-14 17:03:50 +00:00
current_wpa = open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w')
2021-11-18 19:41:50 +00:00
current_wpa.write(wpa_string)
@app.route("/wifi", methods = ['PUT', 'POST', 'GET'])
2022-01-24 18:40:06 +00:00
def set_wifi():
2021-11-18 19:41:50 +00:00
data= request.data.decode('utf-8')
2022-04-07 17:35:38 +00:00
2021-11-18 19:41:50 +00:00
settings = json.loads(data)
country = settings['country'].upper()
ssid = settings['ssid']
pwd = settings['pwd']
2022-04-07 17:35:38 +00:00
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
2021-12-06 20:10:14 +00:00
general_settings['country_code'] = country
2022-04-14 17:03:50 +00:00
f = open('csv/general_settings.json', 'w')
2022-04-07 17:35:38 +00:00
json.dump(general_settings, f)
f.close()
2021-12-06 20:10:14 +00:00
2021-11-18 19:41:50 +00:00
edit_wpa_sup(country, ssid, pwd)
# resstart netoworking
os.system('wpa_cli -i wlan0 reconfigure')
return index()
2021-11-29 15:17:55 +00:00
2022-03-02 11:25:42 +00:00
#def check_internet_connection(host='http://google.com'):
2022-01-24 18:40:06 +00:00
2022-03-02 11:25:42 +00:00
#try:
#urllib.request.urlopen(host) #Python 3.x
#return True
#except:
#return False
2022-01-24 18:40:06 +00:00
2022-03-02 11:25:42 +00:00
#def check_network_connection():
#ps = subprocess.Popen(['iwconfig'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#try:
#output = subprocess.check_output(('grep', 'ESSID'), stdin=ps.stdout)
#return True
#except subprocess.CalledProcessError:
2022-01-24 18:40:06 +00:00
# grep did not match any lines
2022-04-07 17:35:38 +00:00
2022-03-02 11:25:42 +00:00
#return False
2022-01-24 18:40:06 +00:00
2021-11-29 15:17:55 +00:00
def edit_hosts(hostname):
current_hosts = open('/etc/hosts')
hosts_lines = current_hosts.readlines()
2022-04-07 17:35:38 +00:00
2021-11-29 15:17:55 +00:00
hosts_lines[5] = '127.0.1.1 {}'.format(hostname)
current_hosts.close()
hosts_string = ''.join(hosts_lines)
current_hosts = open('/etc/hosts', 'w+')
current_hosts.write(hosts_string)
2021-11-18 19:41:50 +00:00
2021-11-26 15:33:10 +00:00
@app.route("/hostname", methods = ['PUT', 'POST', 'GET'])
def hostname():
data= request.data.decode('utf-8')
settings = json.loads(data)
hostname = settings['hostname']
2022-04-07 17:35:38 +00:00
2021-11-29 15:17:55 +00:00
edit_hosts(hostname)
2021-11-26 15:33:10 +00:00
os.system("sudo hostnamectl set-hostname {}".format(hostname))
os.system("sudo systemctl restart avahi-daemon")
2021-11-18 19:41:50 +00:00
2022-04-07 17:35:38 +00:00
f = open('csv/general_settings.json', 'r')
general_settings = json.load(f)
f.close()
2021-12-06 20:10:14 +00:00
general_settings['hostname'] = hostname
2022-04-14 17:03:50 +00:00
f = open('csv/general_settings.json', 'w')
2022-04-07 17:35:38 +00:00
json.dump(general_settings, f)
f.close()
2021-12-06 20:10:14 +00:00
2021-11-26 15:33:10 +00:00
return index()
@app.route("/saveWeatherAPIKey", methods = ['PUT', 'POST'])
def saveWeatherAPIKey():
2021-11-18 19:41:50 +00:00
data= request.data.decode('utf-8')
settings = json.loads(data)
key = settings['api_key']
2022-04-07 17:35:38 +00:00
with open('./api_keys.txt') as f:
lines = f.readlines()
if len(lines) == 1:
lines.append(str(key))
elif len(lines) == 2:
lines[1] = str(key)
2022-04-07 17:35:38 +00:00
with open('./api_keys.txt', 'w') as f:
for line in lines:
f.write(line)
return index()
2021-11-18 19:41:50 +00:00
2021-11-17 19:34:13 +00:00
@app.route("/screensaver", methods = ['PUT', 'POST'])
def screensaver():
global displaying_screensaver
global ticker
2022-03-05 14:03:11 +00:00
#global api_caller
2021-11-17 19:34:13 +00:00
global screensaver_p
2022-02-26 15:50:10 +00:00
2021-11-17 19:34:13 +00:00
data = str(request.data)
2021-11-18 17:50:50 +00:00
if displaying_screensaver:
screensaver_p.close()
2022-02-26 15:50:10 +00:00
else:
2022-03-05 14:03:11 +00:00
#api_caller.close()
2022-02-26 15:50:10 +00:00
ticker.close()
2021-12-06 20:10:14 +00:00
2021-11-17 19:34:13 +00:00
if "Pulsating Colors" in data:
2021-12-05 19:55:09 +00:00
screensaver_p = pexpect.spawn("sudo -E python3 ./rpi-rgb-led-matrix/bindings/python/samples/pulsing-colors.py --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=4 -r 32 --led-cols 64 -c 2 -P 1")
2021-11-17 19:34:13 +00:00
elif "Rotating Square" in data:
2021-12-05 19:55:09 +00:00
screensaver_p = pexpect.spawn("sudo -E python3 ./rpi-rgb-led-matrix/bindings/python/samples/rotating-block-generator.py --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=4 -r 32 --led-cols 64 -c 2 -P 1")
2021-11-17 19:34:13 +00:00
elif "Pulsating brightness" in data:
2021-12-05 19:55:09 +00:00
screensaver_p = pexpect.spawn("sudo -E python3 ./rpi-rgb-led-matrix/bindings/python/samples/pulsing-brightness.py --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=4 -r 32 --led-cols 64 -c 2 -P 1")
2021-11-17 19:34:13 +00:00
elif "Game of Life" in data:
screensaver_p = pexpect.spawn("sudo -E python3 game_of_life.py")
2021-12-06 20:10:14 +00:00
elif "Sleep" in data:
screensaver_p = DummyProcess()
else: #default in case user hasnt set one yet
screensaver_p = DummyProcess()
2021-12-06 20:10:14 +00:00
2021-11-17 19:34:13 +00:00
displaying_screensaver = True
return index()
2021-09-19 18:19:09 +00:00
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
2021-12-10 17:06:47 +00:00
new_settings = copy.deepcopy(current_settings)
new_settings[current_key] = {}
2021-09-19 18:19:09 +00:00
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:
2021-12-10 17:06:47 +00:00
new_settings[current_key][IS] = []
else:
new_settings[current_key][IS] = current_settings[current_key][IS]
2021-12-14 15:38:09 +00:00
2021-09-19 18:19:09 +00:00
2021-12-10 17:06:47 +00:00
return new_settings
2021-09-19 18:19:09 +00:00
def save_trade_settings(input_settings):
2022-03-05 14:03:11 +00:00
2021-09-19 18:19:09 +00:00
filename = input_settings['feature'].lower() + '_settings.json'
2022-03-05 14:03:11 +00:00
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
2022-04-07 17:35:38 +00:00
2022-03-05 14:03:11 +00:00
f.close()
2021-09-19 18:19:09 +00:00
current_settings['speed'] = input_settings['speed'].lower()
2022-03-07 16:47:07 +00:00
current_settings['speed2'] = input_settings['speed2'].lower()
2021-09-19 18:19:09 +00:00
current_settings['animation'] = input_settings['animation'].lower()
current_settings['percent'] = input_settings['percent']
current_settings['point'] = input_settings['point']
2021-09-23 19:44:14 +00:00
current_settings['logos'] = input_settings['logos']
2021-09-19 18:19:09 +00:00
current_settings['title'] = input_settings['title']
current_settings = combine_dict(current_settings, input_settings['symbols'], 'symbols')
2022-04-14 17:03:50 +00:00
f = open('csv/' + filename, 'w')
2022-03-05 14:03:11 +00:00
json.dump(current_settings, f)
f.close()
2022-03-06 09:29:42 +00:00
f = open('csv/last_updates.json', 'r')
last_updates = json.load(f)
f.close()
2022-03-06 18:58:27 +00:00
if any([current_settings['symbols'][k] == [] for k in input_settings['symbols']]):
last_updates[input_settings['feature'].lower()]['force'] = True
2022-03-05 15:35:56 +00:00
2022-03-06 18:58:27 +00:00
2022-03-06 09:29:42 +00:00
2022-04-14 17:03:50 +00:00
f = open('csv/last_updates.json', 'w')
2022-03-06 09:29:42 +00:00
json.dump(last_updates, f)
f.close()
2022-03-05 15:35:56 +00:00
2021-11-16 19:11:32 +00:00
2021-09-19 18:19:09 +00:00
def save_weather_settings(input_settings):
2021-12-04 15:54:31 +00:00
2022-04-07 17:35:38 +00:00
2021-09-19 18:19:09 +00:00
filename = 'current_weather.json' if input_settings['feature'] == 'Current Weather' else 'daily_weather.json'
2022-04-07 17:35:38 +00:00
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
2021-09-19 18:19:09 +00:00
current_settings['speed'] = input_settings['speed'].lower()
2022-03-07 16:47:07 +00:00
current_settings['speed2'] = input_settings['speed2'].lower()
2021-09-19 18:19:09 +00:00
current_settings['animation'] = input_settings['animation'].lower()
current_settings['temp'] = input_settings['temp'].lower()
current_settings['wind_speed'] = input_settings['wind_speed'].lower()
2021-12-22 09:43:36 +00:00
2021-09-19 18:19:09 +00:00
current_settings['title'] = input_settings['title']
if input_settings['feature'] == 'Daily Forecast':
current_settings['current_weather'] = input_settings['current_weather']
2021-09-16 19:23:04 +00:00
2021-12-14 15:38:09 +00:00
'''
2021-12-04 15:54:31 +00:00
locations = {}
for key in input_settings['locations']:
locations[key] = []
current_settings['locations'] = locations
2021-12-14 15:38:09 +00:00
'''
current_settings = combine_dict(current_settings, input_settings['locations'], 'locations')
2022-04-07 17:35:38 +00:00
f = open('csv/' + filename, 'w+')
json.dump(current_settings, f)
f.close()
2021-09-19 18:19:09 +00:00
2022-03-06 09:29:42 +00:00
#api_caller.sendline('w')
f = open('csv/last_updates.json', 'r')
last_updates = json.load(f)
f.close()
2022-03-06 18:58:27 +00:00
if any([current_settings['locations'][k] == [] for k in input_settings['locations']]):
last_updates['weather']['force'] = True
2022-03-06 09:29:42 +00:00
2022-04-14 17:03:50 +00:00
f = open('csv/last_updates.json', 'w')
2022-03-06 09:29:42 +00:00
json.dump(last_updates, f)
f.close()
2021-11-16 19:11:32 +00:00
2021-09-19 18:19:09 +00:00
def save_news_settings(input_settings):
filename = 'news_settings.json'
2022-04-07 17:35:38 +00:00
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
2021-09-19 18:19:09 +00:00
2022-04-07 17:35:38 +00:00
2022-01-24 18:40:06 +00:00
2021-09-19 18:19:09 +00:00
current_settings['speed'] = input_settings['speed'].lower()
2022-03-07 16:47:07 +00:00
current_settings['speed2'] = input_settings['speed2'].lower()
2021-09-19 18:19:09 +00:00
current_settings['animation'] = input_settings['animation'].lower()
current_settings['title'] = input_settings['title']
2022-02-24 19:42:33 +00:00
current_settings['category'] = input_settings['category']
2021-09-19 18:19:09 +00:00
current_settings['country'] = input_settings['country']
2022-02-24 19:42:33 +00:00
current_settings['use_category'] = input_settings['use_category']
current_settings['use_country'] = input_settings['use_country']
current_settings['num_headlines'] = input_settings['num_headlines']
2021-09-19 18:19:09 +00:00
2022-02-24 19:42:33 +00:00
2022-04-07 17:35:38 +00:00
2022-04-14 17:03:50 +00:00
f = open('csv/' + filename, 'w')
2022-04-07 17:35:38 +00:00
json.dump(current_settings, f)
f.close()
2022-03-06 09:29:42 +00:00
#api_caller.sendline('n')
f = open('csv/last_updates.json', 'r')
last_updates = json.load(f)
f.close()
last_updates['news']['force'] = True
2022-04-14 17:03:50 +00:00
f = open('csv/last_updates.json', 'w')
2022-03-06 09:29:42 +00:00
json.dump(last_updates, f)
f.close()
2021-09-19 18:19:09 +00:00
def save_sports_settings(input_settings):
feature = input_settings['feature']
if feature == 'Sports (Upcoming Games)':
filename = 'upcoming_games.json'
2022-03-07 17:34:24 +00:00
update_key = 'sports_u'
2021-09-19 18:19:09 +00:00
elif feature == 'Sports (Past Games)':
filename = 'past_games.json'
2022-03-07 17:34:24 +00:00
update_key = 'sports_p'
2021-09-19 18:19:09 +00:00
elif feature == 'Sports (Live Games)':
filename = 'live_games.json'
2022-03-07 17:34:24 +00:00
update_key = 'sports_l'
2021-09-19 18:19:09 +00:00
elif feature == 'Sports (Team Stats)':
filename = 'league_tables.json'
2022-03-07 17:34:24 +00:00
update_key = 'sports_t'
2022-04-07 17:35:38 +00:00
f = open('csv/' + filename, 'r')
current_settings = json.load(f)
f.close()
2021-09-19 18:19:09 +00:00
current_settings['speed'] = input_settings['speed'].lower()
2022-03-07 16:47:07 +00:00
current_settings['speed2'] = input_settings['speed2'].lower()
2021-09-19 18:19:09 +00:00
current_settings['animation'] = input_settings['animation'].lower()
current_settings['title'] = input_settings['title']
current_settings['feature'] = input_settings['feature']
2021-09-19 18:19:09 +00:00
current_settings = combine_dict(current_settings, input_settings['leagues'], 'leagues')
2022-04-14 17:03:50 +00:00
f = open('csv/' + filename, 'w')
2022-04-07 17:35:38 +00:00
json.dump(current_settings, f)
f.close()
2021-09-19 18:19:09 +00:00
2022-03-06 09:29:42 +00:00
#api_caller.sendline('S')
f = open('csv/last_updates.json', 'r')
last_updates = json.load(f)
f.close()
2022-03-06 18:58:27 +00:00
if any([current_settings['leagues'][k] == [] for k in input_settings['leagues']]):
2022-03-07 17:34:24 +00:00
last_updates[update_key]['force'] = True
2022-03-06 09:29:42 +00:00
2022-04-14 17:03:50 +00:00
f = open('csv/last_updates.json', 'w')
2022-03-06 09:29:42 +00:00
json.dump(last_updates, f)
f.close()
2021-11-16 19:11:32 +00:00
2021-09-19 18:19:09 +00:00
# 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()
2022-03-07 16:47:07 +00:00
current_settings['speed2'] = input_settings['speed2'].lower()
2021-09-19 18:19:09 +00:00
current_settings['animation'] = input_settings['animation'].lower()
2021-12-08 17:26:35 +00:00
2021-09-19 18:19:09 +00:00
del current_settings['feature']
2022-04-14 17:03:50 +00:00
f = open('csv/' + filename, 'w')
2022-04-07 17:35:38 +00:00
json.dump(current_settings, f)
f.close()
2021-12-22 09:43:36 +00:00
remove_old_uploads()
2021-12-15 11:06:54 +00:00
def save_message_settings(input_settings):
2022-04-07 17:35:38 +00:00
f = open('csv/message_settings.json', 'r')
current_settings = json.load(f)
f.close()
2021-12-15 11:06:54 +00:00
new_settings = copy.deepcopy(input_settings)
2022-04-07 17:35:38 +00:00
2021-12-15 11:06:54 +00:00
for i,IS in enumerate(input_settings['messages']):
# check if this is in current_settings
for CS in current_settings['messages']:
if IS['name'] == CS['name']:
new_settings['messages'][i] = CS
2022-04-07 17:35:38 +00:00
2021-12-15 11:06:54 +00:00
break
2022-04-14 17:03:50 +00:00
f = open('csv/message_settings.json', 'w')
2022-04-07 17:35:38 +00:00
json.dump(new_settings, f)
f.close()
2021-11-18 17:50:50 +00:00
@app.route("/shutdown")
def shutdown():
os.system("sudo shutdown now")
return index()
2021-11-26 15:33:10 +00:00
2021-07-06 19:15:05 +00:00
2021-04-26 18:51:21 +00:00
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1024, debug=False) # the debuggger causes flickering
#sudo ./demo -D1 final.ppm -t 50 -m 25 --led-gpio-mapping=adafruit-hat --led-rows=32 --led-cols=256 --led-slowdown-gpio=4