This commit is contained in:
2025-02-14 16:07:24 -05:00
parent 00af36703a
commit 60416c493f
19 changed files with 204 additions and 308 deletions

View File

@@ -13,7 +13,7 @@ import os
import gpt
from aiohttp import ClientSession, ClientTimeout
import aiosqlite as sqlite3
from typing import Optional, LiteralString
from typing import Union, Optional, LiteralString
from uuid import uuid4 as uuid
from .constructors import RadioException
@@ -28,7 +28,7 @@ class RadioUtil:
self.active_playlist_path: str|LiteralString = os.path.join("/usr/local/share",
"sqlite_dbs", "track_file_map.db")
self.active_playlist_name = "default" # not used
self.active_playlist: list = []
self.active_playlist: list[dict] = []
self.now_playing: dict = {
'artist': 'N/A',
'song': 'N/A',
@@ -75,8 +75,6 @@ class RadioUtil:
if not artistsong and (not artist or not song):
raise RadioException("No query provided")
try:
search_artist: Optional[str] = None
search_song: Optional[str] = None
search_query: str = 'SELECT id, artist, song, (artist || " - " || song) AS artistsong, genre, file_path, duration FROM tracks\
WHERE editdist3((lower(artist) || " " || lower(song)), (? || " " || ?))\
<= 410 ORDER BY editdist3((lower(artist) || " " || lower(song)), ?) ASC LIMIT 1'
@@ -84,10 +82,10 @@ class RadioUtil:
artistsong_split: list = artistsong.split(" - ", maxsplit=1)
(search_artist, search_song) = tuple(artistsong_split)
else:
search_artist: str = artist
search_song: str = song
search_artist = artist
search_song = song
if not artistsong:
artistsong: str = f"{search_artist} - {search_song}"
artistsong = f"{search_artist} - {search_song}"
search_params = (search_artist.lower(), search_song.lower(), artistsong.lower(),)
async with sqlite3.connect(self.active_playlist_path,
timeout=2) as db_conn:
@@ -97,7 +95,7 @@ class RadioUtil:
db_conn.row_factory = sqlite3.Row
async with await db_conn.execute(search_query, search_params) as db_cursor:
result: Optional[sqlite3.Row|bool] = await db_cursor.fetchone()
if not result:
if not result or not isinstance(result, sqlite3.Row):
return False
pushObj: dict = {
'id': result['id'],
@@ -116,7 +114,7 @@ class RadioUtil:
traceback.print_exc()
return False
async def load_playlist(self):
async def load_playlist(self) -> None:
"""Load Playlist"""
try:
logging.info(f"Loading playlist...")
@@ -135,12 +133,19 @@ class RadioUtil:
"grunge", "house", "dubstep", "hardcore", "hair metal", "horror punk", "folk punk", "breakcore",\
"post-rock", "deathcore", "hardcore punk", "synthwave", "trap") GROUP BY artistdashsong ORDER BY RANDOM()'
"""
LIMITED TO ONE ARTIST...
"""
# db_query: str = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, genre, file_path, duration FROM tracks\
# WHERE artist LIKE "%bad omens%" GROUP BY artistdashsong ORDER BY RANDOM()'
async with sqlite3.connect(self.active_playlist_path,
timeout=2) as db_conn:
db_conn.row_factory = sqlite3.Row
async with await db_conn.execute(db_query) as db_cursor:
results: Optional[list[sqlite3.Row]] = await db_cursor.fetchall()
self.active_playlist: list[dict] = [{
results: list[sqlite3.Row] = await db_cursor.fetchall()
self.active_playlist = [{
'uuid': str(uuid().hex),
'id': r['id'],
'artist': double_space.sub(' ', r['artist']).strip(),
@@ -174,7 +179,7 @@ class RadioUtil:
traceback.print_exc()
async def get_album_art(self, track_id: Optional[int] = None,
file_path: Optional[str] = None) -> bytes:
file_path: Optional[str] = None) -> Optional[bytes]:
"""
Get Album Art
Args:
@@ -191,18 +196,18 @@ class RadioUtil:
query_params: tuple = (track_id,)
if file_path and not track_id:
query: str = "SELECT album_art FROM tracks WHERE file_path = ?"
query_params: tuple = (file_path,)
query = "SELECT album_art FROM tracks WHERE file_path = ?"
query_params = (file_path,)
async with await db_conn.execute(query,
query_params) as db_cursor:
result: Optional[sqlite3.Row|bool] = await db_cursor.fetchone()
if not result:
return
if not result or not isinstance(result, sqlite3.Row):
return None
return result['album_art']
except:
traceback.print_exc()
return
return None
def get_queue_item_by_uuid(self, uuid: str) -> Optional[tuple[int, dict]]:
"""
@@ -249,7 +254,7 @@ class RadioUtil:
response: Optional[str] = await self.gpt.get_completion(prompt=f"I am going to listen to {song} by {artist}.")
if not response:
logging.critical("No response received from GPT?")
return
return None
return response
async def webhook_song_change(self, track: dict) -> None:
@@ -314,7 +319,7 @@ class RadioUtil:
if not ai_response:
return
hook_data: dict = {
hook_data = {
'username': 'GPT',
"embeds": [{
"title": "AI Feedback",