Update sync_service.py

This commit is contained in:
Broque Thomas 2025-08-06 23:20:46 -07:00
parent 86fcdd6869
commit 2e4d8be0ac

View file

@ -206,99 +206,49 @@ class PlaylistSyncService:
self._cancelled = False self._cancelled = False
async def _find_track_in_plex(self, spotify_track: SpotifyTrack) -> Tuple[Optional[PlexTrackInfo], float]: async def _find_track_in_plex(self, spotify_track: SpotifyTrack) -> Tuple[Optional[PlexTrackInfo], float]:
"""Find a track in Plex using the same robust search approach as Download Missing Tracks""" """Find a track using the same improved database matching as Download Missing Tracks modal"""
try: try:
if not self.plex_client or not self.plex_client.is_connected(): if not self.plex_client or not self.plex_client.is_connected():
logger.warning("Plex client not connected") logger.warning("Plex client not connected")
return None, 0.0 return None, 0.0
# Use same robust search logic as PlaylistTrackAnalysisWorker # Use the SAME improved database matching as PlaylistTrackAnalysisWorker
from database.music_database import MusicDatabase
original_title = spotify_track.name original_title = spotify_track.name
# Create title variations # Try each artist (same as modal logic)
unique_title_variations = [] for artist in spotify_track.artists:
original_clean = self.matching_engine.get_core_string(original_title)
unique_title_variations.append(original_clean)
# Add cleaned version
cleaned_version = self.matching_engine.clean_title(original_title)
if cleaned_version != original_clean:
unique_title_variations.append(cleaned_version)
all_potential_matches = []
found_match_ids = set()
# Search by artist + title combinations
for artist in spotify_track.artists[:2]: # Limit to first 2 artists
if self._cancelled: if self._cancelled:
return None, 0.0 return None, 0.0
artist_name = self.matching_engine.clean_artist(artist) artist_name = artist if isinstance(artist, str) else artist
for query_title in unique_title_variations: # Use the improved database check_track_exists method
if self._cancelled: try:
logger.debug(f"Sync cancelled during track search for '{original_title}'") db = MusicDatabase()
return None, 0.0 db_track, confidence = db.check_track_exists(original_title, artist_name, confidence_threshold=0.7)
potential_plex_matches = self.plex_client.search_tracks( if db_track and confidence >= 0.7:
title=query_title, logger.debug(f"✔️ Database match found for '{original_title}' by '{artist_name}': '{db_track.title}' with confidence {confidence:.2f}")
artist=artist_name,
limit=15
)
# Check cancellation after each search operation # Convert database track to format compatible with existing code
if self._cancelled: class MockPlexTrack:
logger.debug(f"Sync cancelled after search for '{original_title}'") def __init__(self, db_track):
return None, 0.0 self.id = str(db_track.id)
self.title = db_track.title
self.artist = db_track.artist_name
self.album = db_track.album_title
self.duration = db_track.duration
for track in potential_plex_matches: return MockPlexTrack(db_track), confidence
if track.id not in found_match_ids:
all_potential_matches.append(track)
found_match_ids.add(track.id)
# Early exit check for confident match except Exception as db_error:
if all_potential_matches: logger.error(f"Error checking track existence for '{original_title}' by '{artist_name}': {db_error}")
match_result = self.matching_engine.find_best_match(spotify_track, all_potential_matches) continue
if match_result.is_match:
logger.debug(f"Early confident match found for '{original_title}'")
return match_result.plex_track, match_result.confidence
# Fallback: Title-only search logger.debug(f"❌ No database match found for '{original_title}' by any of the artists {spotify_track.artists}")
if not all_potential_matches: return None, 0.0
if self._cancelled:
logger.debug(f"Sync cancelled before title-only search for '{original_title}'")
return None, 0.0
logger.debug(f"No artist-based matches found. Using title-only fallback for '{original_title}'")
for query_title in unique_title_variations:
if self._cancelled:
logger.debug(f"Sync cancelled during title-only search for '{original_title}'")
return None, 0.0
title_only_matches = self.plex_client.search_tracks(title=query_title, artist="", limit=10)
if self._cancelled:
logger.debug(f"Sync cancelled after title-only search for '{original_title}'")
return None, 0.0
for track in title_only_matches:
if track.id not in found_match_ids:
all_potential_matches.append(track)
found_match_ids.add(track.id)
if not all_potential_matches:
logger.debug(f"No Plex candidates found for '{original_title}'")
return None, 0.0
# Final scoring
final_match_result = self.matching_engine.find_best_match(spotify_track, all_potential_matches)
if final_match_result.is_match:
logger.debug(f"Match found for '{original_title}': '{final_match_result.plex_track.title}' (confidence: {final_match_result.confidence:.2f})")
else:
logger.debug(f"No confident match for '{original_title}' (best score: {final_match_result.confidence:.2f})")
return final_match_result.plex_track, final_match_result.confidence
except Exception as e: except Exception as e:
logger.error(f"Error searching for track '{spotify_track.name}': {e}") logger.error(f"Error searching for track '{spotify_track.name}': {e}")