This commit is contained in:
codey 2025-04-26 21:27:55 -04:00
parent 6a43d32808
commit 0b70d93d47
9 changed files with 65 additions and 34 deletions

View File

@ -56,7 +56,8 @@ class KarmaDB:
"SELECT keyword, score FROM karma ORDER BY score DESC LIMIT ?", (n,) "SELECT keyword, score FROM karma ORDER BY score DESC LIMIT ?", (n,)
) as db_cursor: ) as db_cursor:
return await db_cursor.fetchall() return await db_cursor.fetchall()
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()
return None return None
@ -73,7 +74,7 @@ class KarmaDB:
Optional[bool] Optional[bool]
""" """
if not flag in [0, 1]: if flag not in [0, 1]:
return None return None
modifier: str = "score + 1" if not flag else "score - 1" modifier: str = "score + 1" if not flag else "score - 1"
@ -182,7 +183,8 @@ class Karma(FastAPI):
}, },
) )
return JSONResponse(content=top10) return JSONResponse(content=top10)
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()
return JSONResponse( return JSONResponse(
status_code=500, status_code=500,
@ -214,7 +216,8 @@ class Karma(FastAPI):
"count": count, "count": count,
} }
) )
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()
return JSONResponse( return JSONResponse(
status_code=500, status_code=500,
@ -239,7 +242,7 @@ class Karma(FastAPI):
): ):
raise HTTPException(status_code=403, detail="Unauthorized") raise HTTPException(status_code=403, detail="Unauthorized")
if not data.flag in [0, 1]: if data.flag not in [0, 1]:
return JSONResponse( return JSONResponse(
status_code=500, status_code=500,
content={ content={

View File

@ -1,4 +1,5 @@
import importlib import importlib
import logging
import traceback import traceback
from typing import Optional, Union from typing import Optional, Union
from fastapi import FastAPI from fastapi import FastAPI
@ -214,7 +215,8 @@ class LastFM(FastAPI):
track_info_result.get("errorText", "??"), track_info_result.get("errorText", "??"),
) )
return JSONResponse(content={"success": True, "result": track_info_result}) return JSONResponse(content={"success": True, "result": track_info_result})
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()
return JSONResponse( return JSONResponse(
status_code=500, status_code=500,

View File

@ -125,6 +125,14 @@ class LyricSearch(FastAPI):
"errorText": f"Unknown request source: {data.src}", "errorText": f"Unknown request source: {data.src}",
}, },
) )
if data.a == "N/A" and data.s == "N/A":
return JSONResponse(
status_code=200,
content={
'test': 'success',
}
)
if not data.t: if not data.t:
search_artist: Optional[str] = data.a search_artist: Optional[str] = data.a

View File

@ -96,9 +96,9 @@ class Misc(FastAPI):
""" """
# Measure response time w/ test lyric search # Measure response time w/ test lyric search
time_start: float = time.time() # Start time for response_time time_start: float = time.time() # Start time for response_time
test_lyrics_result = await self.redis_client.ft().search( test_lyrics_result = await self.redis_client.ft().search( # noqa: F841
"@artist: test @song: test" "@artist: test @song: test"
) )
time_end: float = time.time() time_end: float = time.time()
# End response time test # End response time test
total_keys = await self.redis_client.dbsize() total_keys = await self.redis_client.dbsize()

View File

@ -2,7 +2,6 @@ import logging
import traceback import traceback
import time import time
import random import random
from multiprocessing.pool import ThreadPool as Pool
from .constructors import ( from .constructors import (
ValidRadioNextRequest, ValidRadioNextRequest,
ValidRadioReshuffleRequest, ValidRadioReshuffleRequest,
@ -129,14 +128,12 @@ class Radio(FastAPI):
start: int = int(data.start) start: int = int(data.start)
end: int = start + 20 end: int = start + 20
search: Optional[str] = data.search search: Optional[str] = data.search
logging.info("queue request with start pos: %s & end pos: %s", start, end)
if not search: if not search:
queue_full: list = self.radio_util.active_playlist queue_full: list = self.radio_util.active_playlist
else: else:
queue_full: list = self.radio_util.datatables_search(data.search) queue_full: list = self.radio_util.datatables_search(data.search)
queue: list = queue_full[start:end] queue: list = queue_full[start:end]
logging.info("queue length: %s", len(queue))
queue_out: list[dict] = [] queue_out: list[dict] = []
for x, item in enumerate(queue): for x, item in enumerate(queue):
queue_out.append( queue_out.append(
@ -313,9 +310,7 @@ class Radio(FastAPI):
if len(self.radio_util.active_playlist) > 1: if len(self.radio_util.active_playlist) > 1:
self.radio_util.active_playlist.append(next) # Push to end of playlist self.radio_util.active_playlist.append(next) # Push to end of playlist
else: else:
with Pool() as pool: self.loop.run_in_executor(None, self.radio_util.load_playlist)
pool.apply_async(self.radio_util.load_playlist())
pool.close()
self.radio_util.now_playing = next self.radio_util.now_playing = next
next["start"] = time_started next["start"] = time_started
@ -374,7 +369,7 @@ class Radio(FastAPI):
await self.radio_util._ls_skip() await self.radio_util._ls_skip()
return JSONResponse(content={"result": search}) return JSONResponse(content={"result": search})
async def radio_typeahead( def radio_typeahead(
self, data: ValidRadioTypeaheadRequest, request: Request self, data: ValidRadioTypeaheadRequest, request: Request
) -> JSONResponse: ) -> JSONResponse:
""" """

View File

@ -166,5 +166,6 @@ class Genius:
await self.redis_cache.increment_found_count(self.label) await self.redis_cache.increment_found_count(self.label)
await self.cache.store(matched) await self.cache.store(matched)
return matched return matched
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()

View File

@ -156,7 +156,9 @@ class LRCLib:
time=time_diff, time=time_diff,
) )
await self.redis_cache.increment_found_count(self.label) await self.redis_cache.increment_found_count(self.label)
await self.cache.store(matched) if plain:
await self.cache.store(matched)
return matched return matched
except: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()

View File

@ -14,7 +14,7 @@ from lyric_search.constructors import LyricsResult
import redis.asyncio as redis import redis.asyncio as redis
from redis.commands.search.query import Query # type: ignore from redis.commands.search.query import Query # type: ignore
from redis.commands.search.indexDefinition import IndexDefinition, IndexType # type: ignore from redis.commands.search.indexDefinition import IndexDefinition, IndexType # type: ignore
from redis.commands.search.field import TextField, TagField # type: ignore from redis.commands.search.field import TextField # type: ignore
from redis.commands.json.path import Path # type: ignore from redis.commands.json.path import Path # type: ignore
from . import private from . import private
@ -204,8 +204,8 @@ class RedisCache:
) )
return search_res_out return search_res_out
except Exception as e: except Exception as e:
logging.debug("Exception: %s", str(e))
traceback.print_exc() traceback.print_exc()
# await self.notifier.send(f"ERROR @ {__file__.rsplit("/", maxsplit=1)[-1]}", f"{str(e)}\nSearch was: {artist} - {song}; fuzzy: {fuzzy_artist} - {fuzzy_song}")
return None return None
async def redis_store(self, sqlite_id: int, lyr_result: LyricsResult) -> None: async def redis_store(self, sqlite_id: int, lyr_result: LyricsResult) -> None:

View File

@ -3,7 +3,7 @@ import traceback
import time import time
import datetime import datetime
import os import os
import asyncio import random
from uuid import uuid4 as uuid from uuid import uuid4 as uuid
from typing import Union, Optional, Iterable from typing import Union, Optional, Iterable
from aiohttp import ClientSession, ClientTimeout from aiohttp import ClientSession, ClientTimeout
@ -20,13 +20,10 @@ non_alnum: Pattern = regex.compile(r"[^a-zA-Z0-9]")
""" """
TODO: TODO:
- Album art rework
- get_genre should only be called once for load_playlist, rework get_genre to (optionally) accept a list of artists, - get_genre should only be called once for load_playlist, rework get_genre to (optionally) accept a list of artists,
and return (optionally) a list instead of an str and return (optionally) a list instead of an str
- Allow tracks to be queried again based on genre; unable to query tracks based on genre presently,
as genre was moved outside track_file_map to artist_genre_map
- Ask GPT when we encounter an untagged (no genre defined) artist, automation is needed for this tedious task - Ask GPT when we encounter an untagged (no genre defined) artist, automation is needed for this tedious task
- etc.. - etc..
""" """
@ -93,7 +90,7 @@ class RadioUtil:
""" """
return str(datetime.timedelta(seconds=s)).split(".", maxsplit=1)[0] return str(datetime.timedelta(seconds=s)).split(".", maxsplit=1)[0]
async def trackdb_typeahead(self, query: str) -> Optional[list[str]]: def trackdb_typeahead(self, query: str) -> Optional[list[str]]:
""" """
Query track db for typeahead Query track db for typeahead
Args: Args:
@ -338,10 +335,10 @@ class RadioUtil:
LIMITED GENRES LIMITED GENRES
""" """
# db_query: str = ( db_query: str = (
# 'SELECT distinct(LOWER(TRIM(artist)) || " - " || LOWER(TRIM(song))), (TRIM(artist) || " - " || TRIM(song))' 'SELECT distinct(LOWER(TRIM(artist)) || " - " || LOWER(TRIM(song))), (TRIM(artist) || " - " || TRIM(song))'
# "AS artistdashsong, id, artist, song, album, file_path, duration FROM tracks GROUP BY artistdashsong ORDER BY RANDOM()" "AS artistdashsong, id, artist, song, album, file_path, duration FROM tracks"
# ) )
""" """
LIMITED TO ONE/SMALL SUBSET OF GENRES LIMITED TO ONE/SMALL SUBSET OF GENRES
@ -354,8 +351,8 @@ class RadioUtil:
LIMITED TO ONE/SOME ARTISTS... LIMITED TO ONE/SOME ARTISTS...
""" """
db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, file_path, duration FROM tracks\ # db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, file_path, duration FROM tracks\
WHERE (artist LIKE "%chunk!%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(2022)%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY RANDOM()' # ORDER BY album ASC, id ASC' # WHERE (artist LIKE "%outline in color%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(2022)%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY RANDOM()' # ORDER BY album ASC, id ASC'
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\ # db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
# WHERE (artist LIKE "%sullivan king%" OR artist LIKE "%kayzo%" OR artist LIKE "%adventure club%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(2022)%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY RANDOM()'# ORDER BY album ASC, id ASC' # WHERE (artist LIKE "%sullivan king%" OR artist LIKE "%kayzo%" OR artist LIKE "%adventure club%") AND (NOT song LIKE "%%stripped%%" AND NOT song LIKE "%(2022)%" AND NOT song LIKE "%(live%%" AND NOT song LIKE "%%acoustic%%" AND NOT song LIKE "%%instrumental%%" AND NOT song LIKE "%%remix%%" AND NOT song LIKE "%%reimagined%%" AND NOT song LIKE "%%alternative%%" AND NOT song LIKE "%%unzipped%%") GROUP BY artistdashsong ORDER BY RANDOM()'# ORDER BY album ASC, id ASC'
@ -385,12 +382,33 @@ class RadioUtil:
"file_path": r["file_path"], "file_path": r["file_path"],
"duration": r["duration"], "duration": r["duration"],
} }
for r in results for r in results if r not in self.active_playlist
] ]
logging.info( logging.info(
"Populated active playlists with %s items", "Populated active playlists with %s items",
len(self.active_playlist), len(self.active_playlist),
) )
random.shuffle(self.active_playlist)
"""Dedupe"""
logging.info("Removing duplicate tracks...")
dedupe_processed = []
for item in self.active_playlist:
artistsongabc: str = non_alnum.sub('', item.get('artistsong', None))
if not artistsongabc:
logging.info("Missing artistsong: %s", item)
continue
if artistsongabc in dedupe_processed:
self.active_playlist.remove(item)
dedupe_processed.append(artistsongabc)
logging.info(
"Duplicates removed." "New playlist size: %s",
len(self.active_playlist))
logging.info("Playlist: %s", [str(a.get('artistsong', '')) for a in self.active_playlist])
if self.playback_genres: if self.playback_genres:
new_playlist: list[dict] = [] new_playlist: list[dict] = []
logging.info("Limiting playback genres") logging.info("Limiting playback genres")
@ -400,6 +418,8 @@ class RadioUtil:
for genre in self.playback_genres: for genre in self.playback_genres:
genre = genre.strip().lower() genre = genre.strip().lower()
if genre in item_genres: if genre in item_genres:
if item in new_playlist:
continue
new_playlist.append(item) new_playlist.append(item)
matched_genre = True matched_genre = True
continue continue