From 417c76e68cdf54c40ba4ab51edad590b65534013 Mon Sep 17 00:00:00 2001 From: Broque Thomas Date: Mon, 1 Sep 2025 20:34:40 -0700 Subject: [PATCH] move to backend --- web_server.py | 31 +++++++++++++++++ webui/static/script.js | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/web_server.py b/web_server.py index 77cf8151..7980b61d 100644 --- a/web_server.py +++ b/web_server.py @@ -5730,6 +5730,37 @@ def get_tidal_discovery_status(playlist_id): print(f"❌ Error getting Tidal discovery status: {e}") return jsonify({"error": str(e)}), 500 +@app.route('/api/tidal/playlists/states', methods=['GET']) +def get_tidal_playlist_states(): + """Get all stored Tidal playlist discovery states for frontend hydration (similar to YouTube playlists)""" + try: + states = [] + current_time = time.time() + + for playlist_id, state in tidal_discovery_states.items(): + # Update access time when requested + state['last_accessed'] = current_time + + # Return essential data for card state recreation + state_info = { + 'playlist_id': playlist_id, + 'phase': state['phase'], + 'status': state['status'], + 'discovery_progress': state['discovery_progress'], + 'spotify_matches': state['spotify_matches'], + 'spotify_total': state['spotify_total'], + 'discovery_results': state['discovery_results'], + 'last_accessed': state['last_accessed'] + } + states.append(state_info) + + print(f"🎵 Returning {len(states)} stored Tidal playlist states for hydration") + return jsonify({"states": states}) + + except Exception as e: + print(f"❌ Error getting Tidal playlist states: {e}") + return jsonify({"error": str(e)}), 500 + def _run_tidal_discovery_worker(playlist_id): """Background worker for Tidal Spotify discovery process (like sync.py)""" diff --git a/webui/static/script.js b/webui/static/script.js index 59fb2d58..31c1b028 100644 --- a/webui/static/script.js +++ b/webui/static/script.js @@ -6350,6 +6350,9 @@ async function loadTidalPlaylists() { tidalPlaylistsLoaded = true; console.log(`🎵 Loaded ${tidalPlaylists.length} Tidal playlists`); + + // Load and apply saved discovery states from backend (like YouTube) + await loadTidalPlaylistStatesFromBackend(); } catch (error) { container.innerHTML = `
❌ Error: ${error.message}
`; @@ -6623,6 +6626,78 @@ function startTidalDiscoveryPolling(fakeUrlHash, playlistId) { activeYouTubePollers[fakeUrlHash] = pollInterval; } +async function loadTidalPlaylistStatesFromBackend() { + // Load all stored Tidal playlist discovery states from backend (similar to YouTube hydration) + try { + console.log('🎵 Loading Tidal playlist states from backend...'); + + const response = await fetch('/api/tidal/playlists/states'); + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || 'Failed to fetch Tidal playlist states'); + } + + const data = await response.json(); + const states = data.states || []; + + console.log(`🎵 Found ${states.length} stored Tidal playlist states in backend`); + + if (states.length === 0) { + console.log('🎵 No Tidal playlist states to hydrate'); + return; + } + + // Apply states to existing playlist cards + for (const stateInfo of states) { + await applyTidalPlaylistState(stateInfo); + } + + console.log('✅ Tidal playlist states loaded and applied'); + + } catch (error) { + console.error('❌ Error loading Tidal playlist states:', error); + } +} + +async function applyTidalPlaylistState(stateInfo) { + const { playlist_id, phase, discovery_progress, spotify_matches, discovery_results } = stateInfo; + + try { + console.log(`🎵 Applying saved state for Tidal playlist: ${playlist_id}, Phase: ${phase}`); + + // Find the playlist data from the loaded playlists + const playlistData = tidalPlaylists.find(p => p.id === playlist_id); + if (!playlistData) { + console.warn(`⚠️ Playlist data not found for state ${playlist_id} - skipping`); + return; + } + + // Update local state + if (!tidalPlaylistStates[playlist_id]) { + // Initialize state if it doesn't exist + tidalPlaylistStates[playlist_id] = { + playlist: playlistData, + phase: 'fresh' + }; + } + + // Update with backend state + tidalPlaylistStates[playlist_id].phase = phase; + tidalPlaylistStates[playlist_id].discovery_progress = discovery_progress; + tidalPlaylistStates[playlist_id].spotify_matches = spotify_matches; + tidalPlaylistStates[playlist_id].discovery_results = discovery_results; + tidalPlaylistStates[playlist_id].playlist = playlistData; // Ensure playlist data is set + + // Update the card UI to reflect the saved state + updateTidalCardPhase(playlist_id, phase); + + console.log(`✅ Applied saved state for Tidal playlist: ${playlist_id} -> ${phase}`); + + } catch (error) { + console.error(`❌ Error applying Tidal playlist state for ${playlist_id}:`, error); + } +} + // Tidal-specific sync and download functions (placeholder implementations) function startTidalPlaylistSync(urlHash) { console.log(`🎵 Starting Tidal playlist sync for: ${urlHash}`);