$ cat iampd #!/bin/bash cliext=ogg rawurl="${1:-https://archive.org/metadata/gd90-03-29.sbd.nawrocki.3389.sbeok.shnf}" id="${rawurl%/}" id="${id##*/}" url="https://archive.org/metadata/$id" curldata="$(curl -s "$url")" mapfile -t files < <( echo "$curldata" | jq -r '.files[] | select(.name != null) | select(.name|test("\\.(mp3|ogg|flac)$")) | .name' ) for f in "${files[@]}"; do case "$f" in *."$cliext") hascliext=1; extpref="$cliext"; break;; *.flac) hasflac=1 ;; *.ogg) hasogg=1 ;; *.mp3) hasmp3=1 ;; esac done if [[ ! "$extpref" ]]; then (( hasmp3 )) && extpref='mp3' (( hasogg )) && extpref='ogg' (( hasflac )) && extpref='flac' fi for i in "${files[@]}"; do [[ "$i" = *"$extpref" ]] && playlist+=( "$i" ) done #printf %s\\n "${playlist[@]}" #echo "$curldata"| jq -r '.files[]|select(.name|test("\\.(ogg)$"))' dlurl="https://archive.org/download/$id" #for i in "${playlist[@]}"; do # printf '%s/%s\n' "$dlurl" "$i" #done #|mpc add mpdxspf="$(mktemp --suffix='.xspf' --tmpdir='/library/music/mpdplaylists')" echo "$curldata" | jq -r --arg base "$dlurl" --arg ext "$extpref" ' # First, filter the input down to just the MP3 file objects [.files[] | select(.name | test("\\.(mp3)$"))] | " " + # Now map over the filtered array of objects safely (map( # (.length | split(":") | (.[0]|tonumber)*60000 + (.[1]|tonumber)*1000) as $ms | (.length | split(":") | map(tonumber)) as $time_parts | (if ($time_parts | length) == 2 then ($time_parts[0] * 60000) + ($time_parts[1] * 1000) elif ($time_parts | length) == 3 then ($time_parts[0] * 3600000) + ($time_parts[1] * 60000) + ($time_parts[2] * 1000) else (.length | tonumber * 1000) end) as $ms | (.name | gsub("\\.mp3$"; "")) as $basename | " \($base)/\($basename).\($ext) \(.title) \(.creator) \(.album) \(.track) \($ms) " ) | join("\n")) + " " ' > "$mpdxspf" chmod ugo+r "$mpdxspf" (( verbose )) && ls -la "$mpdxspf" if mpc -h "$MPDSOCK" load "$mpdxspf"; then # rm "$mpdxspf" : else printf '/usr/bin/mpc failed to load %s\n' "$mpdxspf" confirm -Y "Do you want to remove the temp .xspf file?" && rm "$mpdxspf" || true fi