Created
February 5, 2026 14:54
-
-
Save tribela/8f100fd8a3071396bb086adf5fa8e470 to your computer and use it in GitHub Desktop.
mixxx-now-playing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run --script | |
| # /// script | |
| # requires-python = ">=3.13" | |
| # dependencies = [ | |
| # "flask", | |
| # ] | |
| # /// | |
| import sqlite3 | |
| import os | |
| import time | |
| import json | |
| from flask import Flask, Response, render_template_string | |
| app = Flask(__name__) | |
| DB_PATH = os.path.expanduser("~/.mixxx/mixxxdb.sqlite") | |
| def get_db_info(): | |
| try: | |
| conn = sqlite3.connect(f"file:{DB_PATH}?mode=ro", uri=True) | |
| cursor = conn.cursor() | |
| query = """ | |
| SELECT artist, title FROM library | |
| WHERE id = ( | |
| SELECT track_id FROM playlisttracks | |
| WHERE playlist_id = (SELECT id FROM playlists ORDER BY id DESC LIMIT 1) | |
| ORDER BY pl_datetime_added DESC LIMIT 1 | |
| ); | |
| """ | |
| cursor.execute(query) | |
| res = cursor.fetchone() | |
| conn.close() | |
| return f"{res[0]} - {res[1]}" if res else None | |
| except: | |
| return None | |
| @app.route('/stream') | |
| def stream(): | |
| def event_stream(): | |
| last_metadata = "" | |
| while True: | |
| current_metadata = get_db_info() | |
| if current_metadata and current_metadata != last_metadata: | |
| last_metadata = current_metadata | |
| # SSE : "data: content\n\n" | |
| yield f"data: {json.dumps({'content': current_metadata})}\n\n" | |
| time.sleep(1) | |
| return Response(event_stream(), mimetype="text/event-stream") | |
| @app.route('/') | |
| def index(): | |
| return render_template_string(''' | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <style> | |
| body { background: transparent; color: white; font-family: sans-serif; overflow: hidden; } | |
| #box { | |
| display: inline-block; padding: 10px 20px; background: rgba(0,0,0,0.6); | |
| border-left: 6px solid #00ffcc; opacity: 0; transform: translateY(20px); | |
| transition: all 0.6s cubic-bezier(0.23, 1, 0.32, 1); | |
| } | |
| #box.show { opacity: 1; transform: translateY(0); } | |
| #content { font-size: 24px; font-weight: bold; } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="box"><div id="content"></div></div> | |
| <script> | |
| const box = document.getElementById('box'); | |
| const content = document.getElementById('content'); | |
| const evtSource = new EventSource("/stream"); | |
| evtSource.onmessage = function(event) { | |
| const data = JSON.parse(event.data); | |
| // Animation: hide, change text, show | |
| box.classList.remove('show'); | |
| setTimeout(() => { | |
| content.innerText = data.content; | |
| box.classList.add('show'); | |
| }, 600); | |
| }; | |
| </script> | |
| </body> | |
| </html> | |
| ''') | |
| if __name__ == '__main__': | |
| app.run(port=5000, threaded=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment