This commit is contained in:
Jaidyn Lev 2019-01-20 15:32:54 -06:00
commit 6dd14c50c4
3 changed files with 319 additions and 0 deletions

40
README.txt Normal file
View File

@ -0,0 +1,40 @@
===============================================================================
BANDCAMP-DL Jam, not jelly
===============================================================================
A simple script that can download a Bandcamp album or track.
(Streaming is for nerds anyway >o>)
----------------------------------------
PRE-REQUISITES
----------------------------------------
You'll need:
* "gendl" in your $PATH
* a POSIX-compatible shell (tested with `pdksh` and `bash`)
* "curl", "wget", or "ftp" installed
----------------------------------------
USAGE
----------------------------------------
Just run "bandcamp-dl" like so:
bandcamp-dl -a album_url
bandcamp-dl -t track_url
If you're downloading an album, use "-a".
If it's a track, use "-t".
Pretty self-explanatory =w=
----------------------------------------
BORING STUFF
----------------------------------------
License is CC-0 (though, "gendl" is GPLv3)
Author is Jenga Phoenix <jadedctrl@teknik.io>
Sauce is at https://git.eunichx.us/bandcamp-dl

124
bandcamp-dl Executable file
View File

@ -0,0 +1,124 @@
#!/bin/sh
########################################
# name: bandcamp-dl
# lisc: cc0
# desc: download the mp3(s) of a
# bandcamp track or album
# main: Jenga Phoenix
# <jadedctrl@teknik.io>
####################
TEMP="$(mktemp)"
DOWNLOADER="curl"
# supported programmes: `curl`, `ftp`, or `wget`
# STRING --> STRING
# Return the URL to the mp3 of a given track (from its URL)
function track_mp3
{
track_url="$1"
gendl -o "$TEMP" "$track_url"
grep "trackinfo" "$TEMP" \
| sed 's%.*"mp3-128":"%%' \
| sed 's%".*%%'
}
# STRING --> STRING
# Get the root of a URL.
function root_url
{
url="$1"
protocol="$(echo "$url" | sed 's%://.*%%')"
url="$(echo "$url" | sed 's%.*://%%')"
root_url="$(echo "$url" | sed 's%/.*%%')"
echo "${protocol}://${root_url}"
}
# STRING --> STRING
# Get the filename of a URL.
function local_url
{
url="$1"
local_url="$(echo "$url" | sed 's%.*/%%')"
echo "$local_url"
}
# STRING --> STRING
# Return the URLs of l
function track_urls
{
album_url="$1"
root_url="$(root_url "$album_url")"
gendl -o "$TEMP" "$album_url" > /dev/null
track_stack="$(grep "\"/track" "$TEMP" | grep "<a href" | grep "</span>")"
track_stack="$(echo "$track_stack" | sed 's%.*<a href=\"%%g' | sed 's%".*%%g')"
track_stack="$(echo "$track_stack" | sed 's%^%'"$root_url"'%g')"
echo "$track_stack"
}
# STRING --> NIL
# Download the MP3s of every track of a given album.
function download_album_mp3
{
album_url="$1"
track_stack="$(track_urls "$album_url")"
track_lines="$(echo "$track_stack" | wc -l)"
while test "$(echo "$track_lines")" -gt 1
do
download_track_mp3 "$(echo "$track_stack" | head -1)"
track_stack="$(echo "$track_stack" | tail -$((track_lines - 1)))"
track_lines="$(echo "$track_stack" | wc -l)"
done
}
# STRING --> NIL
# Download a track's MP3 file.
function download_track_mp3
{
track_url="$1"
gendl -o "$(local_url "$track_url").mp3" \
"$(track_mp3 "$track_url")"
}
# --------------------------------------
# invocation
# NIL --> NIL
# Print the usage message and abort.
function usage
{
echo "Usage: bandcamp-dl -a album_url"
echo " bandcamp-dl -t track_url"
exit 2
}
# --------------------------------------
ARG="$1"
URL="$2"
if test "$ARG" = "-a"; then
download_album_mp3 "$URL"
elif test "$ARG" = "-t"; then
download_track_mp3 "$URL"
else
usage
fi

155
gendl Executable file
View File

@ -0,0 +1,155 @@
#!/bin/sh
##############################
# name: gendl
# lisc: gnu gplv3
# desc: download to stdout or
# file, independent of
# a specific downloader.
# ftp/wget/curl support.
# main: jadedctrl
##############################
# usage: gendl [-o output] url
#---------------------------------------
# backend bits
# NIL --> STRING
# return the download program you're using
function download_program
{
programs="ftp curl wget"
current=""
for program in $programs
do
if whereis $program > /dev/null
then
current=$program
fi
done
if uname -s | grep -e "LibertyBSD" -e "OpenBSD" > /dev/null
then
current="ftp"
fi
echo "$current"
}
# STRING PATH --> NIL
# download URL $1 to stdout
function download_stdout
{
program=$(download_program)
url=$1
case "$program" in
"ftp")
output="$(ftp -VMo- $url)"
;;
"curl")
output="$(curl $url)"
;;
"wget")
output="$(wget --quiet -O $url)"
;;
esac
return_code=$?
echo "$output"
return $return_code
}
# STRING PATH --> NIL
# download URL $1 to path $2
function download_file
{
program=$(download_program)
url=$1
path=$2
case "$program" in
"ftp")
ftp -VMU "." -o $path $url
;;
"curl")
curl -o $path $url
;;
"wget")
wget --quiet -O $path $url
;;
esac
return_code=$?
if test $return_code -ne 0 2>/dev/null
then
rm $path 2> /dev/null
# for consistency in behavior; wget saves 404s anyway, whereas
# ftp doesn't save anything from 404s, etc.
fi
return $return_code
}
# --------------------------------------
# front-end string-manip
# STRING --> STRING
# return the last word in a string
function last_word
{
string="$1"
echo "$string" \
| rev \
| sed 's% .*%%' \
| rev
}
function usage
{
echo "usage: gendl [-o output] URL"
}
# --------------------------------------
# invocation
args="$(getopt o: $*)"
if test -z "$@" 2>/dev/null
then
usage
exit 2
fi
set -- $args
while test $# -ne 0
do
case "$1"
in
-o)
download_path="$2"; shift; shift;;
--)
shift; break;;
esac
done
url="$(last_word "$@")"
if test -n "$download_path" 2> /dev/null
then
download_file $url $download_path
else
download_stdout "$url"
fi