From bf531add0d1580f207988de083864f22c866ed53 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 10 Jan 2026 23:38:05 +0000 Subject: [PATCH] Optimize artist search to avoid redundant API calls Modified `_search_artist` to return both artist ID and name, eliminating the need for a separate `_get_artist_name` call in `_process_artist`. Updated unit tests to reflect the method signature change. --- src/artist_tracker/tracker.py | 19 ++++++++----------- tests/test_tracker.py | 16 ++++++++++------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/artist_tracker/tracker.py b/src/artist_tracker/tracker.py index 3c31a8f..fd4f2b9 100644 --- a/src/artist_tracker/tracker.py +++ b/src/artist_tracker/tracker.py @@ -290,15 +290,15 @@ def _is_noise(self, title: str) -> bool: return True return False - def _search_artist(self, artist_name: str) -> Optional[str]: + def _search_artist(self, artist_name: str) -> Tuple[Optional[str], Optional[str]]: """ - Search for an artist by name and return their ID with retry logic. + Search for an artist by name and return their ID and official name with retry logic. Args: artist_name: Name of the artist to search Returns: - Artist ID or None if not found + Tuple of (Artist ID, Official Name). Both None if not found. Raises: SpotifyAPIError: If API call fails after retries @@ -316,11 +316,12 @@ def search_call(): if results['artists']['items']: artist = results['artists']['items'][0] artist_id = artist['id'] - logger.info(f"Found artist '{artist['name']}' (ID: {artist_id})") - return artist_id + official_name = artist['name'] + logger.info(f"Found artist '{official_name}' (ID: {artist_id})") + return artist_id, official_name else: logger.warning(f"No results found for artist '{artist_name}'") - return None + return None, None except (SpotifyAPIError, RateLimitError) as e: logger.error(f"API error searching for artist '{artist_name}': {e}") @@ -672,15 +673,11 @@ def _process_artist( # Get artist ID if we have a name if artist_name and not artist_id: - artist_id = self._search_artist(artist_name) + artist_id, official_name = self._search_artist(artist_name) if not artist_id: return artist_name, [] # Return name for missing artists tracking # Update artist_name to the official one from Spotify to fix capitalization - # We already did a search, so ideally we'd get the name from that result, - # but _search_artist only returns the ID. - # Let's fetch the official name using the ID. - official_name = self._get_artist_name(artist_id) if official_name: artist_name = official_name diff --git a/tests/test_tracker.py b/tests/test_tracker.py index 3b9ede8..4923a4c 100644 --- a/tests/test_tracker.py +++ b/tests/test_tracker.py @@ -153,8 +153,9 @@ def test_search_artist_success(self): } } - artist_id = self.tracker._search_artist('Taylor Swift') + artist_id, artist_name = self.tracker._search_artist('Taylor Swift') self.assertEqual(artist_id, 'artist123') + self.assertEqual(artist_name, 'Taylor Swift') self.tracker.sp.search.assert_called_once() def test_search_artist_not_found(self): @@ -166,8 +167,9 @@ def test_search_artist_not_found(self): } } - artist_id = self.tracker._search_artist('NonexistentArtist') + artist_id, artist_name = self.tracker._search_artist('NonexistentArtist') self.assertIsNone(artist_id) + self.assertIsNone(artist_name) def test_get_artist_name_success(self): """Test getting artist name by ID.""" @@ -449,9 +451,10 @@ def test_retry_on_server_error(self, mock_sleep): {'artists': {'items': [{'id': 'artist123', 'name': 'Test'}]}} ] - result = self.tracker._search_artist('Test Artist') + artist_id, artist_name = self.tracker._search_artist('Test Artist') - self.assertEqual(result, 'artist123') + self.assertEqual(artist_id, 'artist123') + self.assertEqual(artist_name, 'Test') self.assertEqual(self.tracker.sp.search.call_count, 2) mock_sleep.assert_called() # Should have slept between retries @@ -469,9 +472,10 @@ def test_retry_on_rate_limit(self, mock_sleep): {'artists': {'items': [{'id': 'artist123', 'name': 'Test'}]}} ] - result = self.tracker._search_artist('Test Artist') + artist_id, artist_name = self.tracker._search_artist('Test Artist') - self.assertEqual(result, 'artist123') + self.assertEqual(artist_id, 'artist123') + self.assertEqual(artist_name, 'Test') self.assertEqual(self.tracker.sp.search.call_count, 2) def test_client_error_no_retry(self):