Files
Scrobbler/src/scrobbler/config.py
T
mattandClaude Opus 5.5 6fc9d91fa2
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 41s
CI / python-package (push) Skipped
CI / assets (push) Successful in 8s
CI / images (push) Successful in 28s
CI / notify (push) Successful in 7s
History imports: parsers, import jobs and the import worker
- Import jobs (import_jobs table): uploaded exports stored gzipped,
  processed by `flask imports worker`, which claims jobs with
  SKIP LOCKED, inserts in chunks with progress, supports cancelling and
  re-queues jobs whose worker died
- CSV: lastfm-to-csv (no header) or any header-labelled CSV/TSV with
  comma, semicolon or tab delimiters; several date formats and Unix
  seconds/milliseconds
- JSON: pages of user.getRecentTracks (e.g. lastfm.ghan.nl), a single
  recenttracks response, or a flat list; gzipped files accepted
- Last.fm API pull (only when LASTFM_API_KEY is set): pages through
  user.getRecentTracks up to the job's creation time, rate limited,
  with retries and resume
- Imports keep plays older than 14 days, skip empty/future ones and
  dedupe against existing history
- Import metrics and pending/running job gauges

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 15:02:31 +01:00

76 lines
3.0 KiB
Python

import os
from dotenv import load_dotenv
# Settings are read at import time, so .env must be loaded first.
load_dotenv()
def _csv(value: str | None) -> list[str]:
return [item.strip() for item in (value or "").split(",") if item.strip()]
class Config:
SECRET_KEY = os.environ.get("SECRET_KEY", "dev-insecure-secret")
SQLALCHEMY_DATABASE_URI = os.environ.get(
"DATABASE_URL", "postgresql+psycopg://scrobbler:scrobbler@localhost:5432/scrobbler"
)
SQLALCHEMY_ENGINE_OPTIONS = {"pool_pre_ping": True}
CORS_ORIGINS = _csv(os.environ.get("CORS_ORIGINS", "http://localhost:8080"))
UI_BASE_URL = os.environ.get("UI_BASE_URL", "http://localhost:8080").rstrip("/")
METRICS_PORT = int(os.environ.get("METRICS_PORT", "9100"))
# Start the metrics HTTP server from create_app (dev server). Under gunicorn the
# server is started by gunicorn.conf.py instead, so this stays off there.
START_METRICS_SERVER = os.environ.get("START_METRICS_SERVER", "0") == "1"
# OpenAPI document and docs (flask-smorest)
API_TITLE = "Scrobbler API"
API_VERSION = "v1"
OPENAPI_VERSION = "3.1.0"
OPENAPI_URL_PREFIX = "/api"
OPENAPI_JSON_PATH = "openapi.json"
OPENAPI_DOCS_ENABLED = os.environ.get("OPENAPI_DOCS_ENABLED", "1") == "1"
OPENAPI_SWAGGER_UI_PATH = "docs" if OPENAPI_DOCS_ENABLED else None
OPENAPI_SWAGGER_UI_URL = os.environ.get(
"OPENAPI_SWAGGER_UI_URL", "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/"
)
OPENAPI_REDOC_PATH = "redoc" if OPENAPI_DOCS_ENABLED else None
OPENAPI_REDOC_URL = os.environ.get(
"OPENAPI_REDOC_URL", "https://cdn.jsdelivr.net/npm/redoc@2/bundles/redoc.standalone.js"
)
API_SPEC_OPTIONS = {
"info": {
"description": (
"Two APIs: `/2.0/` is Last.fm (Audioscrobbler 2.0) compatible, for players "
"and scrobblers; `/api/v1/` is the JSON API used by the web UI."
)
},
}
# History imports. Pulling from Last.fm is offered only when an API key is set
# (get one at https://www.last.fm/api/account/create).
LASTFM_API_KEY = os.environ.get("LASTFM_API_KEY", "")
LASTFM_API_URL = os.environ.get("LASTFM_API_URL", "https://ws.audioscrobbler.com/2.0/")
LASTFM_REQUEST_INTERVAL = float(os.environ.get("LASTFM_REQUEST_INTERVAL", "0.25"))
IMPORT_MAX_BYTES = int(os.environ.get("IMPORT_MAX_BYTES", str(200 * 1024 * 1024)))
MAX_CONTENT_LENGTH = IMPORT_MAX_BYTES + 1024 * 1024 # uploads are the largest requests
IMPORT_WORKER_METRICS_PORT = int(os.environ.get("IMPORT_WORKER_METRICS_PORT", "9101"))
# Last.fm behaviour
AUTH_TOKEN_TTL_SECONDS = 60 * 60
UI_TOKEN_TTL_DAYS = 30
SCROBBLE_MAX_AGE_DAYS = 14
SCROBBLE_MAX_FUTURE_SECONDS = 5 * 60
NOW_PLAYING_DEFAULT_SECONDS = 10 * 60
class TestConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = os.environ.get(
"TEST_DATABASE_URL",
"postgresql+psycopg://scrobbler:scrobbler@localhost:5432/scrobbler_test",
)
START_METRICS_SERVER = False