From d35c6539afb55c8f35dd4d57455dc55a356d2ba0 Mon Sep 17 00:00:00 2001 From: Migatu Date: Sat, 30 Mar 2024 17:23:05 +0100 Subject: [PATCH] Installer --- bot.py | 8 ++++ cut_scihub_database.py | 15 +++++++ file_webservice/conjurer_musician.py | 43 ++++++++++++++++++++- file_webservice/install_musician_service.sh | 11 ++++++ file_webservice/requirements_musician.txt | 2 + install.sh => install_main_bot.sh | 7 +++- requirements.txt => requirements_bot.txt | 0 7 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 file_webservice/install_musician_service.sh create mode 100644 file_webservice/requirements_musician.txt rename install.sh => install_main_bot.sh (57%) rename requirements.txt => requirements_bot.txt (100%) diff --git a/bot.py b/bot.py index bbc2d43..3f34fc2 100644 --- a/bot.py +++ b/bot.py @@ -203,6 +203,14 @@ class MusicFileList(object): """ return self.music_file_list def update_file_list(self,item): + """ + The `update_file_list` function appends an item to a music file list and sends a POST request with + the item data to a file service address. + + :param item: The `item` parameter in the `update_file_list` method is the file that you want to add + to the `music_file_list` attribute of the class instance. It is then sent as a JSON payload in a + POST request to a file service address along with the endpoint `SEND_MP3` + """ self.music_file_list.append(item) post_data = {"item" : str(item)} requests.post(f'{FILE_SERVICE_ADDRESS}{SEND_MP3}', json=post_data, timeout = 360) diff --git a/cut_scihub_database.py b/cut_scihub_database.py index 45b772f..bb41bda 100644 --- a/cut_scihub_database.py +++ b/cut_scihub_database.py @@ -1,4 +1,19 @@ # trunk-ignore-all(pylint/C0301) +""" +The function `cut_my_life_into_pieces` reads a source file, splits its content based on a file size +limit, and writes the parts to separate output files in a specified folder. + +:param lines: The `lines` parameter in the `smart_write` function is a string that contains the text +content that you want to write to a file. It represents the actual content that will be written to +the file +:param file: The code you provided defines two functions: `smart_write` and +`cut_my_life_into_pieces`. The `smart_write` function encodes input lines to UTF-8, writes them to a +file, and returns the size of the encoded bytes. The `cut_my_life_into_pieces` function reads +:return: The functions provided in the code snippet do not explicitly return any values. However, +the `smart_write` function returns the size of the encoded bytes of the input `lines` in bytes. The +`cut_my_life_into_pieces` function does not have a return statement, so it does not explicitly +return any value. +""" FILE_SIZE_LIMIT = 100000 * 1024 SOURCE = "/mnt/n/scimag_2020-05-30.sql/scimag_2020-05-30.sql" diff --git a/file_webservice/conjurer_musician.py b/file_webservice/conjurer_musician.py index 150415f..25abc47 100644 --- a/file_webservice/conjurer_musician.py +++ b/file_webservice/conjurer_musician.py @@ -1,3 +1,8 @@ +""" +The provided Python script sets up a Flask web server to manage a list of music files, with +functions for rescanning the music folder, updating the music list, and serving the music list via +API endpoints. +""" import json from pathlib import Path from platform import uname @@ -8,6 +13,9 @@ import logging from flask import Flask, jsonify, request from waitress import serve + +HOST_ADDRESS = "192.168.1.15" +PORT_ADDRESS = 5000 if platform in ("linux", "linux2"): SEPARATOR_FILE_PATH = "/" if "microsoft-standard" in uname().release: @@ -35,6 +43,10 @@ if platform in ("linux", "linux2"): music_file_list = [] def rescan(): + """ + The `rescan` function logs a message, scans for mp3 files in a specified folder, + and adds them to a list of music files. + """ logging.info("Rescan triggered") for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"): temp_music_file = mp3_item.as_posix() @@ -43,6 +55,10 @@ def rescan(): music_file_list.append(temp_music_file) def thread_rescan(): + """ + The `thread_rescan` function periodically triggers a rescan operation after a specified time + interval. + """ logging.info("Starting filesystemupdater") while True: time.sleep(60*60*24) @@ -53,11 +69,25 @@ 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": music_file_list}) @app.route("/update_mp3", methods=["POST"]) def update_music_list(): + """ + The function `update_music_list` receives a POST request with a JSON payload, logs the received + item, adds it to a music file list, and returns a success message along with the updated record. + :return: The function `update_music_list` is returning a tuple containing a JSON response and a + status code. The JSON response includes keys `isError`, `message`, `statusCode`, and `data`, + with values indicating the success of the operation and the data that was + received and added to the `music_file_list`. + The status code returned is 200, indicating a successful response. + """ record = json.loads(request.data) app.logger.info(record["item"]) music_file_list.append(record["item"]) @@ -68,10 +98,19 @@ def update_music_list(): return return_data def flask_debug(): - app.run(debug=True, use_reloader=False, host='0.0.0.0', port=5000) + """ + 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(): - serve(app, host="0.0.0.0", port=5000) + """ + 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) if __name__ == "__main__": logger = logging.getLogger('conjurer_musician') diff --git a/file_webservice/install_musician_service.sh b/file_webservice/install_musician_service.sh new file mode 100644 index 0000000..3b50dfa --- /dev/null +++ b/file_webservice/install_musician_service.sh @@ -0,0 +1,11 @@ +#!/bin/bash +python3 -m venv /home/pi/Conjurer-service/conjurer/file_webservice +source ./bin/activate +./bin/python3 -m pip install --upgrade pip +./bin/python3 -m pip install -r requirements_musician.txt +deactivate + +sudo cp ./conjurer_musician.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl start conjurer_musician.service +sudo systemctl enable conjurer_musician.service diff --git a/file_webservice/requirements_musician.txt b/file_webservice/requirements_musician.txt new file mode 100644 index 0000000..8642e2b --- /dev/null +++ b/file_webservice/requirements_musician.txt @@ -0,0 +1,2 @@ +flask +waitress \ No newline at end of file diff --git a/install.sh b/install_main_bot.sh similarity index 57% rename from install.sh rename to install_main_bot.sh index 897b743..7ec8c71 100755 --- a/install.sh +++ b/install_main_bot.sh @@ -2,8 +2,11 @@ python3 -m venv /home/pi/Conjurer/env source ./env/bin/activate ./env/bin/python3 -m pip install --upgrade pip -./env/bin/python3 -m pip install -r requirements.txt +./env/bin/python3 -m pip install -r requirements_bot.txt sed -i -e 's/os.rename/shutil.copy/g' ./env/lib/python3.11/site-packages/spotify_dl/youtube.py sed -i '1i\import shutil' ./env/lib/python3.11/site-packages/spotify_dl/youtube.py deactivate -sudo systemctl restart conjurer.service +sudo cp ./conjurer.service /etc/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl start conjurer_musician.service +sudo systemctl enable conjurer_musician.service diff --git a/requirements.txt b/requirements_bot.txt similarity index 100% rename from requirements.txt rename to requirements_bot.txt