radio_util: open tracks SQLite DB in readonly mode; black: reformat files
This commit is contained in:
@ -2,5 +2,6 @@
|
||||
LastFM
|
||||
"""
|
||||
|
||||
|
||||
class InvalidLastFMResponseException(Exception):
|
||||
pass
|
||||
pass
|
||||
|
@ -6,54 +6,60 @@ from aiohttp import ClientSession, ClientTimeout
|
||||
from constants import Constants
|
||||
from .constructors import InvalidLastFMResponseException
|
||||
|
||||
|
||||
class LastFM:
|
||||
"""LastFM Endpoints"""
|
||||
def __init__(self,
|
||||
noInit: Optional[bool] = False) -> None:
|
||||
|
||||
def __init__(self, noInit: Optional[bool] = False) -> None:
|
||||
self.creds = Constants().LFM_CREDS
|
||||
self.api_base_url: str = "https://ws.audioscrobbler.com/2.0"
|
||||
|
||||
|
||||
async def search_artist(self, artist: Optional[str] = None) -> dict:
|
||||
"""Search LastFM for an artist"""
|
||||
try:
|
||||
if not artist:
|
||||
return {
|
||||
'err': 'No artist specified.',
|
||||
"err": "No artist specified.",
|
||||
}
|
||||
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("method", "artist.getInfo"),
|
||||
("artist", artist),
|
||||
("api_key", self.creds.get('key')),
|
||||
("api_key", self.creds.get("key")),
|
||||
("autocorrect", "1"),
|
||||
("format", "json"),
|
||||
]
|
||||
|
||||
async with ClientSession() as session:
|
||||
|
||||
async with await session.get(self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
|
||||
async with ClientSession() as session:
|
||||
|
||||
async with await session.get(
|
||||
self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8),
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
data: dict = await request.json()
|
||||
data = data.get('artist', 'N/A')
|
||||
|
||||
data = data.get("artist", "N/A")
|
||||
|
||||
ret_obj: dict = {
|
||||
'id': data.get('mbid'),
|
||||
'touring': data.get('ontour'),
|
||||
'name': data.get('name'),
|
||||
'bio': data.get('bio', None).get('summary').strip()\
|
||||
.split("<a href")[0],
|
||||
}
|
||||
"id": data.get("mbid"),
|
||||
"touring": data.get("ontour"),
|
||||
"name": data.get("name"),
|
||||
"bio": data.get("bio", None)
|
||||
.get("summary")
|
||||
.strip()
|
||||
.split("<a href")[0],
|
||||
}
|
||||
return ret_obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
|
||||
async def get_track_info(self, artist: Optional[str] = None,
|
||||
track: Optional[str] = None) -> Optional[dict]:
|
||||
|
||||
async def get_track_info(
|
||||
self, artist: Optional[str] = None, track: Optional[str] = None
|
||||
) -> Optional[dict]:
|
||||
"""
|
||||
Get Track Info from LastFM
|
||||
Args:
|
||||
@ -66,12 +72,12 @@ class LastFM:
|
||||
if not artist or not track:
|
||||
logging.info("inv request")
|
||||
return {
|
||||
'err': 'Invalid/No artist or track specified',
|
||||
"err": "Invalid/No artist or track specified",
|
||||
}
|
||||
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("method", "track.getInfo"),
|
||||
("api_key", self.creds.get('key')),
|
||||
("api_key", self.creds.get("key")),
|
||||
("autocorrect", "1"),
|
||||
("artist", artist),
|
||||
("track", track),
|
||||
@ -79,29 +85,32 @@ class LastFM:
|
||||
]
|
||||
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
async with await session.get(
|
||||
self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8),
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
data: dict = await request.json()
|
||||
data = data.get('track', None)
|
||||
if not isinstance(data.get('artist'), dict):
|
||||
data = data.get("track", None)
|
||||
if not isinstance(data.get("artist"), dict):
|
||||
return None
|
||||
artist_mbid: int = data.get('artist', None).get('mbid')
|
||||
album: str = data.get('album', None).get('title')
|
||||
artist_mbid: int = data.get("artist", None).get("mbid")
|
||||
album: str = data.get("album", None).get("title")
|
||||
ret_obj: dict = {
|
||||
'artist_mbid': artist_mbid,
|
||||
'album': album,
|
||||
"artist_mbid": artist_mbid,
|
||||
"album": album,
|
||||
}
|
||||
return ret_obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'General Failure',
|
||||
"err": "General Failure",
|
||||
}
|
||||
|
||||
async def get_album_tracklist(self, artist: Optional[str] = None,
|
||||
album: Optional[str] = None) -> dict:
|
||||
|
||||
async def get_album_tracklist(
|
||||
self, artist: Optional[str] = None, album: Optional[str] = None
|
||||
) -> dict:
|
||||
"""
|
||||
Get Album Tracklist
|
||||
Args:
|
||||
@ -113,24 +122,26 @@ class LastFM:
|
||||
try:
|
||||
if not artist or not album:
|
||||
return {
|
||||
'err': 'No artist or album specified',
|
||||
"err": "No artist or album specified",
|
||||
}
|
||||
|
||||
|
||||
tracks: dict = await self.get_release(artist=artist, album=album)
|
||||
tracks = tracks.get('tracks', None)
|
||||
tracks = tracks.get("tracks", None)
|
||||
ret_obj: dict = {
|
||||
'tracks': tracks,
|
||||
}
|
||||
|
||||
"tracks": tracks,
|
||||
}
|
||||
|
||||
return ret_obj
|
||||
|
||||
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'General Failure',
|
||||
}
|
||||
|
||||
async def get_artist_albums(self, artist: Optional[str] = None) -> Union[dict, list[dict]]:
|
||||
"err": "General Failure",
|
||||
}
|
||||
|
||||
async def get_artist_albums(
|
||||
self, artist: Optional[str] = None
|
||||
) -> Union[dict, list[dict]]:
|
||||
"""
|
||||
Get Artists Albums from LastFM
|
||||
Args:
|
||||
@ -141,37 +152,39 @@ class LastFM:
|
||||
try:
|
||||
if not artist:
|
||||
return {
|
||||
'err': 'No artist specified.',
|
||||
"err": "No artist specified.",
|
||||
}
|
||||
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("method", "artist.gettopalbums"),
|
||||
("artist", artist),
|
||||
("api_key", self.creds.get('key')),
|
||||
("api_key", self.creds.get("key")),
|
||||
("autocorrect", "1"),
|
||||
("format", "json"),
|
||||
]
|
||||
|
||||
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
async with await session.get(
|
||||
self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8),
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
json_data: dict = await request.json()
|
||||
data: dict = json_data.get('topalbums', None).get('album')
|
||||
data: dict = json_data.get("topalbums", None).get("album")
|
||||
ret_obj: list = [
|
||||
{
|
||||
'title': item.get('name')
|
||||
} for item in data if not(item.get('name').lower() == "(null)")\
|
||||
and int(item.get('playcount')) >= 50
|
||||
{"title": item.get("name")}
|
||||
for item in data
|
||||
if not (item.get("name").lower() == "(null)")
|
||||
and int(item.get("playcount")) >= 50
|
||||
]
|
||||
return ret_obj
|
||||
return ret_obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
|
||||
|
||||
async def get_artist_id(self, artist: Optional[str] = None) -> int:
|
||||
"""
|
||||
Get Artist ID from LastFM
|
||||
@ -183,16 +196,16 @@ class LastFM:
|
||||
try:
|
||||
if not artist:
|
||||
return -1
|
||||
artist_search: dict = await self.search_artist(artist=artist)
|
||||
artist_search: dict = await self.search_artist(artist=artist)
|
||||
if not artist_search:
|
||||
logging.debug("[get_artist_id] Throwing no result error")
|
||||
return -1
|
||||
artist_id: int = int(artist_search[0].get('id', 0))
|
||||
artist_id: int = int(artist_search[0].get("id", 0))
|
||||
return artist_id
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return -1
|
||||
|
||||
|
||||
async def get_artist_info_by_id(self, artist_id: Optional[int] = None) -> dict:
|
||||
"""
|
||||
Get Artist info by ID from LastFM
|
||||
@ -204,44 +217,48 @@ class LastFM:
|
||||
try:
|
||||
if not artist_id or not str(artist_id).isnumeric():
|
||||
return {
|
||||
'err': 'Invalid/no artist_id specified.',
|
||||
"err": "Invalid/no artist_id specified.",
|
||||
}
|
||||
|
||||
|
||||
req_url: str = f"{self.api_base_url}/artists/{artist_id}"
|
||||
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("key", self.creds.get('key')),
|
||||
("secret", self.creds.get('secret')),
|
||||
("key", self.creds.get("key")),
|
||||
("secret", self.creds.get("secret")),
|
||||
]
|
||||
|
||||
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(req_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
async with await session.get(
|
||||
req_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8),
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
data: dict = await request.json()
|
||||
if not data.get('profile'):
|
||||
raise InvalidLastFMResponseException("Data did not contain 'profile' key.")
|
||||
|
||||
_id: int = data.get('id', None)
|
||||
name: str = data.get('name', None)
|
||||
profile: str = data.get('profile', '')
|
||||
if not data.get("profile"):
|
||||
raise InvalidLastFMResponseException(
|
||||
"Data did not contain 'profile' key."
|
||||
)
|
||||
|
||||
_id: int = data.get("id", None)
|
||||
name: str = data.get("name", None)
|
||||
profile: str = data.get("profile", "")
|
||||
profile = regex.sub(r"(\[(\/{0,})(u|b|i)])", "", profile)
|
||||
members: list = data.get('members', None)
|
||||
|
||||
members: list = data.get("members", None)
|
||||
|
||||
ret_obj: dict = {
|
||||
'id': _id,
|
||||
'name': name,
|
||||
'profile': profile,
|
||||
'members': members,
|
||||
"id": _id,
|
||||
"name": name,
|
||||
"profile": profile,
|
||||
"members": members,
|
||||
}
|
||||
return ret_obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
|
||||
|
||||
async def get_artist_info(self, artist: Optional[str] = None) -> dict:
|
||||
"""
|
||||
Get Artist Info from LastFM
|
||||
@ -253,27 +270,30 @@ class LastFM:
|
||||
try:
|
||||
if not artist:
|
||||
return {
|
||||
'err': 'No artist specified.',
|
||||
"err": "No artist specified.",
|
||||
}
|
||||
artist_id: Optional[int] = await self.get_artist_id(artist=artist)
|
||||
if not artist_id:
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
artist_info: Optional[dict] = await self.get_artist_info_by_id(artist_id=artist_id)
|
||||
artist_info: Optional[dict] = await self.get_artist_info_by_id(
|
||||
artist_id=artist_id
|
||||
)
|
||||
if not artist_info:
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
return artist_info
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'Failed',
|
||||
"err": "Failed",
|
||||
}
|
||||
|
||||
async def get_release(self, artist: Optional[str] = None,
|
||||
album: Optional[str] = None) -> dict:
|
||||
|
||||
async def get_release(
|
||||
self, artist: Optional[str] = None, album: Optional[str] = None
|
||||
) -> dict:
|
||||
"""
|
||||
Get Release info from LastFM
|
||||
Args:
|
||||
@ -285,55 +305,61 @@ class LastFM:
|
||||
try:
|
||||
if not artist or not album:
|
||||
return {
|
||||
'err': 'Invalid artist/album pair',
|
||||
"err": "Invalid artist/album pair",
|
||||
}
|
||||
|
||||
|
||||
request_params: list[tuple] = [
|
||||
("method", "album.getinfo"),
|
||||
("artist", artist),
|
||||
("album", album),
|
||||
("api_key", self.creds.get('key')),
|
||||
("api_key", self.creds.get("key")),
|
||||
("autocorrect", "1"),
|
||||
("format", "json"),
|
||||
]
|
||||
async with ClientSession() as session:
|
||||
async with await session.get(self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8)) as request:
|
||||
async with await session.get(
|
||||
self.api_base_url,
|
||||
params=request_params,
|
||||
timeout=ClientTimeout(connect=3, sock_read=8),
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
json_data: dict = await request.json()
|
||||
data: dict = json_data.get('album', None)
|
||||
data: dict = json_data.get("album", None)
|
||||
ret_obj: dict = {
|
||||
'id': data.get('mbid'),
|
||||
'artists': data.get('artist'),
|
||||
'tags': data.get('tags'),
|
||||
'title': data.get('name'),
|
||||
'summary': data.get('wiki', None).get('summary').split("<a href")[0]\
|
||||
if "wiki" in data.keys()\
|
||||
else "No summary available for this release.",
|
||||
"id": data.get("mbid"),
|
||||
"artists": data.get("artist"),
|
||||
"tags": data.get("tags"),
|
||||
"title": data.get("name"),
|
||||
"summary": (
|
||||
data.get("wiki", None).get("summary").split("<a href")[0]
|
||||
if "wiki" in data.keys()
|
||||
else "No summary available for this release."
|
||||
),
|
||||
}
|
||||
try:
|
||||
track_key: list = data.get('tracks', None).get('track')
|
||||
except:
|
||||
track_key: list = data.get("tracks", None).get("track")
|
||||
except:
|
||||
track_key = []
|
||||
if isinstance(track_key, list):
|
||||
ret_obj['tracks'] = [
|
||||
ret_obj["tracks"] = [
|
||||
{
|
||||
'duration': item.get('duration', 'N/A'),
|
||||
'title': item.get('name'),
|
||||
} for item in track_key]
|
||||
"duration": item.get("duration", "N/A"),
|
||||
"title": item.get("name"),
|
||||
}
|
||||
for item in track_key
|
||||
]
|
||||
else:
|
||||
ret_obj['tracks'] = [
|
||||
{
|
||||
'duration': data.get('tracks').get('track')\
|
||||
.get('duration'),
|
||||
'title': data.get('tracks').get('track')\
|
||||
.get('name'),
|
||||
}
|
||||
]
|
||||
ret_obj["tracks"] = [
|
||||
{
|
||||
"duration": data.get("tracks")
|
||||
.get("track")
|
||||
.get("duration"),
|
||||
"title": data.get("tracks").get("track").get("name"),
|
||||
}
|
||||
]
|
||||
return ret_obj
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return {
|
||||
'err': 'Failed',
|
||||
}
|
||||
"err": "Failed",
|
||||
}
|
||||
|
@ -12,45 +12,48 @@ from typing import Union, Optional, LiteralString, Iterable
|
||||
from uuid import uuid4 as uuid
|
||||
from endpoints.constructors import RadioException
|
||||
|
||||
double_space: Pattern = regex.compile(r'\s{2,}')
|
||||
double_space: Pattern = regex.compile(r"\s{2,}")
|
||||
|
||||
|
||||
class RadioUtil:
|
||||
"""
|
||||
Radio Utils
|
||||
"""
|
||||
|
||||
def __init__(self, constants) -> None:
|
||||
self.constants = constants
|
||||
self.gpt = gpt.GPT(self.constants)
|
||||
self.ls_uri: str = self.constants.LS_URI
|
||||
self.sqlite_exts: list[str] = ['/home/api/api/solibs/spellfix1.cpython-311-x86_64-linux-gnu.so']
|
||||
self.active_playlist_path: Union[str, LiteralString] = os.path\
|
||||
.join("/usr/local/share",
|
||||
"sqlite_dbs", "track_file_map.db")
|
||||
self.active_playlist_name = "default" # not used
|
||||
self.ls_uri: str = self.constants.LS_URI
|
||||
self.sqlite_exts: list[str] = [
|
||||
"/home/api/api/solibs/spellfix1.cpython-311-x86_64-linux-gnu.so"
|
||||
]
|
||||
self.active_playlist_path: Union[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[dict] = []
|
||||
self.now_playing: dict = {
|
||||
'artist': 'N/A',
|
||||
'song': 'N/A',
|
||||
'album': 'N/A',
|
||||
'genre': 'N/A',
|
||||
'artistsong': 'N/A - N/A',
|
||||
'duration': 0,
|
||||
'start': 0,
|
||||
'end': 0,
|
||||
'file_path': None,
|
||||
'id': None,
|
||||
}
|
||||
"artist": "N/A",
|
||||
"song": "N/A",
|
||||
"album": "N/A",
|
||||
"genre": "N/A",
|
||||
"artistsong": "N/A - N/A",
|
||||
"duration": 0,
|
||||
"start": 0,
|
||||
"end": 0,
|
||||
"file_path": None,
|
||||
"id": None,
|
||||
}
|
||||
self.webhooks: dict = {
|
||||
'gpt': {
|
||||
'hook': self.constants.GPT_WEBHOOK,
|
||||
},
|
||||
'sfm': {
|
||||
'hook': self.constants.SFM_WEBHOOK,
|
||||
}
|
||||
}
|
||||
|
||||
def duration_conv(self,
|
||||
s: Union[int, float]) -> str:
|
||||
"gpt": {
|
||||
"hook": self.constants.GPT_WEBHOOK,
|
||||
},
|
||||
"sfm": {
|
||||
"hook": self.constants.SFM_WEBHOOK,
|
||||
},
|
||||
}
|
||||
|
||||
def duration_conv(self, s: Union[int, float]) -> str:
|
||||
"""
|
||||
Convert duration given in seconds to hours, minutes, and seconds (h:m:s)
|
||||
Args:
|
||||
@ -58,14 +61,12 @@ class RadioUtil:
|
||||
Returns:
|
||||
str
|
||||
"""
|
||||
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]]:
|
||||
if not query:
|
||||
return None
|
||||
async with sqlite3.connect(self.active_playlist_path,
|
||||
timeout=1) as _db:
|
||||
async with sqlite3.connect(self.active_playlist_path, timeout=1) as _db:
|
||||
_db.row_factory = sqlite3.Row
|
||||
db_query: str = """SELECT DISTINCT(LOWER(TRIM(artist) || " - " || TRIM(song))),\
|
||||
(TRIM(artist) || " - " || TRIM(song)) as artistsong FROM tracks WHERE\
|
||||
@ -73,14 +74,15 @@ class RadioUtil:
|
||||
db_params: tuple[str] = (f"%{query}%",)
|
||||
async with _db.execute(db_query, db_params) as _cursor:
|
||||
result: Iterable[sqlite3.Row] = await _cursor.fetchall()
|
||||
out_result = [
|
||||
str(r['artistsong']) for r in result
|
||||
]
|
||||
out_result = [str(r["artistsong"]) for r in result]
|
||||
return out_result
|
||||
|
||||
async def search_playlist(self, artistsong: Optional[str] = None,
|
||||
artist: Optional[str] = None,
|
||||
song: Optional[str] = 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:
|
||||
@ -95,9 +97,11 @@ class RadioUtil:
|
||||
if not artistsong and (not artist or not song):
|
||||
raise RadioException("No query provided")
|
||||
try:
|
||||
search_query: str = 'SELECT id, artist, song, (artist || " - " || song) AS artistsong, album, genre, file_path, duration FROM tracks\
|
||||
search_query: str = (
|
||||
'SELECT id, artist, song, (artist || " - " || song) AS artistsong, album, genre, file_path, duration FROM tracks\
|
||||
WHERE editdist3((lower(artist) || " " || lower(song)), (? || " " || ?))\
|
||||
<= 410 ORDER BY editdist3((lower(artist) || " " || lower(song)), ?) ASC LIMIT 1'
|
||||
)
|
||||
if artistsong:
|
||||
artistsong_split: list = artistsong.split(" - ", maxsplit=1)
|
||||
(search_artist, search_song) = tuple(artistsong_split)
|
||||
@ -106,26 +110,31 @@ class RadioUtil:
|
||||
search_song = song
|
||||
if not artistsong:
|
||||
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:
|
||||
search_params = (
|
||||
search_artist.lower(),
|
||||
search_song.lower(),
|
||||
artistsong.lower(),
|
||||
)
|
||||
async with sqlite3.connect(self.active_playlist_path, timeout=2) as db_conn:
|
||||
await db_conn.enable_load_extension(True)
|
||||
for ext in self.sqlite_exts:
|
||||
await db_conn.load_extension(ext)
|
||||
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()
|
||||
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 or not isinstance(result, sqlite3.Row):
|
||||
return False
|
||||
pushObj: dict = {
|
||||
'id': result['id'],
|
||||
'uuid': str(uuid().hex),
|
||||
'artist': result['artist'].strip(),
|
||||
'song': result['song'].strip(),
|
||||
'artistsong': result['artistsong'].strip(),
|
||||
'genre': result['genre'],
|
||||
'file_path': result['file_path'],
|
||||
'duration': result['duration'],
|
||||
"id": result["id"],
|
||||
"uuid": str(uuid().hex),
|
||||
"artist": result["artist"].strip(),
|
||||
"song": result["song"].strip(),
|
||||
"artistsong": result["artistsong"].strip(),
|
||||
"genre": result["genre"],
|
||||
"file_path": result["file_path"],
|
||||
"duration": result["duration"],
|
||||
}
|
||||
self.active_playlist.insert(0, pushObj)
|
||||
return True
|
||||
@ -133,7 +142,7 @@ class RadioUtil:
|
||||
logging.critical("search_playlist:: Search error occurred: %s", str(e))
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
async def load_playlist(self) -> None:
|
||||
"""Load Playlist"""
|
||||
try:
|
||||
@ -141,11 +150,11 @@ class RadioUtil:
|
||||
self.active_playlist.clear()
|
||||
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||
# GROUP BY artistdashsong ORDER BY RANDOM()'
|
||||
|
||||
|
||||
"""
|
||||
LIMITED GENRES
|
||||
"""
|
||||
|
||||
|
||||
db_query: str = """SELECT distinct(LOWER(TRIM(artist)) || " - " || LOWER(TRIM(song))), (TRIM(artist) || " - " || TRIM(song)) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||
WHERE (genre LIKE "%metalcore%"\
|
||||
OR genre LIKE "%math rock%"\
|
||||
@ -176,44 +185,57 @@ class RadioUtil:
|
||||
OR genre LIKE "%indie pop%"\
|
||||
OR genre LIKE "%dnb%")\
|
||||
GROUP BY artistdashsong ORDER BY RANDOM()"""
|
||||
|
||||
|
||||
"""
|
||||
LIMITED TO ONE/SMALL SUBSET OF GENRES
|
||||
"""
|
||||
|
||||
db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||
WHERE (artist LIKE "%winds of plague%" OR artist LIKE "%acacia st%" OR artist LIKE "%suicide si%" OR artist LIKE "%in dying%") AND (NOT song LIKE "%(live%") ORDER BY RANDOM()' #ORDER BY artist DESC, album ASC, song ASC'
|
||||
|
||||
|
||||
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||
# WHERE (artist LIKE "%winds of plague%" OR artist LIKE "%acacia st%" OR artist LIKE "%suicide si%" OR artist LIKE "%in dying%") AND (NOT song LIKE "%(live%") ORDER BY RANDOM()' #ORDER BY artist DESC, album ASC, song ASC'
|
||||
|
||||
"""
|
||||
LIMITED TO ONE/SOME ARTISTS...
|
||||
"""
|
||||
|
||||
|
||||
# db_query = 'SELECT distinct(artist || " - " || song) AS artistdashsong, id, artist, song, album, genre, file_path, duration FROM tracks\
|
||||
# WHERE (artist LIKE "%we butter%" OR artist LIKE "%eisbrecher%" OR artist LIKE "%black ang%" OR artist LIKE "%madison affair%") 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'
|
||||
|
||||
async with sqlite3.connect(self.active_playlist_path,
|
||||
timeout=2) as db_conn:
|
||||
# WHERE (artist LIKE "%rise against%" OR artist LIKE "%i prevail%" OR artist LIKE "%volumes%" OR artist LIKE "%movements%" OR artist LIKE "%woe%" OR artist LIKE "%smittyztop%" OR artist LIKE "%chunk! no,%" OR artist LIKE "%fame on fire%" OR artist LIKE "%our last night%" OR artist LIKE "%animal in me%") 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\
|
||||
# WHERE (artist LIKE "%%" OR artist LIKE "%belmont%" OR artist LIKE "%in dying arms%" OR artist LIKE "%iwrestleda%" OR artist LIKE "%winds of p%") 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\
|
||||
# WHERE (artist LIKE "%akira the don%") 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'
|
||||
|
||||
async with sqlite3.connect(
|
||||
f"file:{self.active_playlist_path}?mode=readonly", uri=True, timeout=2
|
||||
) as db_conn:
|
||||
db_conn.row_factory = sqlite3.Row
|
||||
async with await db_conn.execute(db_query) as db_cursor:
|
||||
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(),
|
||||
'song': double_space.sub(' ', r['song']).strip(),
|
||||
'album': double_space.sub(' ', r['album']).strip(),
|
||||
'genre': r['genre'] if r['genre'] else 'Unknown',
|
||||
'artistsong': double_space.sub(' ', r['artistdashsong']).strip(),
|
||||
'file_path': r['file_path'],
|
||||
'duration': r['duration'],
|
||||
} for r in results]
|
||||
logging.info("Populated active playlists with %s items",
|
||||
len(self.active_playlist))
|
||||
self.active_playlist = [
|
||||
{
|
||||
"uuid": str(uuid().hex),
|
||||
"id": r["id"],
|
||||
"artist": double_space.sub(" ", r["artist"]).strip(),
|
||||
"song": double_space.sub(" ", r["song"]).strip(),
|
||||
"album": double_space.sub(" ", r["album"]).strip(),
|
||||
"genre": r["genre"] if r["genre"] else "Unknown",
|
||||
"artistsong": double_space.sub(
|
||||
" ", r["artistdashsong"]
|
||||
).strip(),
|
||||
"file_path": r["file_path"],
|
||||
"duration": r["duration"],
|
||||
}
|
||||
for r in results
|
||||
]
|
||||
logging.info(
|
||||
"Populated active playlists with %s items",
|
||||
len(self.active_playlist),
|
||||
)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
||||
async def cache_album_art(self, track_id: int,
|
||||
album_art: bytes) -> None:
|
||||
|
||||
async def cache_album_art(self, track_id: int, album_art: bytes) -> None:
|
||||
"""
|
||||
Cache Album Art to SQLite DB
|
||||
Args:
|
||||
@ -223,16 +245,21 @@ class RadioUtil:
|
||||
None
|
||||
"""
|
||||
try:
|
||||
async with sqlite3.connect(self.active_playlist_path,
|
||||
timeout=2) as db_conn:
|
||||
async with await db_conn.execute("UPDATE tracks SET album_art = ? WHERE id = ?",
|
||||
(album_art, track_id,)) as db_cursor:
|
||||
async with sqlite3.connect(self.active_playlist_path, timeout=2) as db_conn:
|
||||
async with await db_conn.execute(
|
||||
"UPDATE tracks SET album_art = ? WHERE id = ?",
|
||||
(
|
||||
album_art,
|
||||
track_id,
|
||||
),
|
||||
) as db_cursor:
|
||||
await db_conn.commit()
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
||||
async def get_album_art(self, track_id: Optional[int] = None,
|
||||
file_path: Optional[str] = None) -> Optional[bytes]:
|
||||
|
||||
async def get_album_art(
|
||||
self, track_id: Optional[int] = None, file_path: Optional[str] = None
|
||||
) -> Optional[bytes]:
|
||||
"""
|
||||
Get Album Art
|
||||
Args:
|
||||
@ -242,28 +269,27 @@ class RadioUtil:
|
||||
bytes
|
||||
"""
|
||||
try:
|
||||
async with sqlite3.connect(self.active_playlist_path,
|
||||
timeout=2) as db_conn:
|
||||
async with sqlite3.connect(self.active_playlist_path, timeout=2) as db_conn:
|
||||
db_conn.row_factory = sqlite3.Row
|
||||
query: str = "SELECT album_art FROM tracks WHERE id = ?"
|
||||
query_params: tuple = (track_id,)
|
||||
|
||||
|
||||
if file_path and not track_id:
|
||||
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[Union[sqlite3.Row, bool]] = await db_cursor.fetchone()
|
||||
|
||||
async with await db_conn.execute(query, query_params) as db_cursor:
|
||||
result: Optional[Union[sqlite3.Row, bool]] = (
|
||||
await db_cursor.fetchone()
|
||||
)
|
||||
if not result or not isinstance(result, sqlite3.Row):
|
||||
return None
|
||||
return result['album_art']
|
||||
return result["album_art"]
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
def get_queue_item_by_uuid(self,
|
||||
uuid: str) -> Optional[tuple[int, dict]]:
|
||||
|
||||
def get_queue_item_by_uuid(self, uuid: str) -> Optional[tuple[int, dict]]:
|
||||
"""
|
||||
Get queue item by UUID
|
||||
Args:
|
||||
@ -272,10 +298,10 @@ class RadioUtil:
|
||||
Optional[tuple[int, dict]]
|
||||
"""
|
||||
for x, item in enumerate(self.active_playlist):
|
||||
if item.get('uuid') == uuid:
|
||||
if item.get("uuid") == uuid:
|
||||
return (x, item)
|
||||
return None
|
||||
|
||||
|
||||
async def _ls_skip(self) -> bool:
|
||||
"""
|
||||
Ask LiquidSoap server to skip to the next track
|
||||
@ -286,18 +312,18 @@ class RadioUtil:
|
||||
"""
|
||||
try:
|
||||
async with ClientSession() as session:
|
||||
async with session.get(f"{self.ls_uri}/next",
|
||||
timeout=ClientTimeout(connect=2, sock_read=2)) as request:
|
||||
request.raise_for_status()
|
||||
text: Optional[str] = await request.text()
|
||||
return text == "OK"
|
||||
async with session.get(
|
||||
f"{self.ls_uri}/next", timeout=ClientTimeout(connect=2, sock_read=2)
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
text: Optional[str] = await request.text()
|
||||
return text == "OK"
|
||||
except Exception as e:
|
||||
logging.debug("Skip failed: %s", str(e))
|
||||
|
||||
return False # failsafe
|
||||
|
||||
async def get_ai_song_info(self, artist: str,
|
||||
song: str) -> Optional[str]:
|
||||
logging.debug("Skip failed: %s", str(e))
|
||||
|
||||
return False # failsafe
|
||||
|
||||
async def get_ai_song_info(self, artist: str, song: str) -> Optional[str]:
|
||||
"""
|
||||
Get AI Song Info
|
||||
Args:
|
||||
@ -312,44 +338,50 @@ class RadioUtil:
|
||||
logging.critical("No response received from GPT?")
|
||||
return None
|
||||
return response
|
||||
|
||||
|
||||
async def webhook_song_change(self, track: dict) -> None:
|
||||
try:
|
||||
"""
|
||||
Handles Song Change Outbounds (Webhooks)
|
||||
Args:
|
||||
Args:
|
||||
track (dict)
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
|
||||
|
||||
|
||||
# First, send track info
|
||||
friendly_track_start: str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(track['start']))
|
||||
friendly_track_end: str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(track['end']))
|
||||
friendly_track_start: str = time.strftime(
|
||||
"%Y-%m-%d %H:%M:%S", time.localtime(track["start"])
|
||||
)
|
||||
friendly_track_end: str = time.strftime(
|
||||
"%Y-%m-%d %H:%M:%S", time.localtime(track["end"])
|
||||
)
|
||||
hook_data: dict = {
|
||||
'username': 'serious.FM',
|
||||
"embeds": [{
|
||||
"username": "serious.FM",
|
||||
"embeds": [
|
||||
{
|
||||
"title": "Now Playing",
|
||||
"description": f'## {track['song']}\nby\n## {track['artist']}',
|
||||
"color": 0x30c56f,
|
||||
"description": f"## {track['song']}\nby\n## {track['artist']}",
|
||||
"color": 0x30C56F,
|
||||
"thumbnail": {
|
||||
"url": f"https://api.codey.lol/radio/album_art?track_id={track['id']}&{int(time.time())}",
|
||||
},
|
||||
"fields": [
|
||||
{
|
||||
"name": "Duration",
|
||||
"value": self.duration_conv(track['duration']),
|
||||
"value": self.duration_conv(track["duration"]),
|
||||
"inline": True,
|
||||
},
|
||||
{
|
||||
"name": "Genre",
|
||||
"value": track['genre'] if track['genre'] else 'Unknown',
|
||||
"value": (
|
||||
track["genre"] if track["genre"] else "Unknown"
|
||||
),
|
||||
"inline": True,
|
||||
},
|
||||
{
|
||||
"name": "Filetype",
|
||||
"value": track['file_path'].rsplit(".", maxsplit=1)[1],
|
||||
"value": track["file_path"].rsplit(".", maxsplit=1)[1],
|
||||
"inline": True,
|
||||
},
|
||||
{
|
||||
@ -359,42 +391,56 @@ class RadioUtil:
|
||||
},
|
||||
{
|
||||
"name": "Album",
|
||||
"value": track['album'] if track['album'] else "Unknown",
|
||||
"value": (
|
||||
track["album"] if track["album"] else "Unknown"
|
||||
),
|
||||
},
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
sfm_hook: str = self.webhooks['sfm'].get('hook')
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
sfm_hook: str = self.webhooks["sfm"].get("hook")
|
||||
async with ClientSession() as session:
|
||||
async with await session.post(sfm_hook, json=hook_data,
|
||||
timeout=ClientTimeout(connect=5, sock_read=5), headers={
|
||||
'content-type': 'application/json; charset=utf-8',}) as request:
|
||||
request.raise_for_status()
|
||||
|
||||
# Next, AI feedback
|
||||
|
||||
ai_response: Optional[str] = await self.get_ai_song_info(track['artist'],
|
||||
track['song'])
|
||||
async with await session.post(
|
||||
sfm_hook,
|
||||
json=hook_data,
|
||||
timeout=ClientTimeout(connect=5, sock_read=5),
|
||||
headers={
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
},
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
|
||||
# Next, AI feedback
|
||||
|
||||
ai_response: Optional[str] = await self.get_ai_song_info(
|
||||
track["artist"], track["song"]
|
||||
)
|
||||
if not ai_response:
|
||||
return
|
||||
|
||||
|
||||
hook_data = {
|
||||
'username': 'GPT',
|
||||
"embeds": [{
|
||||
"username": "GPT",
|
||||
"embeds": [
|
||||
{
|
||||
"title": "AI Feedback",
|
||||
"color": 0x35d0ff,
|
||||
"color": 0x35D0FF,
|
||||
"description": ai_response.strip(),
|
||||
}]
|
||||
}
|
||||
|
||||
ai_hook: str = self.webhooks['gpt'].get('hook')
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
ai_hook: str = self.webhooks["gpt"].get("hook")
|
||||
async with ClientSession() as session:
|
||||
async with await session.post(ai_hook, json=hook_data,
|
||||
timeout=ClientTimeout(connect=5, sock_read=5), headers={
|
||||
'content-type': 'application/json; charset=utf-8',}) as request:
|
||||
request.raise_for_status()
|
||||
async with await session.post(
|
||||
ai_hook,
|
||||
json=hook_data,
|
||||
timeout=ClientTimeout(connect=5, sock_read=5),
|
||||
headers={
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
},
|
||||
) as request:
|
||||
request.raise_for_status()
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
|
Reference in New Issue
Block a user