Music search move file server side att 1

This commit is contained in:
2024-04-05 23:17:51 +02:00
parent 29ef0eb2bc
commit 3bb370e5e0
2 changed files with 156 additions and 88 deletions
+8 -88
View File
@@ -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):
"""