feat: add Flask API server for dashboard to avoid CORS issues with ABS
- Add dashboard_api.py as backend proxy for ABS and weather data - Update dashboard.html to fetch data from local API instead of directly - Add requirements.txt for Flask dependencies - Add .gitignore for Python and pytest files Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
216
dashboard.html
216
dashboard.html
@ -118,6 +118,21 @@
|
||||
.full-width {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
.status-bar {
|
||||
margin-top: 2rem;
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.status-bar .error {
|
||||
color: #ff4444;
|
||||
}
|
||||
|
||||
.status-bar .success {
|
||||
color: #44ff44;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@ -145,15 +160,13 @@
|
||||
|
||||
<button class="refresh-btn" onclick="refreshAll()">Aktualisieren</button>
|
||||
|
||||
<div class="status-bar" id="status"></div>
|
||||
|
||||
<script>
|
||||
// ABS Configuration
|
||||
const ABS_BASE_URL = 'https://abs.sphinx-morray.ts.net/audiobookshelf';
|
||||
const ABS_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXlJZCI6IjFiZTdiZmIzLTE1NGYtNGE3Yy1hMTFjLTZkNDg3YWNhMWZiYyIsIm5hbWUiOiJEYXNoYm9hcmQiLCJ0eXBlIjoiYXBpIiwiaWF0IjoxNzkzNjU0NjA3LCJleHAiOjE3OTM2ODYxNDJ9.h3284mhyio4GIIfAFoa6D6KXiUNWzGy622SiARqTplE';
|
||||
// API endpoint (change this if your server runs on a different port)
|
||||
const API_URL = '/api/dashboard';
|
||||
|
||||
// Wetter Configuration (Aachen: 50.7769, 6.0834)
|
||||
const WEATHER_URL = 'https://api.open-meteo.com/v1/forecast?latitude=50.7769&longitude=6.0834¤t_weather=true&timezone=Europe/Berlin';
|
||||
|
||||
// Wettercode zu Text Mapping (WMO Codes)
|
||||
// Wettercode zu Text Mapping (WMO codes)
|
||||
const weatherCodes = {
|
||||
0: { icon: '☀️', text: 'Klarer Himmel' },
|
||||
1: { icon: '⛅', text: 'Überwiegend klar' },
|
||||
@ -196,113 +209,97 @@
|
||||
document.getElementById('date').textContent = now.toLocaleDateString('de-DE', dateOptions);
|
||||
}
|
||||
|
||||
// Wetterdaten holen
|
||||
async function fetchWeather() {
|
||||
try {
|
||||
const response = await fetch(WEATHER_URL);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.current_weather) {
|
||||
const weather = data.current_weather;
|
||||
const code = weather.weathercode;
|
||||
const weatherInfo = weatherCodes[code] || { icon: '🌐', text: 'Unbekannt' };
|
||||
|
||||
const temp = Math.round(weather.temperature * 10) / 10;
|
||||
const wind = Math.round(weather.windspeed * 10) / 10;
|
||||
const time = new Date(weather.time).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' });
|
||||
|
||||
document.getElementById('weather').innerHTML = `
|
||||
<div class="weather-container">
|
||||
<div class="weather-icon">${weatherInfo.icon}</div>
|
||||
<div class="weather-details">
|
||||
<div class="card-value">${temp}°C</div>
|
||||
<div class="card-subvalue">${weatherInfo.text}</div>
|
||||
<div class="card-subvalue">Wind: ${wind} km/h</div>
|
||||
<div class="card-subvalue">Aktualisiert: ${time}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
} catch (error) {
|
||||
// Wetterdaten anzeigen
|
||||
function displayWeather(weather) {
|
||||
if (!weather) {
|
||||
document.getElementById('weather').innerHTML = '<span class="error">Wetterdaten nicht verfügbar</span>';
|
||||
console.error('Wetter-API Fehler:', error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ABS Daten holen
|
||||
async function fetchAudiobook() {
|
||||
try {
|
||||
// Benutzerdaten mit Fortschritt holen
|
||||
const response = await fetch(`${ABS_BASE_URL}/api/me`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${ABS_TOKEN}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const userData = await response.json();
|
||||
|
||||
if (userData.mediaProgress && userData.mediaProgress.length > 0) {
|
||||
// Nach dem zuletzt aktualisierten Element sortieren
|
||||
const sortedProgress = [...userData.mediaProgress].sort((a, b) =>
|
||||
new Date(b.lastUpdate) - new Date(a.lastUpdate)
|
||||
);
|
||||
|
||||
const latest = sortedProgress[0];
|
||||
const libraryItemId = latest.libraryItemId;
|
||||
|
||||
// Details des Hörbuchs/Podcasts holen
|
||||
const itemResponse = await fetch(`${ABS_BASE_URL}/api/items/${libraryItemId}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${ABS_TOKEN}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const itemData = await itemResponse.json();
|
||||
const title = itemData.media?.metadata?.title || 'Unbekannt';
|
||||
const author = itemData.media?.metadata?.author || '';
|
||||
const mediaType = itemData.mediaType || 'item';
|
||||
|
||||
let displayText = title;
|
||||
if (author) displayText = `${title} – ${author}`;
|
||||
|
||||
// Fortschritt anzeigen
|
||||
const progressPercent = Math.round(latest.progress * 100);
|
||||
const currentTime = formatDuration(latest.currentTime);
|
||||
const duration = formatDuration(latest.duration);
|
||||
|
||||
document.getElementById('audiobook').innerHTML = `
|
||||
<div class="card-value" style="font-size: 1.25rem;">${displayText}</div>
|
||||
<div class="card-subvalue">${mediaType === 'podcast' ? '🎙️ Podcast' : '📖 Hörbuch'}</div>
|
||||
<div class="card-subvalue">Fortschritt: ${progressPercent}% (${currentTime}/${duration})</div>
|
||||
`;
|
||||
} else {
|
||||
document.getElementById('audiobook').innerHTML = '<span class="card-subvalue">Kein aktuell gehörtes Hörbuch</span>';
|
||||
}
|
||||
} catch (error) {
|
||||
document.getElementById('audiobook').innerHTML = '<span class="error">ABS-Daten nicht verfügbar</span>';
|
||||
console.error('ABS-API Fehler:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Dauer formatieren (Sekunden zu HH:MM:SS)
|
||||
function formatDuration(seconds) {
|
||||
if (!seconds) return '0:00';
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
||||
const temp = weather.temperature;
|
||||
const wind = weather.windspeed;
|
||||
const icon = weather.icon || '🌐';
|
||||
const text = weather.text || 'Unbekannt';
|
||||
const time = new Date(weather.time).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit' });
|
||||
|
||||
document.getElementById('weather').innerHTML = `
|
||||
<div class="weather-container">
|
||||
<div class="weather-icon">${icon}</div>
|
||||
<div class="weather-details">
|
||||
<div class="card-value">${temp}°C</div>
|
||||
<div class="card-subvalue">${text}</div>
|
||||
<div class="card-subvalue">Wind: ${wind} km/h</div>
|
||||
<div class="card-subvalue">Aktualisiert: ${time}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// ABS-Daten anzeigen
|
||||
function displayAudiobook(absData) {
|
||||
if (!absData) {
|
||||
document.getElementById('audiobook').innerHTML = '<span class="error">ABS-Daten nicht verfügbar</span>';
|
||||
return;
|
||||
}
|
||||
return `${minutes}:${secs.toString().padStart(2, '0')}`;
|
||||
|
||||
let displayText = absData.title;
|
||||
if (absData.author) displayText = `${absData.title} – ${absData.author}`;
|
||||
|
||||
const mediaType = absData.mediaType === 'podcast' ? '🎙️ Podcast' : (absData.mediaType === 'book' ? '📖 Hörbuch' : '📚 Medium');
|
||||
const progressPercent = absData.progress;
|
||||
const currentTime = absData.currentTime;
|
||||
const duration = absData.duration;
|
||||
|
||||
document.getElementById('audiobook').innerHTML = `
|
||||
<div class="card-value" style="font-size: 1.25rem;">${displayText}</div>
|
||||
<div class="card-subvalue">${mediaType}</div>
|
||||
<div class="card-subvalue">Fortschritt: ${progressPercent}% (${currentTime}/${duration})</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Alle Daten von der API holen
|
||||
async function fetchDashboardData() {
|
||||
try {
|
||||
const response = await fetch(API_URL);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API-Fehler:', error);
|
||||
updateStatus(`Fehler: ${error.message}`, 'error');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Status anzeigen
|
||||
function updateStatus(message, type = '') {
|
||||
const statusEl = document.getElementById('status');
|
||||
statusEl.textContent = message;
|
||||
statusEl.className = type;
|
||||
}
|
||||
|
||||
// Alle Daten aktualisieren
|
||||
async function refreshAll() {
|
||||
updateClock();
|
||||
await Promise.all([fetchWeather(), fetchAudiobook()]);
|
||||
updateStatus('Lade Daten...', 'loading');
|
||||
|
||||
try {
|
||||
const data = await fetchDashboardData();
|
||||
|
||||
if (data) {
|
||||
displayWeather(data.weather);
|
||||
displayAudiobook(data.audiobook);
|
||||
updateStatus('Daten erfolgreich geladen', 'success');
|
||||
} else {
|
||||
updateStatus('Keine Daten erhalten', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
updateStatus(`Fehler: ${error.message}`, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Initial laden
|
||||
@ -317,6 +314,15 @@
|
||||
|
||||
// Bei Fokus zurückkehren auch aktualisieren
|
||||
window.addEventListener('focus', refreshAll);
|
||||
|
||||
// Status nach 5 Sekunden zurücksetzen
|
||||
setTimeout(() => {
|
||||
const statusEl = document.getElementById('status');
|
||||
if (statusEl.className === 'success') {
|
||||
statusEl.textContent = '';
|
||||
statusEl.className = '';
|
||||
}
|
||||
}, 5000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user