#!/usr/bin/env bash # Sanity-check a Yandex Music OAuth token and both stream-signing schemes # from the Mac, before blaming the Apple TV. # # ./tools/check-api.sh [TRACK_ID] set -uo pipefail TOKEN="${1:-}" TRACK="${2:-}" API="https://api.music.yandex.net" CLIENT="YandexMusicAndroid/24023621" WEB_SECRET="${YM_WEB_SECRET:-7tvSmFbyf5hJnIHhCimDDD}" LEGACY_SALT="XGRlBW9FXlekgbPrRHuSiA" if [[ -z "$TOKEN" ]]; then echo "usage: $0 [TRACK_ID]" >&2 exit 2 fi api() { curl -sS -H "Authorization: OAuth $TOKEN" \ -H "X-Yandex-Music-Client: $CLIENT" \ -H "User-Agent: Yandex-Music-API" \ "$@" } say() { printf "\n\033[1m== %s\033[0m\n" "$1"; } say "account/status" STATUS=$(api "$API/account/status") UID_=$(printf '%s' "$STATUS" | python3 -c 'import sys,json;print(json.load(sys.stdin)["result"]["account"].get("uid",""))' 2>/dev/null) if [[ -z "$UID_" ]]; then echo "FAILED - token rejected:"; printf '%s' "$STATUS" | head -c 500; echo; exit 1 fi printf '%s' "$STATUS" | python3 -c ' import sys, json r = json.load(sys.stdin)["result"] a = r["account"] print("uid: ", a.get("uid")) print("login: ", a.get("login")) print("name: ", a.get("displayName") or a.get("fullName")) print("has plus: ", (r.get("plus") or {}).get("hasPlus")) ' if [[ -z "$TRACK" ]]; then say "likes/tracks" LIKES=$(api "$API/users/$UID_/likes/tracks") TRACK=$(printf '%s' "$LIKES" | python3 -c ' import sys, json t = json.load(sys.stdin)["result"]["library"]["tracks"] sys.stderr.write("%d liked tracks\n" % len(t)) print(t[0]["id"] if t else "") ') fi if [[ -z "$TRACK" ]]; then echo "No track to test the stream with - pass TRACK_ID as the second argument." exit 0 fi echo "track under test: $TRACK" say "get-file-info (HMAC-SHA256, lossless)" TS=$(date +%s) QUALITY="lossless" CODECS="flac,aac,he-aac,mp3" TRANSPORT="raw" # The signature joins the codec list WITHOUT commas; the URL keeps them. MSG="${TS}${TRACK}${QUALITY}${CODECS//,/}${TRANSPORT}" SIGN=$(printf '%s' "$MSG" | openssl dgst -sha256 -hmac "$WEB_SECRET" -binary | base64 | tr -d '=') SIGN_ENC=$(python3 -c 'import sys,urllib.parse;print(urllib.parse.quote(sys.argv[1],safe=""))' "$SIGN") RESP=$(curl -sS -H "Authorization: OAuth $TOKEN" \ -H "X-Yandex-Music-Client: YandexMusicWebNext/1.0.0" \ -H "X-Yandex-Music-Without-Invocation-Info: 1" \ -H "Origin: https://music.yandex.ru" \ -H "Referer: https://music.yandex.ru/" \ "$API/get-file-info?ts=$TS&trackId=$TRACK&quality=$QUALITY&codecs=flac%2Caac%2Che-aac%2Cmp3&transports=$TRANSPORT&sign=$SIGN_ENC") printf '%s' "$RESP" | head -c 700; echo FI_URL=$(printf '%s' "$RESP" | python3 -c ' import sys, json try: d = json.load(sys.stdin) except Exception: print(""); sys.exit() info = d.get("downloadInfo") or (d.get("result") or {}).get("downloadInfo") or {} url = info.get("url") or (info.get("urls") or [""])[0] print(url) ') if [[ -n "$FI_URL" ]]; then echo "probe: $(curl -o /dev/null -sS -w '%{http_code} %{content_type} %{size_download}B' -r 0-2047 "$FI_URL")" fi say "download-info (MD5, mp3)" DI=$(api "$API/tracks/$TRACK/download-info") INFO_URL=$(printf '%s' "$DI" | python3 -c ' import sys, json try: items = json.load(sys.stdin)["result"] except Exception: print(""); sys.exit() items = [i for i in items if not i.get("preview")] if not items: print(""); sys.exit() best = max(items, key=lambda i: i.get("bitrateInKbps") or 0) print(best.get("downloadInfoUrl", "")) ') if [[ -z "$INFO_URL" ]]; then echo "download-info empty:"; printf '%s' "$DI" | head -c 400; echo else XML=$(api "$INFO_URL") read -r HOST XPATH XTS XS <<<"$(printf '%s' "$XML" | python3 -c ' import sys, xml.dom.minidom as m d = m.parseString(sys.stdin.read()) g = lambda t: d.getElementsByTagName(t)[0].firstChild.data print(g("host"), g("path"), g("ts"), g("s")) ')" MD5=$(printf '%s' "${LEGACY_SALT}${XPATH:1}${XS}" | openssl md5 -r | cut -d' ' -f1) URL="https://$HOST/get-mp3/$MD5/$XTS$XPATH" echo "direct: $URL" echo "probe: $(curl -o /dev/null -sS -w '%{http_code} %{content_type} %{size_download}B' -r 0-2047 "$URL")" fi