Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
Last active August 21, 2022 16:52
Show Gist options
  • Save andrewrlee/9f98939a10b3307da5f47a60ad1cf174 to your computer and use it in GitHub Desktop.
Save andrewrlee/9f98939a10b3307da5f47a60ad1cf174 to your computer and use it in GitHub Desktop.
What Pixies can do in the time it takes Pearl Jam to play Rockin in the free world
#!/bin/bash
set -e
CLIENT_ID=…
CLIENT_SECRET=…
# ID for Pixies
ARTIST=6zvul52xwTWzilBZl6BUbT
DURATION_MS=$((15 * 60 * 1000))
# Retrieve API token
TOKEN=$(curl -s -X POST \
"https://accounts.spotify.com/api/token" \
-d "grant_type=client_credentials" \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json" \
| jq -r .access_token)
function get() {
curl -s -X GET \
"https://api.spotify.com/v1$1" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json"
}
# Retrieve list of Album IDs, excluding non-live albums
read -r -a ALBUMS <<< $( \
get "/artists/$ARTIST/albums?offset=0&limit=50&include_groups=album&market=GB" \
| jq -cr '.items[] | select(.name | contains("Live") | not) | .id')
TRACKS=""
# Loop over each Album to retrieve their tracks and append tracks to env file
for id in "${ALBUMS[@]}"; do
TRACK=$(get "/albums/$id/tracks" \
| jq -c '.items[] | { "duration": .duration_ms, "name": .name}')
TRACKS="${TRACKS}\n${TRACK}"
done;
# Convert json to CSV format
# Remove duplicates by name
# Numerically sort shortest to longest
# Exclude demo, remastered, live and Peel tracks
# Print out tracks until they surpass 15 minutes worth of milliseconds
echo "${TRACKS}" \
| jq -rs 'unique_by(.name)
| sort_by(.duration) | .[]
| select(.name | contains("Demo") or contains("Remastered") or contains("Live") or contains("Peel") | not)
| [.duration, .name] | @csv' \
| awk -F ',' -v totalDuration=$DURATION_MS '{
hours = ($1 / 1000) % 3600 / 60;
minutes = ($1 / 1000) % 60;
sum += $1;
printf "%-40s %dm:%ds\n", $2, hours, minutes;
if (sum > totalDuration) { exit }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment