stuff/update-playlist.sh
2024-10-11 23:22:17 +02:00

33 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
PLAYLIST_ID="2BoXyJeV0fhEHOy4uiByxr"
BEARER_TOKEN_RES=$(
curl -L https://open.spotify.com/playlist/"$PLAYLIST_ID" \
| grep 'id="session"' \
| sed 's&.*<script id="session"[^>]*>&&' \
| sed 's/<.*//'
)
BEARER_TOKEN=$(echo "$BEARER_TOKEN_RES" | jq -r .accessToken)
GUEST_TOKEN_RES=$(
curl -X POST https://clienttoken.spotify.com/v1/clienttoken \
-H "Accept: application/json" -H "content-type: application/json" \
--data '{"client_data":{"client_version":"1.2.27.147.g503c721c","client_id":"d8a5ed958d274c2e8ee717e6a4b0971d","js_sdk_data":{"device_brand":"unknown","device_model":"unknown","os":"linux","os_version":"unknown","device_id":"nuhuh","device_type":"computer"}}}'
)
if ! [[ "$(echo "$GUEST_TOKEN_RES" | jq -r .response_type)" == "RESPONSE_GRANTED_TOKEN_RESPONSE" ]]; then
printf "\x1b[1;31m%s\x1b[0m\n" 'Token response is NOT granted' >&2
exit 1;
fi
TOKEN=$(echo "$GUEST_TOKEN_RES" | jq -r .granted_token.token)
echo "got bearer token $BEARER_TOKEN" >&2
echo "got token $TOKEN" >&2
curl --request GET \
--url https://api.spotify.com/v1/playlists/"$PLAYLIST_ID"/tracks \
--header 'Authorization: Bearer '"$BEARER_TOKEN" \
--header 'client-token: '"$TOKEN" | jq > playlist.json