feat: add cover proxy endpoint and progress bar to dashboard
- Add /api/cover/<library_item_id> endpoint to proxy cover images from ABS - Return cover proxy URL instead of direct ABS URL to avoid CORS issues - Add visual progress bar to audiobook card - Add cache-busting timestamp to cover image URLs - Improve cover image error handling with fallback placeholder Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@ -9,8 +9,9 @@ Access: http://localhost:5000/
|
||||
"""
|
||||
|
||||
import requests
|
||||
from flask import Flask, jsonify, Response
|
||||
from flask import Flask, jsonify, Response, send_file
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@ -51,6 +52,11 @@ WEATHER_CODES = {
|
||||
99: {"icon": "⛈️", "text": "Gewitter mit starkem Hagel"},
|
||||
}
|
||||
|
||||
ABS_HEADERS = {
|
||||
"Authorization": f"Bearer {ABS_TOKEN}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
def format_duration(seconds):
|
||||
"""Format seconds to HH:MM:SS or MM:SS"""
|
||||
@ -90,15 +96,10 @@ def fetch_weather():
|
||||
|
||||
|
||||
def fetch_abs_data():
|
||||
"""Fetch ABS user data and latest audiobook with cover"""
|
||||
"""Fetch ABS user data and latest audiobook with cover proxy URL"""
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Bearer {ABS_TOKEN}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
# Get user data with progress
|
||||
response = requests.get(f"{ABS_BASE_URL}/api/me", headers=headers, timeout=10)
|
||||
response = requests.get(f"{ABS_BASE_URL}/api/me", headers=ABS_HEADERS, timeout=10)
|
||||
response.raise_for_status()
|
||||
user_data = response.json()
|
||||
|
||||
@ -115,7 +116,7 @@ def fetch_abs_data():
|
||||
# Get item details
|
||||
item_response = requests.get(
|
||||
f"{ABS_BASE_URL}/api/items/{library_item_id}",
|
||||
headers=headers,
|
||||
headers=ABS_HEADERS,
|
||||
timeout=10,
|
||||
)
|
||||
item_response.raise_for_status()
|
||||
@ -126,18 +127,15 @@ def fetch_abs_data():
|
||||
author = metadata.get("author", "")
|
||||
media_type = item_data.get("mediaType", "item")
|
||||
|
||||
# Get cover URL - construct full URL from coverPath
|
||||
cover_path = item_data.get("coverPath", "")
|
||||
cover_url = None
|
||||
if cover_path:
|
||||
# Remove leading slash if present and construct full URL
|
||||
cover_url = f"{ABS_BASE_URL}{cover_path}" if cover_path.startswith("/") else f"{ABS_BASE_URL}/{cover_path}"
|
||||
# Use proxy URL for cover to avoid CORS issues
|
||||
# The proxy endpoint will fetch the actual cover from ABS
|
||||
cover_proxy_url = f"/api/cover/{library_item_id}"
|
||||
|
||||
return {
|
||||
"title": title,
|
||||
"author": author,
|
||||
"mediaType": media_type,
|
||||
"coverUrl": cover_url,
|
||||
"coverUrl": cover_proxy_url,
|
||||
"progress": round(latest.get("progress", 0) * 100),
|
||||
"currentTime": format_duration(latest.get("currentTime", 0)),
|
||||
"duration": format_duration(latest.get("duration", 0)),
|
||||
@ -149,6 +147,52 @@ def fetch_abs_data():
|
||||
return None
|
||||
|
||||
|
||||
@app.route("/api/cover/<library_item_id>")
|
||||
def get_cover(library_item_id):
|
||||
"""Proxy endpoint to fetch cover image from ABS and return it"""
|
||||
try:
|
||||
# Try to get cover from ABS API
|
||||
cover_response = requests.get(
|
||||
f"{ABS_BASE_URL}/api/items/{library_item_id}/cover",
|
||||
headers=ABS_HEADERS,
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
if cover_response.ok and cover_response.content:
|
||||
# Return the image directly
|
||||
return Response(
|
||||
cover_response.content,
|
||||
mimetype=cover_response.headers.get('Content-Type', 'image/jpeg')
|
||||
)
|
||||
|
||||
# Fallback: try static path
|
||||
cover_response = requests.get(
|
||||
f"{ABS_BASE_URL}/metadata/items/{library_item_id}/cover.jpg",
|
||||
headers=ABS_HEADERS,
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
if cover_response.ok and cover_response.content:
|
||||
return Response(
|
||||
cover_response.content,
|
||||
mimetype='image/jpeg'
|
||||
)
|
||||
|
||||
# If no cover found, return a transparent pixel
|
||||
return Response(
|
||||
bytes([0x89, 0x50, 0x4E, 0x47]), # Tiny transparent PNG
|
||||
mimetype='image/png'
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Cover fetch error for {library_item_id}: {e}")
|
||||
# Return transparent pixel on error
|
||||
return Response(
|
||||
bytes([0x89, 0x50, 0x4E, 0x47]),
|
||||
mimetype='image/png'
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/dashboard")
|
||||
def get_dashboard_data():
|
||||
"""Return combined dashboard data"""
|
||||
|
||||
Reference in New Issue
Block a user