How mbusty/power-hour works
Purpose and Entry Points
Power Hour is a locally/LAN-hosted Flask application that builds and controls a 60-track, minute-per-song Spotify session. The server entry point is main.py; container deployment uses Dockerfile, docker-compose.yml, and Gunicorn configuration in gunicorn_conf.py. Browser entry flows are the discovery/search form, setlist review, and player pages.
Flask Application and Session Boundary
main.py owns HTTP routes, template rendering, setlist lifecycle, and server-side coordination. It requires a persistent Flask secret because OAuth state and generated setlists depend on session integrity. HTML views define the three-stage user flow:
• Input: templates/index.html
• Review/edit offsets: templates/setlist.html
• Playback control: templates/player.html
Spotify Authentication and Playback Control
auth.py implements Spotify OAuth through Spotipy, including the loopback callback required at 127.0.0.1. Spotify is both the canonical playable-track catalog and playback controller. The backend uses Spotify Web API/Connect control, while static/js/player.js optionally initializes the Spotify Web Playback SDK for host-browser audio. LAN/mobile clients use Spotify Connect because plain HTTP LAN origins cannot run the DRM-backed browser SDK.
Setlist Generation and Track Resolution
spotify_client.py is the central orchestration layer for Discover and Search modes. It classifies input, requests candidate tracks, resolves playable Spotify recordings, applies filters/deduplication and artist caps, fills to exactly 60 tracks, and preserves seed-placement constraints during shuffle. It also supplies Spotify-only fallback search when recommendation services are unavailable.
Recommendation and Identity-Enrichment Pipeline
Discover mode crosses several external data boundaries:
• lastfm_client.py obtains related artists, similar tracks, and tag-based top tracks for candidate generation.
• deezer_client.py finds ISRCs from song/artist metadata without credentials.
• musicbrainz_client.py provides a keyless, rate-limited ISRC fallback.
• spotify_client.py searches Spotify by ISRC first, then uses verified text-search fallback to avoid incorrect versions, karaoke, or covers.
This pipeline separates recommendation identity from Spotify playback identity.
Optional Natural-Language Vibe Expansion
vibe_parser.py calls an OpenAI-compatible LLM endpoint—GitHub Models by default—to transform freeform prompts into artists, songs, tags, and era constraints. The parser is optional and degrades to Last.fm tag handling when no LLM key is configured. Its outputs feed the same Last.fm expansion and Spotify-resolution pipeline rather than directly choosing playback tracks.
Client Playback State Machine
static/js/player.js implements the timed playback experience: track start offsets, 60-second countdown, final 3-2-1 signals, pause/background behavior, two-second drink-break splash/chime, and automatic progression. templates/player.html supplies its UI contract; static/css/main.css provides shared presentation. Playback audio is streamed by Spotify, not proxied through Flask.
Runtime and Infrastructure
The app targets Python 3.13 with Flask and Spotipy (requirements.txt). Docker runs the service on port 8008; launch wrappers (up.sh, up.ps1, up.cmd) detect and pass the host LAN address for startup messaging. Required external services are Spotify Premium/API, with optional Last.fm and LLM credentials; Deezer and MusicBrainz are keyless supporting APIs.
Main entry points: main.py Flask routes & sessions · spotify_client.py Setlist orchestrator · player.html Player UI · vibe_parser.py Vibe parser
Components 16
Browser client
- Discovery form
templates/index.html - Setlist review · Edits generated track offsets.
templates/setlist.html - Player UI
templates/player.html - Playback state machine · Runs countdowns, breaks, progression, and optional Web Playback SDK.
static/js/player.js - Shared styles
static/css/main.css
Flask service
- Container runtime · Serves the application on port 8008.
Dockerfile - Flask routes & sessions · Owns HTTP flow, templates, and setlist lifecycle.
main.py - Spotify OAuth · Handles Spotipy OAuth and loopback callback state.
auth.py
Discovery pipeline
- Setlist orchestrator · Builds exactly 60 playable tracks for Discover and Search.
spotify_client.py - Vibe parser · Converts freeform prompts into artists, songs, tags, and eras.
vibe_parser.py - Last.fm expansion · Supplies related artists, similar tracks, and tag charts.
lastfm_client.py - Deezer ISRC lookup
deezer_client.py - MusicBrainz ISRC fallback · Keyless and rate-limited fallback.
musicbrainz_client.py
External services
- Spotify API & Connect · Canonical playable catalog and playback controller.
- OpenAI-compatible LLM · GitHub Models is the default endpoint.
- Spotify audio playback · Streams audio through Connect or the host browser SDK.
Connections 19
- Container runtime → Flask routes & sessions: runs
- Discovery form → Flask routes & sessions: submits discovery/search
- Flask routes & sessions → Setlist review: renders generated setlist
- Setlist review → Flask routes & sessions: edits offsets
- Flask routes & sessions → Player UI: renders playback page
- Player UI → Playback state machine: UI contract
- Player UI → Shared styles: presentation
- Flask routes & sessions → Spotify OAuth: authenticates user
- Spotify OAuth → Spotify API & Connect: OAuth via Spotipy
- Flask routes & sessions → Setlist orchestrator: creates setlist
- Vibe parser → Last.fm expansion: tags and constraints
- OpenAI-compatible LLM → Vibe parser: optional prompt expansion
- Setlist orchestrator → Vibe parser: freeform mode
- Setlist orchestrator → Last.fm expansion: candidate generation
- Last.fm expansion → Deezer ISRC lookup: song identity lookup
- Deezer ISRC lookup → MusicBrainz ISRC fallback: ISRC fallback
- Setlist orchestrator → Spotify API & Connect: ISRC/text resolution, filtering
- Playback state machine → Spotify audio playback: Connect or Web Playback SDK
- Flask routes & sessions → Spotify API & Connect: Web API playback control