From e186c81be75777e1d4786b9ddbcb794595280aa3 Mon Sep 17 00:00:00 2001 From: Migatu Date: Tue, 9 Apr 2024 00:53:50 +0200 Subject: [PATCH] test --- bot.py | 50 ++--------------------------- communication_subroutine.py | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 communication_subroutine.py diff --git a/bot.py b/bot.py index eb4c05b..1075378 100644 --- a/bot.py +++ b/bot.py @@ -38,9 +38,7 @@ import requests import spotipy import tiktoken from discord.ext import commands, tasks -from flask import Flask, jsonify, request from spotipy.oauth2 import SpotifyClientCredentials -from waitress import serve import yt_dlp from spotify_dl import spotify @@ -2212,59 +2210,15 @@ async def krecimy_pornola(ctx): else: ctx.reply("Jak ładnie szefa poprosisz.") -app = Flask(__name__) -@app.route("/mp3", methods=["GET"]) -def get_music_list(): - """ - The function `get_music_list` returns a JSON object containing a list of music files. - :return: A JSON response containing a key "music_file_list" with the value of the variable - `music_file_list`. - """ - return jsonify({"music_file_list": MASTER_TIMEOUT}) -@app.route("/conjurer", methods=["GET"]) -def answer_external_command(): - #record = json.loads(request.data) - #logger.info(record) - #answer_data = "TXT" - #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 - return jsonify("SUCCESS") - -def flask_debug(_logger): - """ - 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) - _logger.info("Attempt debug") - app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS, logger=_logger) - - -def waitress_run(_logger): - """ - The `waitress_run` function serves the `app` on host "0.0.0.0" - and port 5000 using the Waitress WSGI server. - """ - _logger.info("Attempt waitress") - serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS, logger=_logger) -t2 = threading.Thread(target=flask_debug, args=(logger,)) -t2.start() -t2.join() # *================================== Run if __name__ == "__main__": # TODO: wpakowac kondzia w wątki odpalic asynchronicznie rownolegle z flaskiem do sterowania nim logger.info("Starting discord bot") threads = [] - t1 = threading.Thread(target=client.run, args=(TOKEN,)) - t1.start() - t1.join() + threads.append(threading.Thread(target=client.run, args=(TOKEN,))) + # widok - generuje się 5 stron po 4 linki # odśwież - zamienić na "dalej" - zaciąga kolejne 20 rezultatów diff --git a/communication_subroutine.py b/communication_subroutine.py new file mode 100644 index 0000000..dc06483 --- /dev/null +++ b/communication_subroutine.py @@ -0,0 +1,64 @@ +import logging +import threading + +from flask import Flask, jsonify, request +from waitress import serve + + + +app = Flask(__name__) +@app.route("/mp3", methods=["GET"]) +def get_music_list(): + """ + The function `get_music_list` returns a JSON object containing a list of music files. + :return: A JSON response containing a key "music_file_list" with the value of the variable + `music_file_list`. + """ + return jsonify({"music_file_list": MASTER_TIMEOUT}) + +@app.route("/conjurer", methods=["GET"]) +def answer_external_command(): + #record = json.loads(request.data) + #logger.info(record) + #answer_data = "TXT" + #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 + return jsonify("SUCCESS") + + +def flask_debug(_logger): + """ + 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) + _logger.info("Attempt debug") + app.run(debug=True, use_reloader=False, host=HOST_ADDRESS, port=PORT_ADDRESS, logger=_logger) + + +def waitress_run(_logger): + """ + The `waitress_run` function serves the `app` on host "0.0.0.0" + and port 5000 using the Waitress WSGI server. + """ + _logger.info("Attempt waitress") + serve(app, host=HOST_ADDRESS, port=PORT_ADDRESS, logger=_logger) + +if __name__ == "__main__": + logger = logging.getLogger('conjurer_musician') + logger.setLevel(logging.DEBUG) + logger.info("Started") + threads = [] + threads.append(threading.Thread(target=waitress_run)) + #threads.append(threading.Thread(target=thread_rescan)) + + for worker in threads: + worker.start() + + for worker in threads: + worker.join()