flipje

soundcloud-user-downloader

Aug 17th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.79 KB | None | 0 0
  1. #!/bin/bash
  2. # +-----------------------------------------------------------------------------------------+
  3. # | Example from Soundcloud music downloader by 360percents.com                             |
  4. # | Download all tracks from username                                                       |
  5. # | Aug 2011 flip hess [email protected]                                                  |
  6. # +-----------------------------------------------------------------------------------------+
  7.  
  8. # Global variables:
  9.  
  10. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  11. SCRIPT_PATH="$( basename ${0} )"
  12. DIR="${HOME}/Music/soundcloud/"
  13.  
  14.  
  15. # Functions:
  16.  
  17.   # exit function
  18.   function die()
  19.   {
  20.     echo "${1}"
  21.     exit 1
  22.   }
  23.  
  24.  
  25.   # The main function.
  26.   function fMain()
  27.   {
  28.     # check for arguments:
  29.     [ ${#} -gt 0 ] || fShowUsage
  30.     [ ${#} -lt 2 ] || fShowUsage
  31.  
  32.     # create directory:
  33.     if [ ! -d "${DIR}" ]
  34.     then
  35.       mkdir -p "${DIR}" || die "Failed to create ${DIR} Exiting ${SCRIPT_PATH}"
  36.     else
  37.       cd "${DIR}" || die "Failed to cd to {HOME}/Music/soundcloud/"
  38.     fi
  39.  
  40.     # check for empty var:
  41.     [ -n "${1}" ] || fShowUsage
  42.  
  43.     # check if username exist:
  44.     { curl -s --user-agent 'Mozilla/5.0' http://soundcloud.com/${1}  | grep -q 'Oops'; } && die "Username unknown on soundcloud! Exiting ${SCRIPT_PATH}"
  45.  
  46.     URL="http://soundcloud.com/${1}"
  47.  
  48.     # check if url is valid by regex:
  49.     REGEX='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
  50.     [[ ${URL} =~ ${REGEX} ]] || { echo "Provided url / username is not valid!" ; echo ; fShowUsage; }
  51.  
  52.     # select pages:
  53.     PAGES="$( curl -s --user-agent 'Mozilla/5.0' ${URL} | tr '"' "\n" | grep "tracks?page=" | sort -u | tail -n 1 | cut -d "=" -f 2 )"
  54.  
  55.     # set to 1 if var is empty
  56.     [ -n "${PAGES}" ] || PAGES=1
  57.  
  58.     # notify
  59.     echo "Found ${PAGES} pages of songs!"
  60.  
  61.     # do the magic
  62.     for (( PAGE=1; PAGE <= ${PAGES}; PAGE++ ))
  63.     do
  64.       if [ "${PAGES}" = 1 ]
  65.       then
  66.         THIS="$( curl -s --user-agent 'Mozilla/5.0' ${URL} )";
  67.       else
  68.         THIS="$( curl -s --user-agent 'Mozilla/5.0' ${URL}/tracks?page=${PAGE} )";
  69.       fi
  70.  
  71.       SONGS="$( echo "${THIS}" | grep 'streamUrl' | tr '"' "\n" | grep 'http://media.soundcloud.com/stream/')" || die "No songs found for user ${1}! Exiting ${SCRIPT_PATH}"
  72.       SONGCOUNT="$( echo "${SONGS}" | wc -l )" || die "Failed to determine songcount"
  73.       TITLES="$( echo "${THIS}" | grep 'title":"' | tr ',' "\n" | grep 'title' | cut -d '"' -f 4)" || die "Failed to determine titles!"
  74.  
  75.       # check for empty variable:
  76.       [ -n "${SONGS}" ] || die "No song found at ${URL}/tracks?page=${PAGE}"
  77.  
  78.       # notify:
  79.       echo "Downloading ${SONGCOUNT} songs from page ${PAGE}..."
  80.  
  81.       # fill aray      
  82.       for (( SONGID=1; SONGID <= ${SONGCOUNT}; SONGID++ ))
  83.       do
  84.         # get title
  85.         TITLE="$( echo "${TITLES}" | sed -n "${SONGID}"p )"
  86.         # notify
  87.         echo "Downloading ${TITLE}..."
  88.         URL="$( echo "${SONGS}" | sed -n "${SONGID}"p)"
  89.         # download track:
  90.     { curl -#  -L --user-agent 'Mozilla/5.0' -o "${DIR}/${TITLE}.mp3" ${URL} && echo "Done downloading ${TITLE}.mp3 from ${URL}"; } || \
  91.     { echo "Failed to download ${TITLE}.mp3, Skipping to next track!"; continue; }
  92.         echo "----------------------------------------------------------------------------"
  93.       done
  94.     done
  95.  
  96.     return 0
  97.  
  98.   }
  99.  
  100.   # Shows usage.
  101.   function fShowUsage()
  102.   {
  103.     echo "Soundcloud.com music downloader"
  104.     echo "Usage: ${SCRIPT_PATH} DJ-username"
  105.     echo
  106.  
  107.     exit 1
  108.   }
  109.  
  110. # first things first: check for curl
  111.   [ -x "$(which curl)" ] || die "this script depends on cURL"
  112.  
  113.  
  114. # Start the program:
  115.   fMain "${@}"
  116.  
  117.   # Exit with previous return code:
  118.   exit "${?}"
Add Comment
Please, Sign In to add comment