diff --git a/dashboard.py b/dashboard.py new file mode 100644 index 0000000..4b46f5c --- /dev/null +++ b/dashboard.py @@ -0,0 +1,121 @@ +import os +import sqlite3 +from datetime import datetime +import traceback + +# Configurable paths via environment variables with fallback defaults +DB_PATH = os.getenv('NAVIDROME_DB_PATH', './navidrome.db') +OUTPUT_HTML = os.getenv('OUTPUT_HTML_PATH', './public/index.html') + +def get_all_users(): + try: + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + cursor.execute("SELECT name FROM user") + users = [row[0] for row in cursor.fetchall()] + conn.close() + return users + except Exception as e: + print(f"Error fetching users: {e}") + return [] + +def get_aggregated_scrobbles(user_name): + try: + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + cursor.execute(""" + SELECT + m.title, + m.artist, + COUNT(*) AS play_count, + MAX(s.submission_time) AS last_played + FROM scrobbles s + JOIN media_file m ON s.media_file_id = m.id + JOIN user u ON s.user_id = u.id + WHERE u.name = ? + GROUP BY m.title, m.artist + ORDER BY play_count DESC, last_played DESC, m.artist ASC + """, (user_name,)) + rows = cursor.fetchall() + conn.close() + return rows + except Exception as e: + print(f"Error fetching scrobbles for {user_name}: {e}") + return [] + +def generate_html(): + print("Fetching users from Navidrome DB...") + users = get_all_users() + print(f"Found users: {users}") + + html_content = """ + +
+ + +| Track | Artist | Plays | Last Played |
|---|---|---|---|
| {title} | {artist} | {play_count} | {dt} |