Server to communicate

This commit is contained in:
2024-04-08 15:41:39 +02:00
parent e93eef28b3
commit 730845e01c
+48 -4
View File
@@ -43,6 +43,11 @@ import yt_dlp
from spotify_dl import spotify
from spotify_dl import youtube as youtube_download
from flask import Flask, jsonify, request
from waitress import serve
COMM_Q = Queue()
Music_Config = TypedDict(
"Music_Config",
{
@@ -51,6 +56,15 @@ Music_Config = TypedDict(
"requester": List[str],
},
)
class QueryControl():
def __init__(self, query_author, query_uuid, query_content, uplogger) -> None:
self.author = query_author
self.uuid = query_uuid
self.content = query_content
self.logger = uplogger
self.logger.info(f"Created Query control for {self.author}, {self.uuid}: {self.content}")
self.replies = []
# *=========================================== Predefines
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
@@ -78,6 +92,8 @@ LIBRARIAN_SERVICE_ADDRESS = "http://192.168.1.192:5001"
SEND_QUERY = "/query"
TIME_BETWEEN_CALLS = 100000
LAST_SPONTANEOUS_CALL = datetime.now()
HOST_ADDRESS = "http://192.168.1.192"
PORT_ADDRESS = "5000"
# *=========================================== Platform Specific Predefines
@@ -266,9 +282,10 @@ logger.info("Done")
logger.info("Creating discord bot")
client = commands.Bot(intents=intents, command_prefix="$")
logger.info("Done")
logger.info("Creating flask app")
app = Flask(__name__)
logger.info("Done")
# *=========================================== Define Events
@client.event
async def on_ready():
"""Metoda wywoływana przy połączeniu do serwera."""
@@ -2089,6 +2106,34 @@ async def krecimy_pornola(ctx):
else:
ctx.reply("Jak ładnie szefa poprosisz.")
@app.route("/", methods=["POST"])
def answer_external_command():
record = json.loads(request.data)
logger.info(record)
answer_data = None
if "UUID" in record and "QUERY" in record and "AUTHOR" in record:
answer_data = "Query logged"
return_data = (
jsonify(isError=False, message="Success", statusCode=200, data=answer_data),
200,
)
return return_data
def flask_debug():
"""
The `flask_debug` function starts a Flask application in debug mode without using the reloader.
Do not use for production for fucks sake.
"""
# trunk-ignore(bandit/B201)
app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS)
def waitress_run():
"""
The `waitress_run` function serves the `app` on host "0.0.0.0"
and port 5000 using the Waitress WSGI server.
"""
serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS)
# *================================== Run
@@ -2096,8 +2141,7 @@ if __name__ == "__main__":
#TODO: wpakowac kondzia w wąteki odpalic asynchronicznie rownolegle z flaskiem do sterowania nim
logger.info("Starting discord bot")
threads = []
comm_q = Queue()
t_bot = threads.append(threading.Thread(target=client.run, args=(TOKEN,)))
threads.append(threading.Thread(target=client.run, args=(TOKEN,)))
for worker in threads:
worker.start()
for worker in threads: