diff --git a/base.py b/base.py index 4845c3a..80a0086 100644 --- a/base.py +++ b/base.py @@ -9,9 +9,10 @@ from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from lyric_search.sources import redis_cache +logging.basicConfig(level=logging.INFO) +logging.getLogger("aiosqlite").setLevel(logging.WARNING) +logging.getLogger("httpx").setLevel(logging.WARNING) logger = logging.getLogger() -logger.setLevel(logging.INFO) - loop = asyncio.get_event_loop() app = FastAPI( @@ -22,7 +23,6 @@ app = FastAPI( loop=loop, ) - constants = importlib.import_module("constants").Constants() util = importlib.import_module("util").Utilities(app, constants) diff --git a/endpoints/rip.py b/endpoints/rip.py index b64da58..109d208 100644 --- a/endpoints/rip.py +++ b/endpoints/rip.py @@ -5,8 +5,6 @@ from fastapi.responses import JSONResponse from utils.sr_wrapper import SRUtil from auth.deps import get_current_user -logging.getLogger().setLevel(logging.INFO) - class RIP(FastAPI): """ diff --git a/utils/sr_wrapper.py b/utils/sr_wrapper.py index aa6242a..4bf8529 100644 --- a/utils/sr_wrapper.py +++ b/utils/sr_wrapper.py @@ -1,10 +1,10 @@ from typing import Optional import logging import os +import asyncio from streamrip.client import TidalClient from streamrip.config import Config as StreamripConfig from dotenv import load_dotenv - load_dotenv() @@ -38,6 +38,7 @@ class SRUtil: ) self.streamrip_config self.streamrip_client = TidalClient(self.streamrip_config) + asyncio.get_event_loop().create_task(self.streamrip_client.login()) def dedupe_by_key(self, key: str, entries: list[dict]) -> list[dict]: deduped = {} @@ -111,7 +112,7 @@ class SRUtil: if not metadata: logging.warning("No metadata found for artist ID: %s", artist_id) return None - albums = metadata.get("albums", []) + albums = self.dedupe_by_key("title", metadata.get("albums", [])) albums_out = [ { "artist": ", ".join(artist["name"] for artist in album["artists"]), @@ -123,7 +124,7 @@ class SRUtil: if "title" in album and "id" in album and "artists" in album ] - logging.info("Retrieved albums: %s", albums_out) + logging.debug("Retrieved albums: %s", albums_out) return albums_out async def get_tracks_by_album_id(self, album_id: int) -> Optional[list | dict]: @@ -178,10 +179,29 @@ class SRUtil: Returns: Optional[str]: The stream URL or None if not found. """ - track_id_str = str(track_id) - track = await self.streamrip_client.get_downloadable( - track_id=track_id_str, quality=self.streamrip_config.session.tidal.quality - ) + if quality not in ["LOSSLESS", "HIGH", "LOW"]: + logging.error("Invalid quality requested: %s", + quality) + quality_int: int = int(self.streamrip_config.session.tidal.quality) + match quality: + case "HIGH": + quality_int = 1 + case "LOW": + quality_int = 0 + track_id_str: str = str(track_id) + + if not self.streamrip_client.logged_in: + await self.streamrip_client.login() + + try: + track = await self.streamrip_client.get_downloadable( + track_id=track_id_str, quality=quality_int + ) + except AttributeError: + await self.streamrip_client.login() + track = await self.streamrip_client.get_downloadable( + track_id=track_id_str, quality=quality_int + ) if not track: logging.warning("No track found for ID: %s", track_id) return None