From d94163e246cc569f96fc4270aa344647f84c5d59 Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 18 Jun 2024 10:00:00 -0700 Subject: [PATCH 1/4] the start of the webui stuff --- .env_default | 15 ++++++++++ flask_templates/index.html | 61 ++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +- sparkytron_webui.py | 24 +++++++++++++++ src/bot.py | 4 +-- src/webui.py | 45 ++++++++++++++++++++++++++++ 6 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 .env_default create mode 100644 flask_templates/index.html create mode 100644 sparkytron_webui.py create mode 100644 src/webui.py diff --git a/.env_default b/.env_default new file mode 100644 index 0000000..c3c92fb --- /dev/null +++ b/.env_default @@ -0,0 +1,15 @@ +discord_token='token' +flask_port='5000' +imgflip_username='username' +imgflip_password='password' +openai.api_key='api_key' +upload_phixxy='False' +ftp_server='www.example.com' +ftp_username='username' +ftp_password='password' +ftp_public_html='/home/debian/www.example.com/' +stable_diffusion_ip='disabled' +stable_diffusion_port='7861' +stable_diffusion_user= +stable_diffusion_password= +eleven_labs='api-key' diff --git a/flask_templates/index.html b/flask_templates/index.html new file mode 100644 index 0000000..66f14f1 --- /dev/null +++ b/flask_templates/index.html @@ -0,0 +1,61 @@ + + + + + + + Input Form + + +

Warning!

+

This information is stored in PLAIN TEXT in a .env file!

+
+ {% for key, value in key_value_pairs.items() %} + + +
+ {% endfor %} + +
+ + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c94a3fc..1312dad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,5 @@ psutil aiofiles inky wakeonlan -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +Flask[async] \ No newline at end of file diff --git a/sparkytron_webui.py b/sparkytron_webui.py new file mode 100644 index 0000000..cd0552c --- /dev/null +++ b/sparkytron_webui.py @@ -0,0 +1,24 @@ +import asyncio +import discord +import os +import subprocess +from dotenv import load_dotenv +from src.bot import bot +from src.webui import flask_app + + +def run_flask_app(process): + flask_port = os.getenv("flask_port") + if not flask_port: + flask_port = '5000' + flask_app.bot_process = process + flask_app.run(debug=True, use_reloader=False ,host='0.0.0.0', port=flask_port) + +def main(): + load_dotenv() + process = subprocess.Popen(["python", "sparkytron3000.py"]) + run_flask_app(process) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/bot.py b/src/bot.py index 8c00b66..0160b86 100644 --- a/src/bot.py +++ b/src/bot.py @@ -10,7 +10,6 @@ intents.message_content = True bot = commands.Bot(command_prefix='!', intents=intents) logger = src.logger.logger_setup() - async def load_cogs(bot: commands.Bot, cog_path: str) -> None: for cog_file in os.listdir(cog_path): if cog_file[-3:] == '.py': @@ -27,6 +26,7 @@ async def on_ready(): await utils.delete_all_files("tmp/") await load_cogs(bot, 'cogs/') logger.info('We have logged in as {0.user}'.format(bot)) + print("Visit http://localhost:5000 to change config!") except: logger.warning(f"Error in on_ready") @@ -43,4 +43,4 @@ async def on_message(ctx): except discord.ext.commands.errors.CommandNotFound: logger.info("Command not found.") except Exception as e: - logger.warning(f"Error processing commands: {e}") + logger.warning(f"Error processing commands: {e}") \ No newline at end of file diff --git a/src/webui.py b/src/webui.py new file mode 100644 index 0000000..5024349 --- /dev/null +++ b/src/webui.py @@ -0,0 +1,45 @@ +import logging +import os +import subprocess + +from flask import Flask, render_template, request + +logger = logging.getLogger("bot") +flask_app = Flask(__name__, template_folder='../flask_templates') + +def read_env(filename): + if os.path.exists(filename): + with open(filename, 'r') as file: + key_value_pairs = {} + for line in file: + try: + key, value = line.strip().split('=') + key = key.strip() + value = value.strip()[1:-1] + key_value_pairs[key] = value + except: + print("This line isnt a kv pair") + return key_value_pairs + else: + return None + +@flask_app.route('/', methods=['GET', 'POST']) +async def index(): + key_value_pairs = read_env('.env') + if not key_value_pairs: + logger.warn("No .env file found! Copying defaults.") + key_value_pairs = read_env('.env_default') + form_dict = {} + if request.method == 'POST': + if key_value_pairs: + for form_name in key_value_pairs.keys(): + form_dict[form_name] = request.form[form_name] + with open('.env', 'w') as file: + for key, value in form_dict.items(): + file.write(f"{key}='{value}'\n") + print(form_dict) + flask_app.bot_process.terminate() + flask_app.bot_process = subprocess.Popen(["python", "sparkytron3000.py"]) + return 'Your input has been saved! The bot must be restarted for the changes to take effect.' + return render_template('index.html', key_value_pairs = key_value_pairs) + From 5cd502b19c6d324caba93858d0e0936a9b5983e1 Mon Sep 17 00:00:00 2001 From: phixxy Date: Tue, 18 Jun 2024 22:59:30 -0700 Subject: [PATCH 2/4] webui progress/added alerts --- flask_templates/index.html | 12 ++++++++++++ sparkytron_webui.py | 6 +++++- src/bot.py | 2 +- src/webui.py | 8 +++++--- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/flask_templates/index.html b/flask_templates/index.html index 66f14f1..ea93c9c 100644 --- a/flask_templates/index.html +++ b/flask_templates/index.html @@ -1,4 +1,6 @@ + + - Input Form + Sparkytron Config + -

Warning!

-

This information is stored in PLAIN TEXT in a .env file!

-
- {% for key, value in key_value_pairs.items() %} - - -
- {% endfor %} - - {% with messages = get_flashed_messages() %} - {% if messages %} - {% for message in messages %} -
- {{ message }} - +
+

Warning!

+

This information is stored in PLAIN TEXT in a .env file!

+ + {% for key, value in key_value_pairs.items() %} +
+ +
+ +
- {% endfor %} - {% endif %} - {% endwith %} - + {% endfor %} + + + + {% with messages = get_flashed_messages() %} + {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %} + {% endwith %} + +
\ No newline at end of file From b14debfb37930eea783ccec9ee8b24606364c697 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 19 Jun 2024 20:59:29 -0700 Subject: [PATCH 4/4] Good enough to merge! --- requirements.txt | 3 ++- sparkytron_webui.py | 20 +++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1312dad..6645a76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ aiofiles inky wakeonlan beautifulsoup4 -Flask[async] \ No newline at end of file +Flask[async] +waitress \ No newline at end of file diff --git a/sparkytron_webui.py b/sparkytron_webui.py index 28ef62f..bb75e1e 100644 --- a/sparkytron_webui.py +++ b/sparkytron_webui.py @@ -6,23 +6,21 @@ import sys from dotenv import load_dotenv from src.bot import bot from src.webui import flask_app +from waitress import serve - -def run_flask_app(process): - flask_port = os.getenv("flask_port") - if not flask_port: - flask_port = '5000' +def get_flask_app(process): flask_app.bot_process = process flask_app.secret_key = "woaoaoahaowhawoiahoahhhhhh" - flask_app.run(debug=True, use_reloader=False ,host='0.0.0.0', port=flask_port) - - + return flask_app def main(): load_dotenv() + flask_port = os.getenv("flask_port") + if not flask_port: + flask_port = '5000' process = subprocess.Popen([sys.executable, "sparkytron3000.py"]) - run_flask_app(process) - - + flask_app = get_flask_app(process) + serve(flask_app, host='0.0.0.0', port=flask_port) + if __name__ == "__main__": main() \ No newline at end of file