# mbusty/power-hour architecture

> Architecture diagram of the GitHub repository mbusty/power-hour, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mbusty/power-hour
- Repository: https://github.com/mbusty/power-hour

## Overview

#### 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`](https://github.com/mbusty/power-hour/blob/main/main.py) (Flask routes & sessions), [`spotify_client.py`](https://github.com/mbusty/power-hour/blob/main/spotify_client.py) (Setlist orchestrator), [`templates/player.html`](https://github.com/mbusty/power-hour/blob/main/templates/player.html) (Player UI), [`vibe_parser.py`](https://github.com/mbusty/power-hour/blob/main/vibe_parser.py) (Vibe parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser client"]
  node_index["Discovery form<br/>HTML view<br/>[index.html]"]
  node_setlist["Setlist review<br/>HTML view<br/>[setlist.html]"]
  node_player_view["Player UI<br/>HTML view<br/>[player.html]"]
  node_player_js["Playback state machine<br/>browser controller<br/>[player.js]"]
  node_styles["Shared styles<br/>CSS<br/>[main.css]"]
end

subgraph group_server["Flask service"]
  node_docker["Container runtime<br/>Docker"]
  node_main{{"Flask routes &amp; sessions<br/>Flask app<br/>[main.py]"}}
  node_auth["Spotify OAuth<br/>auth adapter<br/>[auth.py]"]
end

subgraph group_pipeline["Discovery pipeline"]
  node_orchestrator{{"Setlist orchestrator<br/>Spotify coordinator<br/>[spotify_client.py]"}}
  node_vibe["Vibe parser<br/>LLM adapter<br/>[vibe_parser.py]"]
  node_lastfm["Last.fm expansion<br/>recommendation adapter<br/>[lastfm_client.py]"]
  node_deezer["Deezer ISRC lookup<br/>identity adapter<br/>[deezer_client.py]"]
  node_musicbrainz["MusicBrainz ISRC fallback<br/>identity adapter"]
end

subgraph group_external["External services"]
  node_spotify[("Spotify API &amp; Connect<br/>external platform")]
  node_llm["OpenAI-compatible LLM<br/>optional external API"]
  node_spotify_playback(("Spotify audio playback<br/>external playback"))
end

node_docker -->|"runs"| node_main
node_index -->|"submits discovery/search"| node_main
node_main -->|"renders generated setlist"| node_setlist
node_setlist -->|"edits offsets"| node_main
node_main -->|"renders playback page"| node_player_view
node_player_view -->|"UI contract"| node_player_js
node_player_view -->|"presentation"| node_styles
node_main -->|"authenticates user"| node_auth
node_auth -->|"OAuth via Spotipy"| node_spotify
node_main -->|"creates setlist"| node_orchestrator
node_vibe -->|"tags and constraints"| node_lastfm
node_llm -.->|"optional prompt expansion"| node_vibe
node_orchestrator -.->|"freeform mode"| node_vibe
node_orchestrator -->|"candidate generation"| node_lastfm
node_lastfm -->|"song identity lookup"| node_deezer
node_deezer -.->|"ISRC fallback"| node_musicbrainz
node_orchestrator -->|"ISRC/text resolution, filtering"| node_spotify
node_player_js -->|"Connect or Web Playback SDK"| node_spotify_playback
node_main -->|"Web API playback control"| node_spotify

click node_docker "https://github.com/mbusty/power-hour/tree/main/Dockerfile"
click node_main "https://github.com/mbusty/power-hour/blob/main/main.py"
click node_auth "https://github.com/mbusty/power-hour/blob/main/auth.py"
click node_index "https://github.com/mbusty/power-hour/blob/main/templates/index.html"
click node_setlist "https://github.com/mbusty/power-hour/blob/main/templates/setlist.html"
click node_player_view "https://github.com/mbusty/power-hour/blob/main/templates/player.html"
click node_player_js "https://github.com/mbusty/power-hour/blob/main/static/js/player.js"
click node_styles "https://github.com/mbusty/power-hour/blob/main/static/css/main.css"
click node_orchestrator "https://github.com/mbusty/power-hour/blob/main/spotify_client.py"
click node_vibe "https://github.com/mbusty/power-hour/blob/main/vibe_parser.py"
click node_lastfm "https://github.com/mbusty/power-hour/blob/main/lastfm_client.py"
click node_deezer "https://github.com/mbusty/power-hour/blob/main/deezer_client.py"
click node_musicbrainz "https://github.com/mbusty/power-hour/blob/main/musicbrainz_client.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_index,node_setlist,node_player_view,node_player_js,node_styles toneBlue
class node_docker,node_main,node_auth toneAmber
class node_orchestrator,node_vibe,node_lastfm,node_deezer,node_musicbrainz toneMint
class node_spotify,node_llm,node_spotify_playback toneRose
```

## Components

### Browser client

- [Discovery form](https://github.com/mbusty/power-hour/blob/main/templates/index.html): HTML view · `templates/index.html`
- [Setlist review](https://github.com/mbusty/power-hour/blob/main/templates/setlist.html): HTML view · Edits generated track offsets. · `templates/setlist.html`
- [Player UI](https://github.com/mbusty/power-hour/blob/main/templates/player.html): HTML view · `templates/player.html`
- [Playback state machine](https://github.com/mbusty/power-hour/blob/main/static/js/player.js): browser controller · Runs countdowns, breaks, progression, and optional Web Playback SDK. · `static/js/player.js`
- [Shared styles](https://github.com/mbusty/power-hour/blob/main/static/css/main.css): CSS · `static/css/main.css`

### Flask service

- [Container runtime](https://github.com/mbusty/power-hour/tree/main/Dockerfile): Docker · Serves the application on port 8008. · `Dockerfile`
- [Flask routes & sessions](https://github.com/mbusty/power-hour/blob/main/main.py): Flask app · Owns HTTP flow, templates, and setlist lifecycle. · `main.py`
- [Spotify OAuth](https://github.com/mbusty/power-hour/blob/main/auth.py): auth adapter · Handles Spotipy OAuth and loopback callback state. · `auth.py`

### Discovery pipeline

- [Setlist orchestrator](https://github.com/mbusty/power-hour/blob/main/spotify_client.py): Spotify coordinator · Builds exactly 60 playable tracks for Discover and Search. · `spotify_client.py`
- [Vibe parser](https://github.com/mbusty/power-hour/blob/main/vibe_parser.py): LLM adapter · Converts freeform prompts into artists, songs, tags, and eras. · `vibe_parser.py`
- [Last.fm expansion](https://github.com/mbusty/power-hour/blob/main/lastfm_client.py): recommendation adapter · Supplies related artists, similar tracks, and tag charts. · `lastfm_client.py`
- [Deezer ISRC lookup](https://github.com/mbusty/power-hour/blob/main/deezer_client.py): identity adapter · `deezer_client.py`
- [MusicBrainz ISRC fallback](https://github.com/mbusty/power-hour/blob/main/musicbrainz_client.py): identity adapter · Keyless and rate-limited fallback. · `musicbrainz_client.py`

### External services

- **Spotify API & Connect**: external platform · Canonical playable catalog and playback controller.
- **OpenAI-compatible LLM**: optional external API · GitHub Models is the default endpoint.
- **Spotify audio playback**: external playback · Streams audio through Connect or the host browser SDK.

## Connections

- 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
