Main refactoring

This commit is contained in:
2024-11-10 15:03:44 +01:00
parent 28fdfa0982
commit 9a0f869839
20 changed files with 2349 additions and 2280 deletions
+57 -19
View File
@@ -1,27 +1,27 @@
import logging
import asyncio
import logging
import re
import uuid
from pathlib import Path, PurePath
from sys import platform
import requests
import yt_dlp
from constants import (
FILE_SERVICE_ADDRESS,
GET_MP3,
GET_PLAYLIST,
MUSIC_FOLDER,
SEND_MP3,
SPOTIFY_CTRL,
)
from spotify_dl import spotify
from spotify_dl import youtube as youtube_download
import re
import netrc
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from constants import NETRC_FILE, MUSIC_FOLDER, FILE_SERVICE_ADDRESS, GET_MP3, SEND_MP3
import yt_dlp
import requests
netrc_mod = netrc.netrc(NETRC_FILE)
REMOTE_HOST_NAME = "spotipy"
authTokens = netrc_mod.authenticators(REMOTE_HOST_NAME)
logger = logging.getLogger("discord")
spotify_ctrl = spotipy.Spotify(
client_credentials_manager=SpotifyClientCredentials(
client_id=authTokens[0],
client_secret=authTokens[2],
)
)
class MusicFileList(object):
"""
The `MusicFileList` class manages a list of music files, with the ability to refresh the list from a
@@ -74,6 +74,7 @@ class MusicFileList(object):
post_data = {"item": str(item)}
requests.post(f"{FILE_SERVICE_ADDRESS}{SEND_MP3}", json=post_data, timeout=360)
MUSIC_FILE_LIST = MusicFileList(logger)
@@ -103,11 +104,11 @@ async def get_file(ctx, source, link):
logger.info(item_id)
logger.info("Spotify link provided")
file_list = await asyncio.to_thread(
spotify.fetch_tracks, spotify_ctrl, item_type, link
spotify.fetch_tracks, SPOTIFY_CTRL, item_type, link
)
logger.info(file_list)
directory_name = await asyncio.to_thread(
spotify.get_item_name, spotify_ctrl, item_type, item_id
spotify.get_item_name, SPOTIFY_CTRL, item_type, item_id
)
logger.info(directory_name)
logger.info("Spotify scrape done")
@@ -245,3 +246,40 @@ async def get_file(ctx, source, link):
)
file_list.append("wiadomo co wiadomo gdzie")
return dir_path, file_list
async def search_music(ctx, how_many=0, slowa_kluczowe=None):
"""
Take in a context object and an optional integer parameter "how_many" and perform search
operation on music library.
:param ctx: ctx stands for "context" and is a parameter commonly used in Discord.py commands. It
represents the context in which the command was invoked, including information such as the message,
the channel, the server, and the user who invoked the command
:param how_many: how_many is a parameter that specifies the number of search results to be returned.
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)
"""
# 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")
jrequest = {
"lista_slow": word_list,
"dlugosc_plejlisty": how_many,
"UUID": str(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
logger.info(return_data.json()["data"])
return return_data.json()["data"]