Upload main.js
Browse files148 unmodified lines1491501511521531541552 unmodified lines158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231148 unmodified linessetLoadingState(true, true);try {// Call API to generate podcastconst response = await fetch('/generate-podcast', {method: 'POST',headers: {2 unmodified linesbody: JSON.stringify(scenario)});// Setup progress event handlingconst reader = response.body.getReader();const contentLength = +response.headers.get('Content-Length') || 0;// Process the streamlet receivedLength = 0;let jsonData = '';// Monitor the generation progressconst progressMonitor = setInterval(() => {fetch('/generation-progress').then(res => res.json()).then(progress => {if (progress.status === 'in_progress') {updateProgress(progress.current, progress.total, progress.message);}}).catch(err => console.error('Error fetching progress:', err));}, 1000);while (true) {const { done, value } = await reader.read();if (done) {clearInterval(progressMonitor);break;}receivedLength += value.length;const chunk = new TextDecoder().decode(value);jsonData += chunk;// Update progress if we have content lengthif (contentLength) {const percentComplete = Math.round((receivedLength / contentLength) * 100);updateProgress(percentComplete, 100, 'Réception des données...');}}// Parse the responseconst data = JSON.parse(jsonData);if (!response.ok) {throw new Error(data.error || 'Échec de génération du podcast');}// Process successful responsecurrentAudioUrl = data.audioUrl;currentAudioFilename = currentAudioUrl.split('/').pop();// Setup audio playeraudioPlayer.src = currentAudioUrl;audioPlayer.load();// Show audio cardaudioCard.classList.remove('d-none');audioCard.classList.add('fade-in');// Auto-play the audiotry {await audioPlayer.play();} catch (playError) {console.log('Lecture automatique empêchée par le navigateur.', playError);}} catch (error) {showError(error.message || 'Une erreur inattendue est survenue. Veuillez réessayer.');} finally {setLoadingState(false);}});148 unmodified lines1491501511521531541552 unmodified lines158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224148 unmodified linessetLoadingState(true, true);try {// Call API to start podcast generation in the backgroundconst response = await fetch('/generate-podcast', {method: 'POST',headers: {2 unmodified linesbody: JSON.stringify(scenario)});const data = await response.json();if (!response.ok) {throw new Error(data.error || 'Échec de démarrage de la génération du podcast');}// Start polling for podcast statusconst statusPoller = setInterval(async () => {try {const statusResponse = await fetch('/podcast-status');const statusData = await statusResponse.json();if (statusData.status === 'in_progress') {// Update progress barupdateProgress(statusData.current,statusData.total,statusData.message || 'Génération en cours...');} else if (statusData.status === 'complete') {// Podcast is completeclearInterval(statusPoller);// Process successful responseif (statusData.audioUrl) {currentAudioUrl = statusData.audioUrl;currentAudioFilename = currentAudioUrl.split('/').pop();// Setup audio playeraudioPlayer.src = currentAudioUrl;audioPlayer.load();// Show audio cardaudioCard.classList.remove('d-none');audioCard.classList.add('fade-in');// Auto-play the audiotry {await audioPlayer.play();} catch (playError) {console.log('Lecture automatique empêchée par le navigateur.', playError);}// Turn off loading statesetLoadingState(false);} else {throw new Error('Podcast généré mais URL audio manquante');}} else if (statusData.status === 'error' || statusData.message.startsWith('Erreur')) {// Error occurredclearInterval(statusPoller);throw new Error(statusData.message || 'Erreur pendant la génération du podcast');}} catch (pollError) {clearInterval(statusPoller);throw pollError;}}, 1000);} catch (error) {showError(error.message || 'Une erreur inattendue est survenue. Veuillez réessayer.');setLoadingState(false);}});
static/js/main.js+55-62