diff --git a/bot.py b/bot.py index 1febdda..a372450 100644 --- a/bot.py +++ b/bot.py @@ -72,6 +72,7 @@ MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30 FILE_SERVICE_ADDRESS = "http://192.168.1.15:5000" GET_MP3 = "/mp3" SEND_MP3 = "/update_mp3" +GET_PLAYLIST = "/get_music" LIBRARIAN_SERVICE_ADDRESS = "http://192.168.1.191:5001" SEND_QUERY = "/query" TIME_BETWEEN_CALLS = 10000 @@ -1181,100 +1182,19 @@ async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None): It is an optional parameter with a default value of 0, which means that if no value is provided for how_many, the function will return all search results, defaults to 0 (optional) """ - #TODO: Przerobić na webservice # i sieć neuronową z możliwością wyrażenia opinii o dopasowaniu. if slowa_kluczowe: word_list = slowa_kluczowe else: word_list = ctx.message.content.split() logger.info("Wyszukuje") - search_weight = [] - for _ in range(len(music_file_list.get_file_list())): - search_weight.append((0, "")) - - time_start = datetime.now() - if int(how_many) > 0: - skip_start = 2 - else: - skip_start = 1 - async for word in async_iterator_generator(word_list[skip_start:]): - token_weight = len(word) - logger.info("Słowo kluczowe: %s", word) - itr = 0 - async for file in async_iterator_generator(music_file_list.get_file_list()): - file = file.split(SEPARATOR_FILE_PATH) - - char_remove = [ - ".", - "^", - "$", - "*", - "+", - "?", - "{", - "}", - "[", - "]", - "\\", - "/", - "|", - "(", - ")", - "!", - ",", - "-", - ":", - "mp3", - ] - all_words = [] - async for f_iter in async_iterator_generator(file): - async for char in async_iterator_generator(char_remove): - f_iter = await remove_characters(f_iter, char) - tmp = f_iter.split() - all_words.extend(tmp) - pingu = 1 - pattern_len = len(all_words) - if platform == "win32": - skip = 2 - else: - skip = 4 - matched_times = 1 - async for itm in async_iterator_generator(all_words[skip:]): - pingu += 1 - pattern = ".*" + word + ".*" - if re.match(pattern, itm, re.IGNORECASE): - temp_weight = ( - search_weight[itr][0] - + (token_weight + (pingu**1.5) / pattern_len) / matched_times - ) - search_weight[itr] = ( - temp_weight, - music_file_list.get_file_list()[itr], - ) - matched_times += 1 - itr += 1 - - logger.info("Stworzylem tablice wag zajęło mi to %s", datetime.now() - time_start) - time_start = datetime.now() - item_to_search = await max_weight(search_weight) - itr = 0 - if item_to_search == 0: - return None - not_found = True - return_list = [] - if int(how_many) <= 0: - logger.info("Jeden plik do zagrania") - while not_found: - if search_weight[itr][0] == item_to_search: - return_list.append(search_weight[itr]) - return return_list - itr += 1 - else: - logger.info("Wiele plików do zagrania") - search_weight.sort(key=lambda x: x[0], reverse=True) - return_list.extend(search_weight[: int(how_many)]) - return return_list - + jrequest = {"lista_slow" : word_list, "dlugosc_plejlisty" : how_many, "UUID": uuid.uuid4()} + coroutine = asyncio.to_thread(requests.post, f"{FILE_SERVICE_ADDRESS}{GET_PLAYLIST}", json=jrequest, timeout=360) + return_data = await coroutine + if not return_data.status_code == 200: + await ctx.send("Wołaj szefa - coś się wyjebało") + return + return return_data.json()["data"] async def remove_characters(string, character): """ diff --git a/file_webservice/conjurer_musician.py b/file_webservice/conjurer_musician.py index c8333fb..fc26d0e 100644 --- a/file_webservice/conjurer_musician.py +++ b/file_webservice/conjurer_musician.py @@ -4,10 +4,12 @@ functions for rescanning the music folder, updating the music list, and serving API endpoints. """ import json +import re from pathlib import Path from platform import uname import threading import time +from datetime import datetime from sys import platform import logging from flask import Flask, jsonify, request @@ -67,6 +69,125 @@ def thread_rescan(): app = Flask(__name__) +def wyszukaj(word_list, how_many): + logger = logging.getLogger() + return_list = [] + search_weight = [] + + for _ in range(len(music_file_list)): + search_weight.append((0, "")) + + time_start = datetime.now() + if int(how_many) > 0: + skip_start = 2 + else: + skip_start = 1 + for word in word_list[skip_start:]: + token_weight = len(word) + logger.info("Słowo kluczowe: %s", word) + itr = 0 + for file in music_file_list: + file = file.split(SEPARATOR_FILE_PATH) + + char_remove = [ + ".", + "^", + "$", + "*", + "+", + "?", + "{", + "}", + "[", + "]", + "\\", + "/", + "|", + "(", + ")", + "!", + ",", + "-", + ":", + "mp3", + ] + all_words = [] + for f_iter in file: + for char in char_remove: + f_iter = remove_characters(f_iter, char) + tmp = f_iter.split() + all_words.extend(tmp) + pingu = 1 + pattern_len = len(all_words) + if platform == "win32": + skip = 2 + else: + skip = 4 + matched_times = 1 + for itm in all_words[skip:]: + pingu += 1 + pattern = ".*" + word + ".*" + if re.match(pattern, itm, re.IGNORECASE): + temp_weight = ( + search_weight[itr][0] + + (token_weight + (pingu**1.5) / pattern_len) / matched_times + ) + search_weight[itr] = ( + temp_weight, + music_file_list[itr], + ) + matched_times += 1 + itr += 1 + + logger.info("Stworzylem tablice wag zajęło mi to %s", datetime.now() - time_start) + time_start = datetime.now() + item_to_search = max_weight(search_weight) + itr = 0 + if item_to_search == 0: + return None + not_found = True + return_list = [] + if int(how_many) <= 0: + logger.info("Jeden plik do zagrania") + while not_found: + if search_weight[itr][0] == item_to_search: + return_list.append(search_weight[itr]) + return return_list + itr += 1 + else: + logger.info("Wiele plików do zagrania") + search_weight.sort(key=lambda x: x[0], reverse=True) + return_list.extend(search_weight[: int(how_many)]) + return return_list + +def max_weight(lista): + """ + Take a list of weights and return the maximum weight. + + :param lista: It seems like the parameter `lista` is a list of items, possibly representing weights. + The function name `max_weight` suggests that the function is intended to find the maximum weight + from the list. However, without more context or information about the problem, it's difficult to say + for sure what the + """ + maximum_weight = 0 + for iterator in lista: + if iterator[0] > maximum_weight: + maximum_weight = iterator[0] + return maximum_weight + + +def remove_characters(string, character): + """ + The `remove_characters` function removes all occurrences of a specified character from a given + string. + + :param string: The string parameter is the input string from which characters will be removed + :param character: The character parameter is the character that you want to remove from the string + :return: a new string where all occurrences of the specified character have been removed. + """ + return string.replace(character, "") + + @app.route("/mp3", methods=["GET"]) def get_music_list(): """ @@ -97,6 +218,33 @@ def update_music_list(): ) return return_data +@app.route("/get_music", methods=["POST"]) +def look_for_playlist(): + """ + 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) + app.logger.info(record["word_list"]) + app.logger.info(record["UUID"]) + app.logger.info(record["dlugosc_plejlisty"]) + return_data = wyszukaj(record["word_list"],record["dlugosc_plejlisty"]) + + return_data = ( + jsonify(isError=False, message="Success", statusCode=200, data=return_data), + 200, + ) + return return_data + + + + def flask_debug(): """ The `flask_debug` function starts a Flask application in debug mode without using the reloader.