stuff/spotify/update-playlist.sh

50 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
MYSELF=$(realpath "$0")
MYDIR=$(dirname "$MYSELF")
. "$MYDIR"/get-tokens.sh
PLAYLIST_ID=${PLAYLIST_ID:-"2BoXyJeV0fhEHOy4uiByxr"}
TARGET=${TARGET:-}
if [ -z "$TARGET" ]; then
printf '\x1b[1;33m%s\x1b[0m\n' "W: No \$TARGET specified, using CWD"
TARGET=.
fi
PLAYLIST_JSON="$TARGET/playlist.json"
PLAYLIST_TXT="$TARGET/playlist.txt"
SUM_PLAYLIST_JSON="$TARGET/summary-playlist.json"
# i=0
iter_url=https://api.spotify.com/v1/playlists/$PLAYLIST_ID/tracks
while :; do
RES=$(curl --request GET \
--url "$iter_url" \
--header 'Authorization: Bearer '"$BEARER_TOKEN" \
--header 'client-token: '"$TOKEN" 2> /dev/null)
iter_url=$(jq -r .next <<< "$RES")
echo "$RES"
# _=$((i++))
[[ "$iter_url" == "null" ]] && break
done \
| pv \
| jq .items \
| jq -s 'reduce .[] as $x ([]; . + $x)' \
| jq 'del(.. | .available_markets?, .added_by?, .popularity?)' > "$PLAYLIST_JSON"
ARR=" "$'\x1b'"[32m=>"$'\x1b'"[0m"
printf "$ARR downloaded \x1b[33m%s\x1b[0m songs metadata at \x1b[35m%s\x1b[0m\n" \
"$(jq length "$PLAYLIST_JSON")" "$PLAYLIST_JSON"
jq -r '.[].track | ( .name + " - " + (.album.artists | map(.name) | join(", ")) )' "$PLAYLIST_JSON" \
> "$PLAYLIST_TXT"
printf "$ARR dumped it as text on \x1b[35m%s\x1b[0m\n" "$PLAYLIST_TXT"
jq -c '.[].track | ( [.id, .name, .album.name, (.album.artists | map(.name))] )' "$PLAYLIST_JSON" \
> "$SUM_PLAYLIST_JSON"
printf "$ARR dumped summary metadata on \x1b[35m%s\x1b[0m\n" "$SUM_PLAYLIST_JSON"