Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | Example from Soundcloud music downloader by 360percents.com |
- # | Download all tracks from username |
- # | Aug 2011 flip hess [email protected] |
- # +-----------------------------------------------------------------------------------------+
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="$( basename ${0} )"
- DIR="${HOME}/Music/soundcloud/"
- # Functions:
- # exit function
- function die()
- {
- echo "${1}"
- exit 1
- }
- # The main function.
- function fMain()
- {
- # check for arguments:
- [ ${#} -gt 0 ] || fShowUsage
- [ ${#} -lt 2 ] || fShowUsage
- # create directory:
- if [ ! -d "${DIR}" ]
- then
- mkdir -p "${DIR}" || die "Failed to create ${DIR} Exiting ${SCRIPT_PATH}"
- else
- cd "${DIR}" || die "Failed to cd to {HOME}/Music/soundcloud/"
- fi
- # check for empty var:
- [ -n "${1}" ] || fShowUsage
- # check if username exist:
- { curl -s --user-agent 'Mozilla/5.0' http://soundcloud.com/${1} | grep -q 'Oops'; } && die "Username unknown on soundcloud! Exiting ${SCRIPT_PATH}"
- URL="http://soundcloud.com/${1}"
- # check if url is valid by regex:
- REGEX='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
- [[ ${URL} =~ ${REGEX} ]] || { echo "Provided url / username is not valid!" ; echo ; fShowUsage; }
- # select pages:
- PAGES="$( curl -s --user-agent 'Mozilla/5.0' ${URL} | tr '"' "\n" | grep "tracks?page=" | sort -u | tail -n 1 | cut -d "=" -f 2 )"
- # set to 1 if var is empty
- [ -n "${PAGES}" ] || PAGES=1
- # notify
- echo "Found ${PAGES} pages of songs!"
- # do the magic
- for (( PAGE=1; PAGE <= ${PAGES}; PAGE++ ))
- do
- if [ "${PAGES}" = 1 ]
- then
- THIS="$( curl -s --user-agent 'Mozilla/5.0' ${URL} )";
- else
- THIS="$( curl -s --user-agent 'Mozilla/5.0' ${URL}/tracks?page=${PAGE} )";
- fi
- SONGS="$( echo "${THIS}" | grep 'streamUrl' | tr '"' "\n" | grep 'http://media.soundcloud.com/stream/')" || die "No songs found for user ${1}! Exiting ${SCRIPT_PATH}"
- SONGCOUNT="$( echo "${SONGS}" | wc -l )" || die "Failed to determine songcount"
- TITLES="$( echo "${THIS}" | grep 'title":"' | tr ',' "\n" | grep 'title' | cut -d '"' -f 4)" || die "Failed to determine titles!"
- # check for empty variable:
- [ -n "${SONGS}" ] || die "No song found at ${URL}/tracks?page=${PAGE}"
- # notify:
- echo "Downloading ${SONGCOUNT} songs from page ${PAGE}..."
- # fill aray
- for (( SONGID=1; SONGID <= ${SONGCOUNT}; SONGID++ ))
- do
- # get title
- TITLE="$( echo "${TITLES}" | sed -n "${SONGID}"p )"
- # notify
- echo "Downloading ${TITLE}..."
- URL="$( echo "${SONGS}" | sed -n "${SONGID}"p)"
- # download track:
- { curl -# -L --user-agent 'Mozilla/5.0' -o "${DIR}/${TITLE}.mp3" ${URL} && echo "Done downloading ${TITLE}.mp3 from ${URL}"; } || \
- { echo "Failed to download ${TITLE}.mp3, Skipping to next track!"; continue; }
- echo "----------------------------------------------------------------------------"
- done
- done
- return 0
- }
- # Shows usage.
- function fShowUsage()
- {
- echo "Soundcloud.com music downloader"
- echo "Usage: ${SCRIPT_PATH} DJ-username"
- echo
- exit 1
- }
- # first things first: check for curl
- [ -x "$(which curl)" ] || die "this script depends on cURL"
- # Start the program:
- fMain "${@}"
- # Exit with previous return code:
- exit "${?}"
Add Comment
Please, Sign In to add comment