#!/bin/bash
# Desc: Download albums from bandcamp. Accepts either artist page, or album page as argv. then proceeds to a repl mode
# Usage: bandcampdl.sh ARTIST_URL
# Example: bandcampdl.sh https://paperceilings.bandcamp.com/
#set -x
# /* FUNC DEF */ ---------------------------------------------------------------
msg(){ printf -- "%s\n" "$@"; }
emsg(){ printf -- "%s\n" "$@" >&2; }
bail(){ emsg "$@"; usage; exit 1; }
usage(){ awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"; }
slide(){ eval "set -- \$$# $*"; printf -- "%s" "$@"; }
bandrip(){
album="$(curl -s "$1" | awk -F'(<|>)' '/
/{gsub(/[^[:alnum:] ]/, "", $3);print $3}')"
mkdir "$album"
(
cd "$album" || return
printf -- "downloading: %s\n" "$album"
awk '/^[download]/{print $0}' < <(youtube-dl -R infinite --sleep-interval 20 -i -x "$1")
)
}
bandwalk(){
local url="${1}"
if awk '/href=.\/album\//{exit}' <<<"$(curl -sL "$url")"; then
mapfile -t discog < <(curl -s "${url}/music" | awk -v artist="$url" -F'"' '/href=.\/album\//{printf("%s%s\n", artist,$2)}')
for link in "${discog[@]}"; do
# NOTE: Cant disown or `wait` wont work. derp.
(bandrip "$link") &
sleep 5s
done
else
(bandrip "$link")&
fi
}
# /* INIT */ ------------------------------------------------------------------
test "$1" == "--help" && { usage; exit; }
# /* MAIN */ ------------------------------------------------------------------
while (($#)); do
bandwalk "$1"
shift
sleep 0.2s
done
wait
sleep 2s
printf -- "starting read eval loop. press ^D to exit.\n"
while read -p 'enter url: ' entry; do
bandwalk "$entry"
done