mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-19 00:02:11 +00:00
Test phase 2
This commit is contained in:
@@ -36,7 +36,7 @@ import spotipy
|
|||||||
import tiktoken
|
import tiktoken
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
from spotipy.oauth2 import SpotifyClientCredentials
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
|
from flask import jsonify
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
from spotify_dl import spotify
|
from spotify_dl import spotify
|
||||||
from spotify_dl import youtube as youtube_download
|
from spotify_dl import youtube as youtube_download
|
||||||
@@ -71,6 +71,7 @@ MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30
|
|||||||
|
|
||||||
FILE_SERVICE_ADDRESS = "http://192.168.1.15:5000"
|
FILE_SERVICE_ADDRESS = "http://192.168.1.15:5000"
|
||||||
GET_MP3 = "/mp3"
|
GET_MP3 = "/mp3"
|
||||||
|
SEND_MP3 = "/update_mp3"
|
||||||
|
|
||||||
# *=========================================== Platform Specific Predefines
|
# *=========================================== Platform Specific Predefines
|
||||||
|
|
||||||
@@ -162,19 +163,52 @@ logger.addHandler(handler)
|
|||||||
random.seed()
|
random.seed()
|
||||||
logger.debug(platform)
|
logger.debug(platform)
|
||||||
logger.info("Playlist generation")
|
logger.info("Playlist generation")
|
||||||
|
|
||||||
# *=========================================== Load Data files
|
# *=========================================== Load Data files
|
||||||
|
|
||||||
|
class MusicFileList(object):
|
||||||
|
# The `MusicFileList` class manages a list of music files, refreshing the list by connecting to a
|
||||||
|
# file service or scanning a local directory if the service is unavailable.
|
||||||
|
def __init__(self, uplogger) -> None:
|
||||||
|
self.music_file_list = []
|
||||||
|
self.file_service_active = False
|
||||||
|
self.logger = uplogger
|
||||||
|
self.logger.info("Created Playlist organizer class")
|
||||||
|
|
||||||
|
def refresh_file_list(self):
|
||||||
|
"""
|
||||||
|
The `refresh_file_list` function attempts to connect to a file service to retrieve a list of music
|
||||||
|
files, and if unsuccessful, it populates the list by scanning a local directory for .mp3 files.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.logger.info("Attempt to connect to file service")
|
||||||
|
response = requests.get(f'{FILE_SERVICE_ADDRESS}{GET_MP3}',timeout=360)
|
||||||
|
self.music_file_list = response.json()["music_file_list"]
|
||||||
|
self.file_service_active = True
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"):
|
||||||
|
temp_music_file = mp3_item.as_posix()
|
||||||
|
if platform == "win32":
|
||||||
|
temp_music_file = temp_music_file.replace("/", "\\")
|
||||||
|
self.music_file_list.append(temp_music_file)
|
||||||
|
self.file_service_active = False
|
||||||
|
self.logger.error(e.strerror)
|
||||||
|
self.logger.error("Service Unavailable")
|
||||||
|
def get_file_list(self):
|
||||||
|
"""
|
||||||
|
The `get_file_list` function returns the list of music files stored in the object.
|
||||||
|
:return: The `music_file_list` attribute is being returned.
|
||||||
|
"""
|
||||||
|
return self.music_file_list
|
||||||
|
def update_file_list(self,item):
|
||||||
|
self.music_file_list.append(item)
|
||||||
|
data = jsonify({"item" : item})
|
||||||
|
requests.post(f'{FILE_SERVICE_ADDRESS}{GET_MP3}', data, timeout = 360)
|
||||||
|
|
||||||
|
|
||||||
music_file_list = []
|
music_file_list = []
|
||||||
try:
|
music_file_list = MusicFileList(logger)
|
||||||
logger.info("Attempt to connect to file service")
|
music_file_list.refresh_file_list()
|
||||||
response = requests.get(f'{FILE_SERVICE_ADDRESS}{GET_MP3}',timeout=360)
|
|
||||||
music_file_list = response.json()["music_file_list"]
|
|
||||||
except:
|
|
||||||
for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"):
|
|
||||||
temp_music_file = mp3_item.as_posix()
|
|
||||||
if platform == "win32":
|
|
||||||
temp_music_file = temp_music_file.replace("/", "\\")
|
|
||||||
music_file_list.append(temp_music_file)
|
|
||||||
logger.error("Service Unavailable")
|
|
||||||
logger.info("Playlist generation done")
|
logger.info("Playlist generation done")
|
||||||
|
|
||||||
logger.info("Loading GPT settings")
|
logger.info("Loading GPT settings")
|
||||||
@@ -460,8 +494,8 @@ async def play(ctx, zamawial=None, arg=None):
|
|||||||
zamawial = MUZYKA["requester"].pop()
|
zamawial = MUZYKA["requester"].pop()
|
||||||
logger.info("Muzyka z listy zamówień")
|
logger.info("Muzyka z listy zamówień")
|
||||||
else:
|
else:
|
||||||
index = random.randint(0, len(music_file_list) - 1)
|
index = random.randint(0, len(music_file_list.get_file_list()) - 1)
|
||||||
file_to_play = music_file_list[index]
|
file_to_play = music_file_list.get_file_list()[index]
|
||||||
logger.info("Muzyka losowo")
|
logger.info("Muzyka losowo")
|
||||||
|
|
||||||
logger.info(file_to_play)
|
logger.info(file_to_play)
|
||||||
@@ -567,7 +601,7 @@ async def check_data():
|
|||||||
await channel.send(message)
|
await channel.send(message)
|
||||||
# trunk-ignore(codespell/misspelled)
|
# trunk-ignore(codespell/misspelled)
|
||||||
# TODO: dlaczego sie tu wypierdala
|
# TODO: dlaczego sie tu wypierdala
|
||||||
if os.path.getsize(LOGFILE) > 600000:
|
if os.path.getsize(LOGFILE) > 6000000:
|
||||||
shutil.copyfile(LOGFILE, f"{LOGSTORE}discord{datetime.now()}")
|
shutil.copyfile(LOGFILE, f"{LOGSTORE}discord{datetime.now()}")
|
||||||
logger.info("Log rollover")
|
logger.info("Log rollover")
|
||||||
handler.doRollover()
|
handler.doRollover()
|
||||||
@@ -958,7 +992,7 @@ async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None):
|
|||||||
word_list = ctx.message.content.split()
|
word_list = ctx.message.content.split()
|
||||||
logger.info("Wyszukuje")
|
logger.info("Wyszukuje")
|
||||||
search_weight = []
|
search_weight = []
|
||||||
for _ in range(len(music_file_list)):
|
for _ in range(len(music_file_list.get_file_list())):
|
||||||
search_weight.append((0, ""))
|
search_weight.append((0, ""))
|
||||||
|
|
||||||
time_start = datetime.now()
|
time_start = datetime.now()
|
||||||
@@ -970,7 +1004,7 @@ async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None):
|
|||||||
token_weight = len(word)
|
token_weight = len(word)
|
||||||
logger.info("Słowo kluczowe: %s", word)
|
logger.info("Słowo kluczowe: %s", word)
|
||||||
itr = 0
|
itr = 0
|
||||||
async for file in async_iterator_generator(music_file_list):
|
async for file in async_iterator_generator(music_file_list.get_file_list()):
|
||||||
file = file.split(SEPARATOR_FILE_PATH)
|
file = file.split(SEPARATOR_FILE_PATH)
|
||||||
|
|
||||||
char_remove = [
|
char_remove = [
|
||||||
@@ -1016,7 +1050,7 @@ async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None):
|
|||||||
search_weight[itr][0]
|
search_weight[itr][0]
|
||||||
+ (token_weight + (pingu**1.5) / pattern_len) / matched_times
|
+ (token_weight + (pingu**1.5) / pattern_len) / matched_times
|
||||||
)
|
)
|
||||||
search_weight[itr] = temp_weight, music_file_list[itr]
|
search_weight[itr] = temp_weight, music_file_list.get_file_list()[itr]
|
||||||
matched_times += 1
|
matched_times += 1
|
||||||
itr += 1
|
itr += 1
|
||||||
|
|
||||||
@@ -1111,7 +1145,7 @@ async def dej_co_ze_spotifaja(ctx):
|
|||||||
+ ".mp3",
|
+ ".mp3",
|
||||||
)
|
)
|
||||||
MUZYKA["requester"].insert(0, ctx.author)
|
MUZYKA["requester"].insert(0, ctx.author)
|
||||||
music_file_list.append(file)
|
music_file_list.update_file_list(file)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
f"Dodałem do listy {dir_path}{file['artist']} - + {file['name']}.mp3"
|
f"Dodałem do listy {dir_path}{file['artist']} - + {file['name']}.mp3"
|
||||||
)
|
)
|
||||||
@@ -1144,7 +1178,7 @@ async def dej_co_z_jutuba(ctx):
|
|||||||
for file_iter in files:
|
for file_iter in files:
|
||||||
global MUZYKA # pylint: disable=global-variable-not-assigned
|
global MUZYKA # pylint: disable=global-variable-not-assigned
|
||||||
MUZYKA["queue"].insert(0, file_iter)
|
MUZYKA["queue"].insert(0, file_iter)
|
||||||
music_file_list.append(file_iter)
|
music_file_list.update_file_list(file_iter)
|
||||||
MUZYKA["requester"].insert(0, ctx.author)
|
MUZYKA["requester"].insert(0, ctx.author)
|
||||||
await ctx.send(f"Dodałem do listy {file_iter}")
|
await ctx.send(f"Dodałem do listy {file_iter}")
|
||||||
logger.info("Jutub udany")
|
logger.info("Jutub udany")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from platform import uname
|
from platform import uname
|
||||||
from sys import platform
|
from sys import platform
|
||||||
|
import json
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
@@ -42,6 +43,14 @@ app = Flask(__name__)
|
|||||||
def get_music_list():
|
def get_music_list():
|
||||||
return jsonify({'music_file_list':music_file_list})
|
return jsonify({'music_file_list':music_file_list})
|
||||||
|
|
||||||
|
#TODO: Przyjecie informacji o zmianie i dodanie pliku saute
|
||||||
|
@app.route('/update_mp3', methods=['POST'])
|
||||||
|
def update_music_list():
|
||||||
|
record = json.loads(request.data)
|
||||||
|
print(record)
|
||||||
|
music_file_list.appemd(record["item"])
|
||||||
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
app.run(debug=True, host="0.0.0.0")
|
app.run(debug=True, host="0.0.0.0")
|
||||||
|
|
||||||
@@ -49,6 +58,4 @@ if __name__=='__main__':
|
|||||||
|
|
||||||
#TODO: Informacja o zmianie i update
|
#TODO: Informacja o zmianie i update
|
||||||
|
|
||||||
#TODO: Odpowiedz na zapytanie
|
#TODO: Move download Youtube i Spotify tuta j
|
||||||
|
|
||||||
#TODO: Przyjecie informacji o zmianie i dodanie pliku saute
|
|
||||||
Reference in New Issue
Block a user