cleanup/refactor

This commit is contained in:
codey 2025-02-12 07:53:22 -05:00
parent 128019423b
commit 9f615ad0d9
2 changed files with 41 additions and 22 deletions

View File

@ -143,8 +143,10 @@ class Radio(FastAPI):
Otherwise, current track album art will be pulled.
"""
try:
if not track_id:
track_id = self.radio_util.now_playing.get('id')
logging.debug("Seeking album art with trackId: %s", track_id)
album_art: Optional[bytes] = await self.radio_util._get_album_art(track_id=track_id)
album_art: Optional[bytes] = await self.radio_util.get_album_art(track_id=track_id)
if not album_art:
return RedirectResponse(url="https://codey.lol/images/radio_art_default.jpg",
status_code=302)
@ -205,7 +207,7 @@ class Radio(FastAPI):
traceback.print_exc()
try:
if not await self.radio_util.get_album_art(file_path=next['file_path']):
album_art = await self.radio_util._get_album_art(next['file_path'])
album_art = await self.radio_util.get_album_art(file_path=next['file_path'])
await self.radio_util.cache_album_art(next['id'], album_art)
except:
traceback.print_exc()

View File

@ -59,7 +59,17 @@ class RadioUtil:
"""
return str(datetime.timedelta(seconds=s)).split(".", maxsplit=1)[0]
async def search_playlist(self, artistsong: str|None = None, artist: str|None = None, song: str|None = None) -> bool:
async def search_playlist(self, artistsong: Optional[str] = None, artist: Optional[str] = None,
song: Optional[str] = None) -> bool:
"""
Search for track, add it up next in play queue if found
Args:
artistsong (Optional[str]): Artist - Song combo to search [ignored if artist/song are specified]
artist (Optional[str]): Artist to search (ignored if artistsong is specified)
song (Optional[str]): Song to search (ignored if artistsong is specified)
Returns:
bool
"""
if artistsong and (artist or song):
raise RadioException("Cannot search using combination provided")
if not artistsong and (not artist or not song):
@ -107,6 +117,7 @@ class RadioUtil:
return False
async def load_playlist(self):
"""Load Playlist"""
try:
logging.info(f"Loading playlist...")
self.active_playlist.clear()
@ -118,7 +129,8 @@ class RadioUtil:
"""
db_query: str = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, genre, file_path, duration FROM tracks\
WHERE genre IN ("metalcore", "pop punk", "punk rock", "metal", "punk", "electronic", "nu metal", "EDM",\
WHERE genre IN ("metalcore", "rock", "pop punk", "math rock", "punk rock",\
"metal", "punk", "electronic", "nu metal", "EDM",\
"post-hardcore", "pop rock", "experimental", "post-punk", "death metal", "electronicore", "hard rock", "psychedelic rock",\
"grunge", "house", "dubstep", "hardcore", "hair metal", "horror punk", "folk punk", "breakcore",\
"post-rock", "deathcore", "hardcore punk", "synthwave", "trap") GROUP BY artistdashsong ORDER BY RANDOM()'
@ -144,6 +156,14 @@ class RadioUtil:
traceback.print_exc()
async def cache_album_art(self, track_id: int, album_art: bytes) -> None:
"""
Cache Album Art to SQLite DB
Args:
track_id (int): Track ID to update
album_art (bytes): Album art data
Returns:
None
"""
try:
async with sqlite3.connect(self.active_playlist_path,
timeout=2) as db_conn:
@ -155,6 +175,14 @@ class RadioUtil:
async def get_album_art(self, track_id: Optional[int] = None,
file_path: Optional[str] = None) -> bytes:
"""
Get Album Art
Args:
track_id (Optional[int]): Track ID to query (ignored if file_path is specified)
file_path (Optional[str]): file_path to query (ignored if track_id is specified)
Returns:
bytes
"""
try:
async with sqlite3.connect(self.active_playlist_path,
timeout=2) as db_conn:
@ -176,24 +204,6 @@ class RadioUtil:
traceback.print_exc()
return
async def _get_album_art(self, track_id: Optional[int] = None, file_path: Optional[str] = None) -> Optional[bytes]:
try:
if not file_path:
file_path: Optional[str] = self.now_playing.get('file_path')
if not file_path:
logging.critical("_get_album_art:: No current file")
return
original_file_path: Optional[str] = file_path
file_path: Optional[str] = file_path.replace("/paul/toons/",
"/singer/gogs_toons/")
cached_album_art: Optional[bytes|bool] = await self.get_album_art(file_path=original_file_path,
track_id=track_id)
if cached_album_art:
return cached_album_art
except:
traceback.print_exc()
def get_queue_item_by_uuid(self, uuid: str) -> Optional[tuple[int, dict]]:
"""
Get queue item by UUID
@ -208,6 +218,13 @@ class RadioUtil:
return None
async def _ls_skip(self) -> bool:
"""
Ask LiquidSoap server to skip to the next track
Args:
None
Returns:
bool
"""
try:
async with ClientSession() as session:
async with session.get(f"{self.ls_uri}/next",