0.6: Spotify OAuth connect flow, AzuraCast base URL, fingerprint/buy-url fixes
- Add "Connect Spotify" OAuth flow (/connect/spotify) so newly created Spotify apps can read playlists: Spotify now requires a user token for playlist reads, which client-credentials alone can no longer provide. The stored refresh token is rendered as spotify-refresh into each playlist's sldl .conf -- sldl's own docs confirm supplying it skips its interactive login flow, which is what was causing multi-hour hangs on a stuck sync. Grandfathered apps keep working unchanged if no account is connected. - Fix the connect flow's redirect_uri: request.url_for() reflected the raw connection scheme (http) rather than what the reverse proxy actually served over, which Spotify's exact-match redirect_uri check rejected. Force https rather than trust the unproxied scheme. - Add an AzuraCast base URL credential field alongside the API key, with a keyfile fallback so the scheduled enrich-buy-url.py job can actually reach AzuraCast (previously it had no way to receive a base URL at all when run from the scheduler, so the reprocess call was silently always skipped). - Fix build-fingerprint-index.py exiting non-zero on any single fingerprint failure instead of only when nothing was written. - Guard enrich-buy-url.py's AzuraCast reprocess against an empty --azuracast-base (raised ValueError since PD2 removed the hardcoded base). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,19 +12,27 @@ import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
_TOKEN_URL = "https://accounts.spotify.com/api/token"
|
||||
_cache: dict[tuple[str, str], str] = {}
|
||||
_cache: dict[tuple[str, str, str | None], str] = {}
|
||||
|
||||
|
||||
def get_token(client_id: str, client_secret: str) -> str:
|
||||
"""Return a client-credentials access token, cached per (id, secret) for
|
||||
the life of the process."""
|
||||
key = (client_id, client_secret)
|
||||
def get_token(client_id: str, client_secret: str, refresh_token: str | None = None) -> str:
|
||||
"""Return an access token, cached per (id, secret, refresh_token) for the
|
||||
life of the process. If refresh_token is set (an account has been
|
||||
connected via /connect/spotify), mints a user token -- required for
|
||||
playlist reads on newly created Spotify apps, which reject
|
||||
client-credentials tokens. Otherwise falls back to client-credentials."""
|
||||
key = (client_id, client_secret, refresh_token)
|
||||
if key in _cache:
|
||||
return _cache[key]
|
||||
creds = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()
|
||||
grant = (
|
||||
{"grant_type": "refresh_token", "refresh_token": refresh_token}
|
||||
if refresh_token
|
||||
else {"grant_type": "client_credentials"}
|
||||
)
|
||||
req = urllib.request.Request(
|
||||
_TOKEN_URL,
|
||||
data=urllib.parse.urlencode({"grant_type": "client_credentials"}).encode(),
|
||||
data=urllib.parse.urlencode(grant).encode(),
|
||||
headers={
|
||||
"Authorization": f"Basic {creds}",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
|
||||
@@ -189,7 +189,7 @@ def main() -> int:
|
||||
elapsed = time.time() - start
|
||||
print(f"[index] done in {elapsed:.0f}s. wrote {done - failed} rows, {failed} failed. "
|
||||
f"index now has {total} entries.", flush=True)
|
||||
return 0 if failed == 0 else 1
|
||||
return 0 if (done - failed) > 0 else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -294,6 +294,9 @@ def set_flac_tag(path, name, value):
|
||||
|
||||
# ---- AzuraCast reprocess (same as backfill-buy-url.py) ----------------------
|
||||
def az_reprocess(base, key, station, host_paths, library, log):
|
||||
if not base:
|
||||
log(" no AzuraCast base URL configured — skipping reprocess")
|
||||
return
|
||||
root = library.rstrip("/") + "/"
|
||||
want = {p[len(root):] for p in host_paths if p.startswith(root)}
|
||||
headers = {"X-API-Key": key, "Accept": "application/json"}
|
||||
@@ -365,7 +368,7 @@ def refresh_qobuz_links(flacs, args, az_key):
|
||||
f"{converted} {'converted' if args.apply else 'would-convert'}, "
|
||||
f"{unconvertible} left intact (not purchasable / unresolved)")
|
||||
|
||||
if args.apply and touched and az_key:
|
||||
if args.apply and touched and az_key and args.azuracast_base:
|
||||
print("[refresh-qobuz] telling AzuraCast to reprocess touched files...")
|
||||
az_reprocess(args.azuracast_base, az_key, args.station,
|
||||
touched, args.library, lambda m: print(m))
|
||||
@@ -415,6 +418,16 @@ def main():
|
||||
if os.path.exists(keyfile):
|
||||
az_key = Path(keyfile).read_text().strip()
|
||||
|
||||
# Same cascade for the base URL: --azuracast-base, then env, then the file
|
||||
# credential_service renders from Settings -> Credentials -> AzuraCast.
|
||||
# The scheduled job (scheduler_service) invokes this script with neither
|
||||
# the flag nor the env var set, so the keyfile is the only way it ever
|
||||
# gets one.
|
||||
if not args.azuracast_base:
|
||||
basefile = f"{_ALEMBIC_CONFIG_DIR}/pipeline/azuracast/base_url"
|
||||
if os.path.exists(basefile):
|
||||
args.azuracast_base = Path(basefile).read_text().strip()
|
||||
|
||||
cascade = [s.strip() for s in args.sources.split(",") if s.strip() in SOURCES]
|
||||
if not cascade:
|
||||
sys.exit(f"[enrich] no valid sources in {args.sources!r}")
|
||||
@@ -518,12 +531,12 @@ def main():
|
||||
f" no-match={looked - found}"
|
||||
+ (f" upgraded={upgraded}" if upgrade_from else ""))
|
||||
|
||||
if args.apply and touched and az_key:
|
||||
if args.apply and touched and az_key and args.azuracast_base:
|
||||
print("[enrich] telling AzuraCast to reprocess touched files...")
|
||||
az_reprocess(args.azuracast_base, az_key, args.station,
|
||||
touched, args.library, lambda m: print(m))
|
||||
elif args.apply and touched and not az_key:
|
||||
print("[enrich] no AzuraCast key (flag/env/keyfile) — tags written; "
|
||||
elif args.apply and touched and (not az_key or not args.azuracast_base):
|
||||
print("[enrich] no AzuraCast key/base URL (flag/env/keyfile) — tags written; "
|
||||
"AzuraCast's periodic media scan will pick them up.")
|
||||
elif not args.apply:
|
||||
print("[enrich] re-run with --apply to write (key via --azuracast-key, "
|
||||
|
||||
@@ -89,7 +89,8 @@ def _spotify_token():
|
||||
continue
|
||||
k, v = line.split("=", 1)
|
||||
env[k] = v.strip().strip("'").strip('"')
|
||||
return get_token(env["SPOTIFY_CLIENT_ID"], env["SPOTIFY_CLIENT_SECRET"])
|
||||
return get_token(env["SPOTIFY_CLIENT_ID"], env["SPOTIFY_CLIENT_SECRET"],
|
||||
env.get("SPOTIFY_REFRESH_TOKEN") or None)
|
||||
|
||||
|
||||
def _spotify_get(path, token):
|
||||
|
||||
@@ -10,10 +10,21 @@ set -u
|
||||
CONFIGS="${ALEMBIC_CONFIG_DIR:-/config}/pipeline"
|
||||
LOG="${ALEMBIC_CONFIG_DIR:-/config}/logs/backfill-$(date +%Y%m%d-%H%M%S).log"
|
||||
RETAG="${PIPELINE_DIR:-/app/pipeline}/lib/spotify-retag.py"
|
||||
SPOTIFY_ENV="${ALEMBIC_CONFIG_DIR:-/config}/pipeline/_spotify.env"
|
||||
|
||||
mkdir -p "$(dirname "$LOG")"
|
||||
echo "[$(date -Iseconds)] === Backfill start ===" | tee -a "$LOG"
|
||||
|
||||
# Spotify credentials (client id/secret, and a refresh token if an account is
|
||||
# connected via /connect/spotify) are rendered once to _spotify.env as well as
|
||||
# into each playlist .conf; read from the single shared file here rather than
|
||||
# re-scraping every conf individually.
|
||||
[ -f "$SPOTIFY_ENV" ] && source "$SPOTIFY_ENV"
|
||||
if [[ -z "${SPOTIFY_CLIENT_ID:-}" || -z "${SPOTIFY_CLIENT_SECRET:-}" ]]; then
|
||||
echo "[$(date -Iseconds)] ERROR: Spotify credentials not found at $SPOTIFY_ENV" | tee -a "$LOG"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TOTAL_TRACKS=0
|
||||
TOTAL_PLAYLISTS=0
|
||||
|
||||
@@ -22,11 +33,9 @@ for conf in "$CONFIGS"/*.conf; do
|
||||
playlist=$(basename "$conf" .conf)
|
||||
|
||||
url=$(sed -n 's/^input *= *//p' "$conf" | tr -d ' ')
|
||||
cid=$(sed -n 's/^spotify-id *= *//p' "$conf" | tr -d ' ')
|
||||
csec=$(sed -n 's/^spotify-secret *= *//p' "$conf" | tr -d ' ')
|
||||
|
||||
if [[ -z "$url" || -z "$cid" || -z "$csec" ]]; then
|
||||
echo "[$playlist] SKIP: missing url/creds in conf" | tee -a "$LOG"
|
||||
if [[ -z "$url" ]]; then
|
||||
echo "[$playlist] SKIP: missing url in conf" | tee -a "$LOG"
|
||||
continue
|
||||
fi
|
||||
|
||||
@@ -41,7 +50,8 @@ for conf in "$CONFIGS"/*.conf; do
|
||||
fi
|
||||
|
||||
echo "[$playlist] retagging $count tracks via Spotify" | tee -a "$LOG"
|
||||
if echo "$paths" | SPOTIFY_CLIENT_ID="$cid" SPOTIFY_CLIENT_SECRET="$csec" \
|
||||
if echo "$paths" | SPOTIFY_CLIENT_ID="$SPOTIFY_CLIENT_ID" SPOTIFY_CLIENT_SECRET="$SPOTIFY_CLIENT_SECRET" \
|
||||
SPOTIFY_REFRESH_TOKEN="${SPOTIFY_REFRESH_TOKEN:-}" \
|
||||
python3 "$RETAG" "$url" - >> "$LOG" 2>&1; then
|
||||
echo "[$playlist] retag OK" | tee -a "$LOG"
|
||||
TOTAL_PLAYLISTS=$((TOTAL_PLAYLISTS + 1))
|
||||
|
||||
@@ -54,9 +54,9 @@ def _load_spotify_creds():
|
||||
continue
|
||||
k, v = line.split("=", 1)
|
||||
env[k] = v.strip().strip("'").strip('"')
|
||||
return env["SPOTIFY_CLIENT_ID"], env["SPOTIFY_CLIENT_SECRET"]
|
||||
return env["SPOTIFY_CLIENT_ID"], env["SPOTIFY_CLIENT_SECRET"], env.get("SPOTIFY_REFRESH_TOKEN") or None
|
||||
|
||||
SPOTIFY_CID, SPOTIFY_CSEC = _load_spotify_creds()
|
||||
SPOTIFY_CID, SPOTIFY_CSEC, SPOTIFY_REFRESH = _load_spotify_creds()
|
||||
WHITELIST_FILE = f"{ALEMBIC_CONFIG_DIR}/pipeline/genres-whitelist.txt"
|
||||
MAX_GENRES = 3
|
||||
SEPARATOR = "; "
|
||||
@@ -76,7 +76,7 @@ def load_whitelist():
|
||||
|
||||
|
||||
def _spotify_token():
|
||||
return get_token(SPOTIFY_CID, SPOTIFY_CSEC)
|
||||
return get_token(SPOTIFY_CID, SPOTIFY_CSEC, SPOTIFY_REFRESH)
|
||||
|
||||
|
||||
# Cache: artist_name → list[str] of Spotify genres (or None if not found)
|
||||
|
||||
@@ -22,6 +22,8 @@ Usage:
|
||||
spotify-retag.py <playlist_url> - # read newline-separated paths from stdin
|
||||
|
||||
Credentials are read from env vars SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET.
|
||||
SPOTIFY_REFRESH_TOKEN, if set, mints a user token instead of client-credentials
|
||||
(required for newly created Spotify apps -- see _spotify_auth.get_token).
|
||||
|
||||
Matching strategy (per file):
|
||||
1. Parse "Artist - Title.ext" from filename. If multiple " - " separators,
|
||||
@@ -223,6 +225,7 @@ def main() -> int:
|
||||
url, target = sys.argv[1], sys.argv[2]
|
||||
cid = os.environ.get("SPOTIFY_CLIENT_ID")
|
||||
csec = os.environ.get("SPOTIFY_CLIENT_SECRET")
|
||||
refresh = os.environ.get("SPOTIFY_REFRESH_TOKEN") or None
|
||||
if not cid or not csec:
|
||||
sys.exit("SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET must be set")
|
||||
|
||||
@@ -232,7 +235,7 @@ def main() -> int:
|
||||
files_iter = Path(target).rglob("*")
|
||||
|
||||
print(f"[spotify-retag] Fetching playlist {url}")
|
||||
token = get_token(cid, csec)
|
||||
token = get_token(cid, csec, refresh)
|
||||
tracks = fetch_playlist(url, token)
|
||||
print(f"[spotify-retag] Got {len(tracks)} tracks from Spotify")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user