Split docs into deploy, setup and usage guides
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m10s
CI / test (push) Successful in 3m13s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m10s
CI / test (push) Successful in 3m13s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
README had grown into one long file covering deployment, local development and end-user features together. Split into: - docs/deploy.md: running a real instance - images, configuration, putting it behind TLS (with a minimal Caddy example and the CORS_ORIGINS/UI_BASE_URL distinction the last session's manual test found the hard way), monitoring, backups, releases. - docs/setup.md: working on the code - local dev environment, migrations, tests, the manual fediverse test, CI/CD. - docs/usage.md: accounts and apps, connecting a player, importing history, sharing on the fediverse. README is now a short overview and quick start that links to the three, plus the existing API reference and federation/design docs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
1 parent
9ca2ace854
commit
b941a17e07
4 files changed
+367
-196
No files matched your search
@@ -1,6 +1,10 @@
|
||||
# Scrobbler
|
||||
|
||||
A self-hosted music scrobbling service. It speaks the **Last.fm (Audioscrobbler 2.0) API**, so players and scrobblers that let you set a custom Last.fm server can use it unchanged. It also has a web UI for your listening history.
|
||||
A self-hosted music scrobbling service. It speaks the **Last.fm (Audioscrobbler 2.0) API**,
|
||||
so players and scrobblers that let you set a custom Last.fm server can use it unchanged.
|
||||
It also has a web UI for your listening history, and optional sharing on the fediverse
|
||||
(ActivityPub) — followable from Mastodon, with weekly summaries, milestones and now
|
||||
playing.
|
||||
|
||||
```
|
||||
players / scrobblers ──► API /2.0/ Last.fm-compatible
|
||||
@@ -9,208 +13,31 @@ web UI (static files) ──► API /api/v1/ JSON API for the UI
|
||||
Prometheus ◄── API :9100/metrics, postgres-exporter ──► Grafana
|
||||
```
|
||||
|
||||
- **API** (`src/scrobbler/`): Flask under gunicorn. It is API-only and serves no pages.
|
||||
- **Web UI** (`frontend/`): plain HTML, CSS and JavaScript with no build step. Host it anywhere static files can be served.
|
||||
- **Docs**: OpenAPI at `/api/openapi.json`, Swagger UI at `/api/docs` and ReDoc at `/api/redoc`. A copy of the spec is committed at `docs/openapi.json`.
|
||||
- **Worker**: `flask worker` runs background work, such as importing Last.fm history from export files or straight from Last.fm.
|
||||
- **Monitoring**: Prometheus metrics, four provisioned Grafana dashboards, and alert rules.
|
||||
## Docs
|
||||
|
||||
## Run it
|
||||
- **[Deploy](docs/deploy.md)** — running a real instance: images, configuration, TLS,
|
||||
monitoring, backups, releases.
|
||||
- **[Set up](docs/setup.md)** — working on the code: local dev environment, migrations,
|
||||
tests, CI/CD.
|
||||
- **[Use](docs/usage.md)** — accounts and apps, connecting a player, importing history,
|
||||
sharing on the fediverse.
|
||||
- **API reference**: `/api/openapi.json`, Swagger UI at `/api/docs`, ReDoc at
|
||||
`/api/redoc` (a copy is committed at `docs/openapi.json`).
|
||||
- **Federation internals**: `src/scrobbler/federation/README.md`. Design docs for how it
|
||||
was built: `docs/design/`.
|
||||
|
||||
## Try it
|
||||
|
||||
```sh
|
||||
cp .env.example .env # then set SECRET_KEY and POSTGRES_PASSWORD
|
||||
docker compose up -d # db, api (:5050), worker, ui (:8080)
|
||||
docker compose --profile monitoring up -d # + Prometheus (:9090), Grafana (:3000)
|
||||
```
|
||||
|
||||
Open http://localhost:8080, create an account, then go to **Apps** and create an app for each player.
|
||||
|
||||
To run the published images instead of building them locally:
|
||||
|
||||
```sh
|
||||
SCROBBLER_IMAGE_TAG=main docker compose up -d --no-build
|
||||
```
|
||||
|
||||
The images are `pkgs.daubney.dev/scrobbler/scrobbler-api` and `pkgs.daubney.dev/scrobbler/scrobbler-ui`.
|
||||
|
||||
### Configuration
|
||||
|
||||
| Variable | Used by | Default | Purpose |
|
||||
|---|---|---|---|
|
||||
| `DATABASE_URL` | api | local Postgres | SQLAlchemy URL (`postgresql+psycopg://…`) |
|
||||
| `SECRET_KEY` | api | `dev-insecure-secret` | Flask secret |
|
||||
| `CORS_ORIGINS` | api | `http://localhost:8080` | Comma-separated UI origins allowed to call `/api/v1` |
|
||||
| `UI_BASE_URL` | api | `http://localhost:8080` | Where `/api/auth/` sends users to approve desktop sign-ins |
|
||||
| `OPENAPI_DOCS_ENABLED` | api | `1` | Set to `0` to hide `/api/docs` and `/api/redoc` |
|
||||
| `METRICS_PORT` | api | `9100` | Prometheus metrics port (keep it off the public network) |
|
||||
| `WEB_CONCURRENCY` | api | `2` | gunicorn workers |
|
||||
| `RUN_MIGRATIONS` | api image | `1` | Apply database migrations on start |
|
||||
| `LASTFM_API_KEY` | api, worker | *(unset)* | Enables importing straight from Last.fm ([get a key](https://www.last.fm/api/account/create)) |
|
||||
| `IMPORT_MAX_BYTES` | api | 200 MB | Largest export file accepted |
|
||||
| `WORKER_METRICS_PORT` | worker | `9101` | The worker's Prometheus metrics port (`IMPORT_WORKER_METRICS_PORT` still works) |
|
||||
| `API_BASE_URL` | ui image | `http://localhost:5050` | API location, written into `config.js` and the CSP |
|
||||
|
||||
In `docker compose` the host ports are set with `DB_PORT`, `API_PORT`, `UI_PORT`, `PROMETHEUS_PORT` and `GRAFANA_PORT`.
|
||||
|
||||
## Connecting a player
|
||||
|
||||
In the player's scrobbling settings, choose a custom or self-hosted Last.fm server and enter:
|
||||
|
||||
- **API URL**: `https://your-host/2.0/`
|
||||
- **API key and shared secret**: from an app on the **Apps** page
|
||||
- **Username and password**: your Scrobbler account
|
||||
|
||||
Two ways of signing in are supported:
|
||||
|
||||
- **Mobile**: `auth.getMobileSession` with a username and password, sent as a POST body over HTTPS.
|
||||
- **Desktop**: `auth.getToken`, then the user approves at `/api/auth/` (which redirects to the web UI), then `auth.getSession`.
|
||||
|
||||
The legacy `authToken = md5(username + md5(password))` form is **not supported**, because checking it would mean storing unsalted password hashes. pylast's `password_hash` option uses that form. With pylast, use `SessionKeyGenerator` (the desktop flow) instead.
|
||||
|
||||
Supported methods:
|
||||
|
||||
- `auth.getMobileSession`, `auth.getToken`, `auth.getSession`
|
||||
- `track.scrobble` (up to 50 per request), `track.updateNowPlaying`
|
||||
- `user.getInfo`, `user.getRecentTracks`, `user.getTopArtists`, `user.getTopAlbums`, `user.getTopTracks`
|
||||
|
||||
`/api/docs` lists the parameters, rules and example responses for each method.
|
||||
|
||||
Scrobbles are ignored (not rejected) if the artist or track is empty, or if the timestamp is more than 14 days old or more than 5 minutes in the future. Sending the same scrobble again is accepted and stored once, so client retries are safe.
|
||||
|
||||
## Importing Last.fm history
|
||||
|
||||
Open **Import** in the web UI. You can import from:
|
||||
|
||||
- **An export file**:
|
||||
- CSV from [lastfm-to-csv](https://benjaminbenben.com/lastfm-to-csv/), with no header row and artist, album, track and date columns.
|
||||
- Any CSV or TSV with a header row naming artist, track and a date or Unix-time column. Comma, semicolon and tab delimiters all work.
|
||||
- A JSON export of `user.getRecentTracks` pages (such as lastfm.ghan.nl's), a single `recenttracks` response, or a flat list of tracks.
|
||||
- Files can be gzipped.
|
||||
- **Last.fm directly**: enter a Last.fm username and the worker pages through that account's public history. This option only appears when the server has `LASTFM_API_KEY` set.
|
||||
|
||||
Imports run in the background in the `worker` service. The page shows progress, and an import can be cancelled.
|
||||
|
||||
Unlike live scrobbles, imports keep plays of any age. They still skip rows with no artist or track, and plays dated in the future. Plays already in your history are skipped, so importing again is safe.
|
||||
|
||||
## Sharing on the fediverse
|
||||
|
||||
Users can make their profile findable from Mastodon and other fediverse apps as `@name@<your UI host>`, and be followed:
|
||||
|
||||
- Follows are accepted automatically, or wait for approval if the user turns that on.
|
||||
- The **Followers** page lists requests, followers and blocked accounts, with approve, decline, remove, block and unblock.
|
||||
|
||||
Once someone follows, the **Sharing** page's **What to post** section controls what they see:
|
||||
|
||||
- **Weekly summary**: play count and top artist(s), posted once a week (Monday 09:00 in the user's own time zone, for the Monday–Sunday week that just ended). A live preview on the same page shows what it would say so far.
|
||||
- **Milestones**: total scrobbles, an artist's plays, and a new entry into the all-time top 10 — each posted once, the first time it's reached. History already on file when sharing is turned on, or added by an import, is never posted about retroactively.
|
||||
|
||||
Both are on by default and can be switched off independently. Posts use the same visibility as the profile (followers only by default, or unlisted/public); the **Posts** page lists a user's own posts with delete (sends a `Delete` to whoever received it).
|
||||
|
||||
The Sharing page's **Now playing** section is a separate, three-way choice:
|
||||
|
||||
- **Off** (default): nothing shown or posted.
|
||||
- **Show on my profile**: a "Now playing: *Track* by Artist" field on the profile, refreshed at most every 5 minutes while something's playing and cleared a few minutes after it stops. A `GET` of the profile always shows the truth right now; the push is just so already-following servers don't have to ask.
|
||||
- **Show on my profile and post**: the profile field, plus a post for each track played 30 seconds or more, at most one every 30 minutes, with the previous now-playing post deleted once the new one is confirmed published.
|
||||
|
||||
Federation is tested in CI against a real GoToSocial server (`scripts/interop/run.sh`), including that a posted weekly summary reaches a follower's timeline with the right content and visibility and disappears from it when deleted, and that turning now playing on updates the field GoToSocial has cached for the profile.
|
||||
|
||||
It's **off unless the server enables it**, and then **off for each user** until they switch it on under **Sharing**. To enable it:
|
||||
|
||||
1. Set these on the API and worker:
|
||||
- `FEDERATION_ENABLED=1`
|
||||
- `FEDERATION_DOMAIN`: the UI's host, e.g. `scrobble.example`
|
||||
- `FEDERATION_BASE_URL`: the UI's origin, e.g. `https://scrobble.example`
|
||||
- `FEDERATION_KEY_SECRET`: 32 or more random characters
|
||||
2. Give the UI container `API_INTERNAL_URL` (compose sets `http://api:8000`). It then forwards these paths to the API: `/.well-known/webfinger`, `/.well-known/nodeinfo`, `/.well-known/host-meta`, `/nodeinfo/`, `/users/`, `/inbox` and `/actor`.
|
||||
3. Serve the UI over HTTPS on that domain. Other servers only talk to HTTPS.
|
||||
|
||||
Handles and actor URLs **can't change once anyone follows** a user, so choose the domain carefully.
|
||||
|
||||
`FEDERATION_KEY_SECRET` encrypts each user's signing key. **Back it up, and never change it**: losing it means users' keys can't be read, and followers would have to follow again.
|
||||
|
||||
Optional settings:
|
||||
|
||||
- `FEDERATION_BLOCKED_DOMAINS`: comma-separated domains, including subdomains, whose deliveries and follows are refused.
|
||||
- `FEDERATION_DELIVERY_CONCURRENCY`: parallel outgoing deliveries per worker (default 4).
|
||||
- `FEDERATION_SIGNATURE_SCHEMES`: the signing order (default `draft-cavage,rfc9421`).
|
||||
|
||||
The worker must be running: it processes incoming activities and delivers outgoing ones.
|
||||
|
||||
## Develop
|
||||
|
||||
```sh
|
||||
docker compose up -d db # Postgres with a scrobbler_test database
|
||||
uv sync
|
||||
uv run flask --app scrobbler db upgrade heads
|
||||
uv run flask --app scrobbler run --port 5050 # API (port 5000 is taken by AirPlay on macOS)
|
||||
python -m http.server 8080 -d frontend # UI
|
||||
uv run flask --app scrobbler worker # background work (imports, …)
|
||||
uv run pytest # needs TEST_DATABASE_URL (see .env.example)
|
||||
uv run ruff check && uv run ruff format --check
|
||||
```
|
||||
|
||||
- **Migrations** come in two branches, `core` and `federation`. After changing core models, run `uv run flask --app scrobbler db migrate --head core@head -m "…"`; federation tables use `--head federation@head` (see `src/scrobbler/federation/README.md`). Apply them with `db upgrade heads`. CI fails if the models and migrations disagree.
|
||||
- **API docs**: after changing an endpoint, run `uv run python -m scrobbler.openapi.export docs/openapi.json`. It documents the API with every optional module (such as federation) switched on. A test fails if the committed spec is stale. Every route has to appear in the spec, and every UI route has to declare bearer auth.
|
||||
- **Demo traffic**: `uv run python scripts/generate_traffic.py --minutes 5` sends a realistic mix of scrobbles and errors, so the dashboards have something to show.
|
||||
|
||||
The test suite includes an end-to-end run with [pylast](https://github.com/pylast/pylast) over HTTPS and a real two-worker gunicorn run, which checks that metrics aggregate across workers.
|
||||
|
||||
## Monitoring
|
||||
|
||||
The API serves Prometheus metrics on `METRICS_PORT`, summed across gunicorn workers. All series are prefixed `scrobbler_`:
|
||||
|
||||
- **HTTP**: request rate, errors and latency per endpoint
|
||||
- **Last.fm**: calls, errors by Last.fm error code, and authentication failures by reason
|
||||
- **Scrobbles**: accepted, duplicate and ignored (with the reason), batch size, and how late scrobbles arrive
|
||||
- **Database**: query latency and connection-pool use
|
||||
- **Users**: registered, active, and listening now (computed from the database and cached for 60s)
|
||||
|
||||
Labels never contain users, keys or track names.
|
||||
|
||||
With the `monitoring` profile, Grafana (admin password `GRAFANA_ADMIN_PASSWORD`) has a **Scrobbler** folder with four dashboards:
|
||||
|
||||
- API overview
|
||||
- Last.fm API
|
||||
- Usage
|
||||
- Database
|
||||
|
||||
Alert rules live in `deploy/prometheus/alerts.yml`:
|
||||
|
||||
- API down
|
||||
- import worker down, or imports left waiting
|
||||
- 5xx ratio
|
||||
- p95 latency
|
||||
- authentication-failure spike
|
||||
- ignored-scrobble ratio
|
||||
- database pool saturated
|
||||
- Postgres down
|
||||
|
||||
They're evaluated by Prometheus and appear in Grafana. Nothing routes them anywhere yet; that needs Alertmanager.
|
||||
|
||||
Tests check that every metric a dashboard or alert uses actually exists.
|
||||
|
||||
## CI/CD (Gitea Actions)
|
||||
|
||||
Everything runs from `.gitea/workflows/ci.yml`:
|
||||
|
||||
- **Every push and PR**:
|
||||
- lint
|
||||
- the migrations check
|
||||
- tests against Postgres
|
||||
- `promtool` checks of the Prometheus config and alert rules
|
||||
- **Pushes to `main` and `v*` tags**, once every check has passed:
|
||||
- both images are pushed to the ProGet `scrobbler` Docker feed. `main` builds are tagged `main` and `sha-<commit>`; tags are tagged `<version>`, `latest` and `sha-<commit>`.
|
||||
- the OpenAPI spec, dashboards and Prometheus config are uploaded to the `scrobbler-assets` feed under `scrobbler/<version>/` or `scrobbler/main/`.
|
||||
- **`v*` tags** also publish the wheel and sdist to the `scrobbler-python` PyPI feed, as the `scrobby` distribution. The import name is `scrobbler`. (`scrobbler` on public PyPI is an unrelated project.)
|
||||
- **Every run** is posted to the `ScrobblingService` topic on notify.daubney.dev.
|
||||
|
||||
The workflow needs these repository secrets and variables:
|
||||
|
||||
- **Secrets**: `PKGS_USER`, `PKGS_PASSWORD`, `PKGS_API_KEY`, `NTFY_USER`, `NTFY_PASSWORD`
|
||||
- **Variables**: `PKGS_HOST`, `PKGS_DOCKER_FEED`, `PKGS_PYPI_FEED`, `PKGS_ASSET_FEED`, `NTFY_URL`, `NTFY_TOPIC`
|
||||
|
||||
To release, tag `vX.Y.Z` on `main` and push the tag. The tag sets the package and image version.
|
||||
Open http://localhost:8080, create an account, then go to **Apps** and create an app for
|
||||
a player. See [docs/deploy.md](docs/deploy.md) for the published images, full
|
||||
configuration reference and production notes.
|
||||
|
||||
## Licence
|
||||
|
||||
BSD 3-Clause; see [LICENSE](LICENSE). The fonts vendored in `frontend/fonts/` are under the SIL Open Font License 1.1.
|
||||
BSD 3-Clause; see [LICENSE](LICENSE). The fonts vendored in `frontend/fonts/` are under
|
||||
the SIL Open Font License 1.1.
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
# Deploying Scrobbler
|
||||
|
||||
This covers running a real instance: the published images, configuration, putting it
|
||||
behind TLS, monitoring, backups and releases. For running the code locally to work on
|
||||
it, see [setup.md](setup.md). For what the service does once it's running, see
|
||||
[usage.md](usage.md).
|
||||
|
||||
## Services
|
||||
|
||||
```
|
||||
players / scrobblers ──► API /2.0/ Last.fm-compatible
|
||||
web UI (static files) ──► API /api/v1/ JSON API for the UI
|
||||
API ──► PostgreSQL
|
||||
Prometheus ◄── API :9100/metrics, postgres-exporter ──► Grafana
|
||||
```
|
||||
|
||||
- **`api`**: Flask under gunicorn. API-only; it serves no HTML pages.
|
||||
- **`ui`**: nginx serving the static frontend (`frontend/`, no build step).
|
||||
- **`worker`**: `flask worker` — history imports and background tasks for optional
|
||||
modules (federation's deliveries, scheduled posts, and so on).
|
||||
- **`db`**: PostgreSQL.
|
||||
- With the `monitoring` profile: `prometheus`, `grafana`, `postgres-exporter`.
|
||||
|
||||
## Quick start
|
||||
|
||||
```sh
|
||||
cp .env.example .env # then set SECRET_KEY and POSTGRES_PASSWORD
|
||||
SCROBBLER_IMAGE_TAG=main docker compose up -d --no-build # or drop --no-build to build locally
|
||||
```
|
||||
|
||||
Open `http://localhost:8080`, create an account, then go to **Apps** and create an app
|
||||
for each player. Add `--profile monitoring` for Prometheus and Grafana.
|
||||
|
||||
Published images: `pkgs.daubney.dev/scrobbler/scrobbler-api` and
|
||||
`pkgs.daubney.dev/scrobbler/scrobbler-ui`, tagged `main` (latest push to `main`),
|
||||
`sha-<commit>`, and `<version>`/`latest` for tagged releases.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Used by | Default | Purpose |
|
||||
|---|---|---|---|
|
||||
| `DATABASE_URL` | api | local Postgres | SQLAlchemy URL (`postgresql+psycopg://…`) |
|
||||
| `SECRET_KEY` | api | `dev-insecure-secret` | Flask secret — set a real one |
|
||||
| `CORS_ORIGINS` | api | `http://localhost:8080` | Comma-separated origins allowed to call `/api/v1`: wherever browsers actually load the UI from |
|
||||
| `UI_BASE_URL` | api | `http://localhost:8080` | Where `/api/auth/` sends users to approve desktop sign-ins, and (with federation on) the public identity users are found under |
|
||||
| `OPENAPI_DOCS_ENABLED` | api | `1` | Set to `0` to hide `/api/docs` and `/api/redoc` |
|
||||
| `METRICS_PORT` | api | `9100` | Prometheus metrics port — keep it off the public network |
|
||||
| `WEB_CONCURRENCY` | api | `2` | gunicorn workers |
|
||||
| `RUN_MIGRATIONS` | api image | `1` | Apply database migrations on start |
|
||||
| `LASTFM_API_KEY` | api, worker | *(unset)* | Enables importing straight from Last.fm ([get a key](https://www.last.fm/api/account/create)) |
|
||||
| `IMPORT_MAX_BYTES` | api | 200 MB | Largest export file accepted |
|
||||
| `WORKER_METRICS_PORT` | worker | `9101` | The worker's Prometheus metrics port |
|
||||
| `API_BASE_URL` | ui image | `http://localhost:5050` | Where the browser reaches the API — written into `config.js` and the CSP |
|
||||
|
||||
In `docker compose`, host ports are set with `DB_PORT`, `API_PORT`, `UI_PORT`,
|
||||
`PROMETHEUS_PORT` and `GRAFANA_PORT`.
|
||||
|
||||
`CORS_ORIGINS` and `UI_BASE_URL` answer different questions and can differ: the first is
|
||||
*which browser origins may call the JSON API*, the second is *where the public identity
|
||||
and sign-in redirects point*. Don't assume setting one sets the other — `docker-compose.yml`
|
||||
passes them through independently.
|
||||
|
||||
### Fediverse sharing (optional)
|
||||
|
||||
Off unless the server enables it, and then off for each user until they turn it on:
|
||||
|
||||
1. Set on the **api** and **worker**:
|
||||
- `FEDERATION_ENABLED=1`
|
||||
- `FEDERATION_DOMAIN`: the UI's host, e.g. `scrobble.example`
|
||||
- `FEDERATION_BASE_URL`: the UI's origin, e.g. `https://scrobble.example`
|
||||
- `FEDERATION_KEY_SECRET`: 32+ random characters
|
||||
2. Give the **ui** container `API_INTERNAL_URL` (compose sets `http://api:8000`). It
|
||||
forwards these paths to the API: `/.well-known/webfinger`, `/.well-known/nodeinfo`,
|
||||
`/.well-known/host-meta`, `/nodeinfo/`, `/users/`, `/inbox` and `/actor`.
|
||||
3. Serve the UI over **real HTTPS** on that domain — other fediverse servers only talk to
|
||||
HTTPS, and won't accept a self-signed certificate.
|
||||
|
||||
Handles and actor URLs **can't change once anyone follows a user**, so choose the domain
|
||||
carefully before enabling this in production.
|
||||
|
||||
`FEDERATION_KEY_SECRET` encrypts every user's signing key. **Back it up, and never
|
||||
change it**: losing it means users' keys can't be read, and followers would have to
|
||||
follow again.
|
||||
|
||||
Optional: `FEDERATION_BLOCKED_DOMAINS` (comma-separated, subdomains included),
|
||||
`FEDERATION_DELIVERY_CONCURRENCY` (default 4), `FEDERATION_SIGNATURE_SCHEMES` (default
|
||||
`draft-cavage,rfc9421`). The `worker` service must be running — it delivers outgoing
|
||||
activities and processes incoming ones.
|
||||
|
||||
## Putting it behind TLS
|
||||
|
||||
Scrobbler itself only speaks plain HTTP; something in front of it needs to terminate
|
||||
TLS. Any reverse proxy works — nginx, Caddy, or a tunnel like Cloudflare Tunnel for
|
||||
testing without a public IP (see `docs/design/activitypub-phase4.md` for how the
|
||||
project's own manual federation test used one). A minimal Caddy example, proxying
|
||||
everything to the `ui` container (which itself forwards the fediverse paths to `api`):
|
||||
|
||||
```
|
||||
scrobble.example {
|
||||
reverse_proxy localhost:8080
|
||||
}
|
||||
```
|
||||
|
||||
Point `UI_BASE_URL`/`FEDERATION_BASE_URL`/`FEDERATION_DOMAIN` at that same public
|
||||
`https://scrobble.example` origin. The API (`API_PORT`, default 5050) doesn't need its
|
||||
own public hostname unless you want browsers to reach it directly instead of through the
|
||||
UI's proxy — set `API_BASE_URL` accordingly either way, since the browser calls it
|
||||
directly for `/api/v1`.
|
||||
|
||||
## Monitoring
|
||||
|
||||
The API serves Prometheus metrics on `METRICS_PORT`, summed across gunicorn workers. All
|
||||
series are prefixed `scrobbler_`: HTTP request rate/errors/latency, Last.fm calls and
|
||||
auth failures, scrobble outcomes, database query latency and pool use, and gauges for
|
||||
registered/active/currently-listening users. Labels never contain usernames, API keys or
|
||||
track names.
|
||||
|
||||
With `--profile monitoring`, Grafana (`GRAFANA_ADMIN_PASSWORD`) provisions a
|
||||
**Scrobbler** folder with dashboards for API overview, Last.fm API, usage, database and
|
||||
(with federation on) federation. Alert rules live in `deploy/prometheus/alerts.yml` —
|
||||
evaluated by Prometheus and visible in Grafana, but nothing routes them anywhere without
|
||||
Alertmanager.
|
||||
|
||||
## Backups
|
||||
|
||||
Back up, at minimum:
|
||||
|
||||
- The Postgres database (all scrobble history, accounts, federation state).
|
||||
- `FEDERATION_KEY_SECRET`, if federation is on — it's the only thing that can decrypt
|
||||
stored signing keys, and it's never derivable from the database alone.
|
||||
|
||||
## Releases
|
||||
|
||||
Gitea Actions (`.gitea/workflows/ci.yml`) builds and publishes on every push to `main`
|
||||
and on `v*` tags, once lint, tests and the Prometheus config checks all pass:
|
||||
|
||||
- Both images push to the ProGet `scrobbler` Docker feed.
|
||||
- The OpenAPI spec, dashboards and Prometheus config upload to the `scrobbler-assets`
|
||||
feed.
|
||||
- `v*` tags also publish the wheel/sdist to the `scrobbler-python` PyPI feed (as
|
||||
`scrobby`; `scrobbler` on public PyPI is an unrelated project).
|
||||
|
||||
To release: tag `vX.Y.Z` on `main` and push the tag. That tag sets both the package and
|
||||
image version. See [setup.md](setup.md) for the secrets/variables the workflow needs and
|
||||
how to run it locally before tagging.
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
# Working on Scrobbler
|
||||
|
||||
Setting up a local development environment. For running a real instance, see
|
||||
[deploy.md](deploy.md). For what the service does, see [usage.md](usage.md).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- [uv](https://docs.astral.sh/uv/) for Python dependencies
|
||||
- Docker, for Postgres (and optionally the full stack)
|
||||
|
||||
## Get running
|
||||
|
||||
```sh
|
||||
docker compose up -d db # Postgres, with a scrobbler_test database too
|
||||
uv sync
|
||||
uv run flask --app scrobbler db upgrade heads
|
||||
uv run flask --app scrobbler run --port 5050 # API (port 5000 is taken by AirPlay on macOS)
|
||||
python -m http.server 8080 -d frontend # UI
|
||||
uv run flask --app scrobbler worker # background work: imports, federation deliveries, …
|
||||
```
|
||||
|
||||
`TEST_DATABASE_URL` (see `.env.example`) points the test suite at `scrobbler_test`.
|
||||
|
||||
```sh
|
||||
uv run pytest
|
||||
uv run ruff check && uv run ruff format --check
|
||||
```
|
||||
|
||||
The test suite includes an end-to-end run with [pylast](https://github.com/pylast/pylast)
|
||||
over HTTPS and a real two-worker gunicorn run, checking that metrics aggregate across
|
||||
workers. `TestConfig` pins every setting a developer might have in their own `.env`
|
||||
(`FEDERATION_ENABLED`, `LASTFM_API_KEY`, `UI_BASE_URL`, `CORS_ORIGINS`, …) so a local
|
||||
`.env` used for manual testing can never change what the test suite exercises — if you
|
||||
add a new setting that a test might reasonably assert an exact value for, pin it there
|
||||
too rather than relying on its default.
|
||||
|
||||
## Migrations
|
||||
|
||||
Two Alembic branches, `core` and `federation`, so federation's tables can be changed or
|
||||
dropped without touching core ones:
|
||||
|
||||
```sh
|
||||
uv run flask --app scrobbler db migrate --head core@head -m "…" # core models
|
||||
uv run flask --app scrobbler db migrate --head federation@head -m "…" # federation models
|
||||
uv run flask --app scrobbler db upgrade heads # apply both
|
||||
```
|
||||
|
||||
CI fails if the models and migrations disagree. See
|
||||
`src/scrobbler/federation/README.md` for more on the federation branch specifically
|
||||
(it also enforces that federation stays an optional, replaceable module — an
|
||||
AST-based boundary test checks what it's allowed to import).
|
||||
|
||||
## API docs
|
||||
|
||||
After changing an endpoint:
|
||||
|
||||
```sh
|
||||
uv run python -m scrobbler.openapi.export docs/openapi.json
|
||||
```
|
||||
|
||||
It documents the API with every optional module (federation included) switched on. A
|
||||
test fails if the committed spec is stale, if any route is missing from it, or if any UI
|
||||
route is missing its bearer-auth declaration.
|
||||
|
||||
## Useful scripts
|
||||
|
||||
- `uv run python scripts/generate_traffic.py --minutes 5` — sends a realistic mix of
|
||||
scrobbles and errors, so the Grafana dashboards have something to show.
|
||||
- `scripts/interop/run.sh` — runs Scrobbler against a real GoToSocial server on a private
|
||||
network and checks the whole federation follow/post/now-playing life cycle end to end.
|
||||
CI runs this on every push; run it locally the same way before changing anything in
|
||||
`scrobbler.federation`.
|
||||
|
||||
## Manual fediverse test
|
||||
|
||||
The interop test above proves the protocol works against one real server on a private
|
||||
network. It can't prove WebFinger resolves from the public internet, or that a real
|
||||
Mastodon instance renders things sensibly. Before merging federation changes that touch
|
||||
anything user-visible, do a manual pass with a real account through a tunnel
|
||||
(`cloudflared tunnel --url http://localhost:8080` needs no account for a quick HTTPS
|
||||
URL) — see `docs/design/activitypub-phase4.md` for what was checked and two real bugs
|
||||
that manual pass caught that no automated test had.
|
||||
|
||||
## CI/CD
|
||||
|
||||
Everything runs from `.gitea/workflows/ci.yml`:
|
||||
|
||||
- **Every push and PR**: lint, the migrations-vs-models check, tests against Postgres,
|
||||
and `promtool` checks of the Prometheus config and alert rules.
|
||||
- **Pushes to `main` and tags**, once those all pass: images and assets publish (see
|
||||
[deploy.md](deploy.md#releases)).
|
||||
- **Every run** posts to the `ScrobblingService` topic on notify.daubney.dev.
|
||||
|
||||
Repository secrets: `PKGS_USER`, `PKGS_PASSWORD`, `PKGS_API_KEY`, `NTFY_USER`,
|
||||
`NTFY_PASSWORD`. Variables: `PKGS_HOST`, `PKGS_DOCKER_FEED`, `PKGS_PYPI_FEED`,
|
||||
`PKGS_ASSET_FEED`, `NTFY_URL`, `NTFY_TOPIC`.
|
||||
|
||||
## Conventions
|
||||
|
||||
- Keep the web UI (`frontend/`) entirely outside the API stack: Flask serves JSON only,
|
||||
never HTML.
|
||||
- Every endpoint needs an OpenAPI entry and Prometheus metrics; every new dashboard
|
||||
metric needs a Grafana panel (or an existing one it plausibly extends).
|
||||
- Commit at logical checkpoints through a piece of work, not only at the end.
|
||||
- BSD-3-Clause licensed; avoid GPL/AGPL dependencies.
|
||||
@@ -0,0 +1,94 @@
|
||||
# Using Scrobbler
|
||||
|
||||
What the service does, once it's running. For getting an instance running, see
|
||||
[deploy.md](deploy.md); for working on the code, see [setup.md](setup.md).
|
||||
|
||||
## Account and apps
|
||||
|
||||
Register in the web UI, then go to **Apps** and create one app per player or scrobbler
|
||||
client you use — each gets its own API key and shared secret, and can be revoked on its
|
||||
own from the **Sessions** page without affecting the others.
|
||||
|
||||
## Connecting a player
|
||||
|
||||
In the player's scrobbling settings, choose a custom or self-hosted Last.fm server:
|
||||
|
||||
- **API URL**: `https://your-host/2.0/`
|
||||
- **API key and shared secret**: from an app on the **Apps** page
|
||||
- **Username and password**: your Scrobbler account
|
||||
|
||||
Two sign-in flows are supported:
|
||||
|
||||
- **Mobile**: `auth.getMobileSession` with a username and password, sent as a POST body
|
||||
over HTTPS.
|
||||
- **Desktop**: `auth.getToken`, then the user approves at `/api/auth/` (redirects to the
|
||||
web UI), then `auth.getSession`.
|
||||
|
||||
The legacy `authToken = md5(username + md5(password))` form isn't supported — checking
|
||||
it would mean storing unsalted password hashes. pylast's `password_hash` option uses
|
||||
that form; use `SessionKeyGenerator` (the desktop flow) instead.
|
||||
|
||||
Supported methods: `auth.getMobileSession`, `auth.getToken`, `auth.getSession`,
|
||||
`track.scrobble` (up to 50 per request), `track.updateNowPlaying`, `user.getInfo`,
|
||||
`user.getRecentTracks`, `user.getTopArtists`, `user.getTopAlbums`, `user.getTopTracks`.
|
||||
`/api/docs` has the full parameters, rules and example responses for each.
|
||||
|
||||
A scrobble is ignored (not rejected) if the artist or track is empty, or the timestamp
|
||||
is more than 14 days old or more than 5 minutes in the future. Resending the same
|
||||
scrobble is accepted and stored once, so client retries are safe.
|
||||
|
||||
## Importing history
|
||||
|
||||
Open **Import** in the web UI:
|
||||
|
||||
- **An export file**:
|
||||
- CSV from [lastfm-to-csv](https://benjaminbenben.com/lastfm-to-csv/), no header row,
|
||||
artist/album/track/date columns.
|
||||
- Any CSV or TSV with a header row naming artist, track and a date or Unix-time
|
||||
column. Comma, semicolon and tab delimiters all work.
|
||||
- A JSON export of `user.getRecentTracks` pages (such as lastfm.ghan.nl's), a single
|
||||
`recenttracks` response, or a flat list of tracks.
|
||||
- Files can be gzipped.
|
||||
- **Last.fm directly**: enter a Last.fm username and the worker pages through that
|
||||
account's public history. Only appears when the server has `LASTFM_API_KEY` set.
|
||||
|
||||
Imports run in the background; the page shows progress and can cancel one in flight.
|
||||
Unlike live scrobbles, imports keep plays of any age (still skipping rows with no artist
|
||||
or track, or plays dated in the future). Plays already in your history are skipped, so
|
||||
importing the same export again is safe.
|
||||
|
||||
## Sharing on the fediverse
|
||||
|
||||
Only available when the server has federation enabled (ask whoever runs it, or see
|
||||
[deploy.md](deploy.md#fediverse-sharing-optional) if that's you). Turn it on under
|
||||
**Sharing**, and you're findable as `@name@<the server's host>` from Mastodon and other
|
||||
fediverse apps:
|
||||
|
||||
- Follows are accepted automatically, or wait for your approval if you turn that on.
|
||||
- **Followers** lists requests, followers and blocked accounts, with approve, decline,
|
||||
remove, block and unblock.
|
||||
|
||||
Once someone follows, **Sharing**'s **What to post** section controls what they see:
|
||||
|
||||
- **Weekly summary**: play count and top artist(s), posted once a week (Monday 09:00 in
|
||||
your own time zone, for the week that just ended). A live preview shows what it would
|
||||
say so far.
|
||||
- **Milestones**: total scrobbles, an artist's plays, and a new entry into your all-time
|
||||
top 10 — each posted once, the first time it's reached. History already on file when
|
||||
you turn sharing on, or added by an import, is never posted about retroactively.
|
||||
|
||||
Both are on by default and can be switched off independently. Posts use the same
|
||||
visibility as your profile (followers only by default, or unlisted/public); **Posts**
|
||||
lists your own posts with delete (sends a `Delete` to whoever received it).
|
||||
|
||||
**Now playing** is a separate, three-way choice:
|
||||
|
||||
- **Off** (default): nothing shown or posted.
|
||||
- **Show on my profile**: a "Now playing: *Track* by Artist" field, refreshed at most
|
||||
every 5 minutes while something's playing and cleared a few minutes after it stops.
|
||||
- **Show on my profile and post**: the profile field, plus a post for each track played
|
||||
30 seconds or more, at most one every 30 minutes, with the previous now-playing post
|
||||
deleted once the new one is confirmed published.
|
||||
|
||||
Handles and actor URLs **can't change once anyone follows you**, so if you're the one
|
||||
who gets to choose the server's domain, choose it carefully.
|
||||
Reference in new issue
Block a user