Upload main.js
Browse files- static/js/main.js +55 -62
static/js/main.js
CHANGED
|
@@ -149,7 +149,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
| 149 |
setLoadingState(true, true);
|
| 150 |
|
| 151 |
try {
|
| 152 |
-
// Call API to
|
| 153 |
const response = await fetch('/generate-podcast', {
|
| 154 |
method: 'POST',
|
| 155 |
headers: {
|
|
@@ -158,74 +158,67 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
| 158 |
body: JSON.stringify(scenario)
|
| 159 |
});
|
| 160 |
|
| 161 |
-
|
| 162 |
-
const reader = response.body.getReader();
|
| 163 |
-
const contentLength = +response.headers.get('Content-Length') || 0;
|
| 164 |
-
|
| 165 |
-
// Process the stream
|
| 166 |
-
let receivedLength = 0;
|
| 167 |
-
let jsonData = '';
|
| 168 |
-
|
| 169 |
-
// Monitor the generation progress
|
| 170 |
-
const progressMonitor = setInterval(() => {
|
| 171 |
-
fetch('/generation-progress')
|
| 172 |
-
.then(res => res.json())
|
| 173 |
-
.then(progress => {
|
| 174 |
-
if (progress.status === 'in_progress') {
|
| 175 |
-
updateProgress(progress.current, progress.total, progress.message);
|
| 176 |
-
}
|
| 177 |
-
})
|
| 178 |
-
.catch(err => console.error('Error fetching progress:', err));
|
| 179 |
-
}, 1000);
|
| 180 |
-
|
| 181 |
-
while (true) {
|
| 182 |
-
const { done, value } = await reader.read();
|
| 183 |
-
|
| 184 |
-
if (done) {
|
| 185 |
-
clearInterval(progressMonitor);
|
| 186 |
-
break;
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
receivedLength += value.length;
|
| 190 |
-
const chunk = new TextDecoder().decode(value);
|
| 191 |
-
jsonData += chunk;
|
| 192 |
-
|
| 193 |
-
// Update progress if we have content length
|
| 194 |
-
if (contentLength) {
|
| 195 |
-
const percentComplete = Math.round((receivedLength / contentLength) * 100);
|
| 196 |
-
updateProgress(percentComplete, 100, 'Réception des données...');
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
// Parse the response
|
| 201 |
-
const data = JSON.parse(jsonData);
|
| 202 |
|
| 203 |
if (!response.ok) {
|
| 204 |
-
throw new Error(data.error || 'Échec de génération du podcast');
|
| 205 |
}
|
| 206 |
|
| 207 |
-
//
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
} catch (error) {
|
| 227 |
showError(error.message || 'Une erreur inattendue est survenue. Veuillez réessayer.');
|
| 228 |
-
} finally {
|
| 229 |
setLoadingState(false);
|
| 230 |
}
|
| 231 |
});
|
|
|
|
| 149 |
setLoadingState(true, true);
|
| 150 |
|
| 151 |
try {
|
| 152 |
+
// Call API to start podcast generation in the background
|
| 153 |
const response = await fetch('/generate-podcast', {
|
| 154 |
method: 'POST',
|
| 155 |
headers: {
|
|
|
|
| 158 |
body: JSON.stringify(scenario)
|
| 159 |
});
|
| 160 |
|
| 161 |
+
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
if (!response.ok) {
|
| 164 |
+
throw new Error(data.error || 'Échec de démarrage de la génération du podcast');
|
| 165 |
}
|
| 166 |
|
| 167 |
+
// Start polling for podcast status
|
| 168 |
+
const statusPoller = setInterval(async () => {
|
| 169 |
+
try {
|
| 170 |
+
const statusResponse = await fetch('/podcast-status');
|
| 171 |
+
const statusData = await statusResponse.json();
|
| 172 |
+
|
| 173 |
+
if (statusData.status === 'in_progress') {
|
| 174 |
+
// Update progress bar
|
| 175 |
+
updateProgress(
|
| 176 |
+
statusData.current,
|
| 177 |
+
statusData.total,
|
| 178 |
+
statusData.message || 'Génération en cours...'
|
| 179 |
+
);
|
| 180 |
+
} else if (statusData.status === 'complete') {
|
| 181 |
+
// Podcast is complete
|
| 182 |
+
clearInterval(statusPoller);
|
| 183 |
+
|
| 184 |
+
// Process successful response
|
| 185 |
+
if (statusData.audioUrl) {
|
| 186 |
+
currentAudioUrl = statusData.audioUrl;
|
| 187 |
+
currentAudioFilename = currentAudioUrl.split('/').pop();
|
| 188 |
+
|
| 189 |
+
// Setup audio player
|
| 190 |
+
audioPlayer.src = currentAudioUrl;
|
| 191 |
+
audioPlayer.load();
|
| 192 |
+
|
| 193 |
+
// Show audio card
|
| 194 |
+
audioCard.classList.remove('d-none');
|
| 195 |
+
audioCard.classList.add('fade-in');
|
| 196 |
+
|
| 197 |
+
// Auto-play the audio
|
| 198 |
+
try {
|
| 199 |
+
await audioPlayer.play();
|
| 200 |
+
} catch (playError) {
|
| 201 |
+
console.log('Lecture automatique empêchée par le navigateur.', playError);
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
// Turn off loading state
|
| 205 |
+
setLoadingState(false);
|
| 206 |
+
} else {
|
| 207 |
+
throw new Error('Podcast généré mais URL audio manquante');
|
| 208 |
+
}
|
| 209 |
+
} else if (statusData.status === 'error' || statusData.message.startsWith('Erreur')) {
|
| 210 |
+
// Error occurred
|
| 211 |
+
clearInterval(statusPoller);
|
| 212 |
+
throw new Error(statusData.message || 'Erreur pendant la génération du podcast');
|
| 213 |
+
}
|
| 214 |
+
} catch (pollError) {
|
| 215 |
+
clearInterval(statusPoller);
|
| 216 |
+
throw pollError;
|
| 217 |
+
}
|
| 218 |
+
}, 1000);
|
| 219 |
|
| 220 |
} catch (error) {
|
| 221 |
showError(error.message || 'Une erreur inattendue est survenue. Veuillez réessayer.');
|
|
|
|
| 222 |
setLoadingState(false);
|
| 223 |
}
|
| 224 |
});
|