From 255aae6459beda742553318b9e705fc2d4fe1f3d Mon Sep 17 00:00:00 2001 From: zebra Date: Mon, 9 Jun 2025 15:37:57 -0700 Subject: [PATCH] Search functions now have verbosity --- musicfetch | 107 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 19 deletions(-) diff --git a/musicfetch b/musicfetch index 4f3f285..b5671cc 100644 --- a/musicfetch +++ b/musicfetch @@ -88,14 +88,21 @@ def search_artist(name, timeout_seconds=15): return None def get_existing_artist(name): + print(f"--> Checking if artist '{name}' already exists in Lidarr...") try: - resp = requests.get(f"{LIDARR_URL}/api/v1/artist", headers=headers) + print("...Sending request to /api/v1/artist") + resp = requests.get(f"{LIDARR_URL}/api/v1/artist", headers=headers, timeout=10) + print("...Got response from Lidarr") resp.raise_for_status() for artist in resp.json(): if artist["artistName"].lower() == name.lower(): + print("...Artist match found!") return artist - except requests.RequestException as e: - print(f"Failed to get existing artists: {e}") + print("...Artist not found in existing list.") + except requests.exceptions.Timeout: + print("!!! Timeout during existing artist check.") + except requests.exceptions.RequestException as e: + print(f"!!! Exception during existing artist check: {e}") return None def add_artist(metadata_artist): @@ -125,25 +132,87 @@ def add_artist(metadata_artist): resp.raise_for_status() return resp.json() +def search_local_album(track_name, artist_id=None, artist_name=None): + try: + print(f"--> Searching local albums for track: '{track_name}'") + resp = requests.get( + f"{LIDARR_URL}/api/v1/album", + headers=headers, + timeout=10 + ) + resp.raise_for_status() + albums = resp.json() + print(f"...Loaded {len(albums)} local albums") + + # Search by artist_id and track_name + for album in albums: + # Check artist ID match if provided + if artist_id and album.get("artistId") != artist_id: + continue + # Check artist name match if provided + if artist_name and album.get("artist", {}).get("artistName", "").lower() != artist_name.lower(): + continue + # Check if track_name matches album title (case insensitive substring) + if track_name.lower() in album.get("title", "").lower(): + print(f"!!! Found local album: {album.get('title')} by {album.get('artist', {}).get('artistName')}") + return album + print("!!! No matching local album found.") + except requests.exceptions.Timeout: + print("!!! Timeout during local album search.") + except requests.exceptions.RequestException as e: + print(f"!!! Exception during local album search: {e}") + return None + def search_album(track_name, artist_id): - resp = requests.get( - f"{LIDARR_URL}/api/v1/album/lookup", - headers=headers, - params={"term": track_name, "artistId": artist_id} - ) - resp.raise_for_status() - results = resp.json() - return results[0] if results else None + # Check local albums first + album = search_local_album(track_name, artist_id=artist_id) + if album: + return album + + # Fallback to external lookup + try: + print(f"--> Searching album externally with track: '{track_name}' and artist ID: {artist_id}") + resp = requests.get( + f"{LIDARR_URL}/api/v1/album/lookup", + headers=headers, + params={"term": track_name, "artistId": artist_id}, + timeout=10 + ) + resp.raise_for_status() + results = resp.json() + print(f"...Found {len(results)} results from album lookup with artist ID.") + return results[0] if results else None + except requests.exceptions.Timeout: + print("!!! Timeout during album search (with artist ID).") + except requests.exceptions.RequestException as e: + print(f"!!! Exception during album search (with artist ID): {e}") + return None def search_album_by_artist(track_name, artist_name): - resp = requests.get( - f"{LIDARR_URL}/api/v1/album/lookup", - headers=headers, - params={"term": f"{artist_name} {track_name}"} - ) - resp.raise_for_status() - results = resp.json() - return results[0] if results else None + # Check local albums first + album = search_local_album(track_name, artist_name=artist_name) + if album: + return album + + # Fallback to external lookup + try: + print(f"--> Fallback external search: '{artist_name} {track_name}'") + resp = requests.get( + f"{LIDARR_URL}/api/v1/album/lookup", + headers=headers, + params={"term": f"{artist_name} {track_name}"}, + timeout=10 + ) + resp.raise_for_status() + results = resp.json() + print(f"...Found {len(results)} results from fallback search.") + return results[0] if results else None + except requests.exceptions.Timeout: + print("!!! Timeout during fallback album search.") + except requests.exceptions.RequestException as e: + print(f"!!! Exception during fallback album search: {e}") + return None + def trigger_album_search(album_id): data = {