fix: serve dashboard.html directly from current directory instead of static folder
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@ -5,11 +5,11 @@ Provides a simple API endpoint that aggregates weather and ABS data for the dash
|
|||||||
This avoids CORS issues with the ABS API.
|
This avoids CORS issues with the ABS API.
|
||||||
|
|
||||||
Run with: python3 dashboard_api.py
|
Run with: python3 dashboard_api.py
|
||||||
Access: http://localhost:5000/api/dashboard
|
Access: http://localhost:5000/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify, Response
|
||||||
import os
|
import os
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -70,12 +70,12 @@ def fetch_weather():
|
|||||||
response = requests.get(WEATHER_URL, timeout=10)
|
response = requests.get(WEATHER_URL, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
if "current_weather" in data:
|
if "current_weather" in data:
|
||||||
weather = data["current_weather"]
|
weather = data["current_weather"]
|
||||||
code = weather["weathercode"]
|
code = weather["weathercode"]
|
||||||
weather_info = WEATHER_CODES.get(code, {"icon": "🌐", "text": "Unbekannt"})
|
weather_info = WEATHER_CODES.get(code, {"icon": "🌐", "text": "Unbekannt"})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"temperature": round(weather["temperature"], 1),
|
"temperature": round(weather["temperature"], 1),
|
||||||
"windspeed": round(weather["windspeed"], 1),
|
"windspeed": round(weather["windspeed"], 1),
|
||||||
@ -96,12 +96,12 @@ def fetch_abs_data():
|
|||||||
"Authorization": f"Bearer {ABS_TOKEN}",
|
"Authorization": f"Bearer {ABS_TOKEN}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get user data with progress
|
# 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=headers, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
user_data = response.json()
|
user_data = response.json()
|
||||||
|
|
||||||
if "mediaProgress" in user_data and user_data["mediaProgress"]:
|
if "mediaProgress" in user_data and user_data["mediaProgress"]:
|
||||||
# Sort by lastUpdate, get latest
|
# Sort by lastUpdate, get latest
|
||||||
sorted_progress = sorted(
|
sorted_progress = sorted(
|
||||||
@ -111,7 +111,7 @@ def fetch_abs_data():
|
|||||||
)
|
)
|
||||||
latest = sorted_progress[0]
|
latest = sorted_progress[0]
|
||||||
library_item_id = latest.get("libraryItemId")
|
library_item_id = latest.get("libraryItemId")
|
||||||
|
|
||||||
# Get item details
|
# Get item details
|
||||||
item_response = requests.get(
|
item_response = requests.get(
|
||||||
f"{ABS_BASE_URL}/api/items/{library_item_id}",
|
f"{ABS_BASE_URL}/api/items/{library_item_id}",
|
||||||
@ -120,12 +120,12 @@ def fetch_abs_data():
|
|||||||
)
|
)
|
||||||
item_response.raise_for_status()
|
item_response.raise_for_status()
|
||||||
item_data = item_response.json()
|
item_data = item_response.json()
|
||||||
|
|
||||||
metadata = item_data.get("media", {}).get("metadata", {})
|
metadata = item_data.get("media", {}).get("metadata", {})
|
||||||
title = metadata.get("title", "Unbekannt")
|
title = metadata.get("title", "Unbekannt")
|
||||||
author = metadata.get("author", "")
|
author = metadata.get("author", "")
|
||||||
media_type = item_data.get("mediaType", "item")
|
media_type = item_data.get("mediaType", "item")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"title": title,
|
"title": title,
|
||||||
"author": author,
|
"author": author,
|
||||||
@ -134,7 +134,7 @@ def fetch_abs_data():
|
|||||||
"currentTime": format_duration(latest.get("currentTime", 0)),
|
"currentTime": format_duration(latest.get("currentTime", 0)),
|
||||||
"duration": format_duration(latest.get("duration", 0)),
|
"duration": format_duration(latest.get("duration", 0)),
|
||||||
}
|
}
|
||||||
|
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"ABS API error: {e}")
|
print(f"ABS API error: {e}")
|
||||||
@ -146,7 +146,7 @@ def get_dashboard_data():
|
|||||||
"""Return combined dashboard data"""
|
"""Return combined dashboard data"""
|
||||||
weather = fetch_weather()
|
weather = fetch_weather()
|
||||||
abs_data = fetch_abs_data()
|
abs_data = fetch_abs_data()
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"weather": weather,
|
"weather": weather,
|
||||||
"audiobook": abs_data,
|
"audiobook": abs_data,
|
||||||
@ -155,8 +155,12 @@ def get_dashboard_data():
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def serve_dashboard():
|
def serve_dashboard():
|
||||||
"""Serve the dashboard HTML"""
|
"""Serve the dashboard HTML directly from the current directory"""
|
||||||
return app.send_static_file("dashboard.html")
|
try:
|
||||||
|
with open("dashboard.html", "r", encoding="utf-8") as f:
|
||||||
|
return Response(f.read(), mimetype="text/html")
|
||||||
|
except FileNotFoundError:
|
||||||
|
return "Error: dashboard.html not found. Please run from the project directory.", 404
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -164,5 +168,7 @@ if __name__ == "__main__":
|
|||||||
if not os.path.exists("dashboard.html"):
|
if not os.path.exists("dashboard.html"):
|
||||||
print("Error: dashboard.html not found. Please run from the project directory.")
|
print("Error: dashboard.html not found. Please run from the project directory.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
print("Starting dashboard server...")
|
||||||
|
print("Open http://localhost:5000 in your browser")
|
||||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user