Instantly share code, notes, and snippets.
Created
February 3, 2010 12:27
-
Save gandro/293568 to your computer and use it in GitHub Desktop.
Rapidshare Free-User Download Shellscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Version 3.0 | |
# | |
# "THE COFFEE-WARE LICENSE" (Revision 12/2007): | |
# | |
# Sebastian Wicki <gandro@gmx.net> wrote this file. As long as you retain | |
# this notice you can do whatever you want with this stuff. If we meet some | |
# day, and you think this stuff is worth it, you can buy me a cup of coffee | |
# in return. | |
# | |
################################################################################ | |
# DEFAULT SETTINGS ############################################################# | |
################################################################################ | |
# Premium Account User (optional) | |
PREMIUM_USER= | |
# Premium Account Password (optional, will be prompted if not given) | |
PREMIUM_PASS= | |
# Where to log any messages | |
LOGFILE="/dev/stderr" | |
# Fetch and save the MD5 hash of the downloads (1=yes or 0=no) | |
MD5SUM=1 | |
# Where to save temporary data | |
COOKIE="${TMP:-/tmp}/.rscookie_$$" | |
TMPFILE="${TMP:-/tmp}/rstmp_$$" | |
# Command which will be executed instead of waiting 15min as free-user (optional) | |
IP_RENEWAL_CMD= | |
# Arguments for wget for the file download (optional) | |
WGET_ARGS= | |
################################################################################ | |
################################################################################ | |
################################################################################ | |
# | |
# RSFileInfo $URL | |
# | |
# Get file information from the Rapidshare API | |
# | |
# Arguments: | |
# $URL - Rapidshare URL of the file to check | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure. | |
# - an array containing the following elements on stdout: | |
# | |
# $download_url, $filename, $filesize, $md5sum | |
# | |
function RSFileInfo() { | |
local fileid=$(basename $(dirname "${1}")) | |
local filename=$(basename "${1}") | |
local apiurl="http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1" | |
apiurl="${apiurl}&files=${fileid}&filenames=${filename}&incmd5=${MD5SUM}" | |
local return=$(wget -q -O - "${apiurl}") | |
local -a return_r=( `echo $return | tr ',' ' '` ) | |
if [ "${return:0:5}" = "ERROR" ] ; then | |
echo "Rapidshare API:" ${return:7} | |
return 1 | |
elif [ "${return_r[4]}" -ne 1 ] ; then | |
case "${return_r[4]}" in | |
0) | |
echo "Error: File not found!" | |
return 1 | |
;; | |
3) | |
echo "Error: Server down!" | |
return 1 | |
;; | |
4) | |
echo "Error: File marked as illegal!" | |
return 1 | |
;; | |
5) | |
echo "Error: Anonymous file locked, because it has more than 10 downloads already" | |
return 1 | |
;; | |
2|6) | |
echo "Notice: TrafficShare Download" | |
;; | |
esac | |
fi | |
local filename=${return_r[1]} | |
local fileid=${return_r[0]} | |
local filesize=${return_r[2]} | |
local md5sum=$(echo ${return_r[6]} | tr '[:upper:]' '[:lower:]') | |
local download_url="http://rs${return_r[3]}${return_r[5]}.rapidshare.com/files/" | |
download_url="${download_url}${fileid}/${filename}" | |
echo "${download_url} ${filename} ${filesize} ${md5sum}" | |
return 0 | |
} | |
# | |
# RSDownloadPA $URL $File | |
# | |
# Rapidshare Download as Premium User | |
# | |
# Arguments: | |
# $URL - Rapidshare-Mirror URL of the file to check | |
# $File - Download file destination | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# | |
function RSDownloadPA() { | |
local url="${1}?directstart=1" | |
local file=${2} | |
local wgetarg=$WGET_ARGS | |
if [ -e "${file}" ] ; then | |
wgetarg="${wgetarg} --continue" | |
fi | |
wget --load-cookies=${COOKIE} ${wgetarg} -O "${file}" "${url}" | |
return $? | |
} | |
# | |
# RSParseHTMLFree $URL | |
# | |
# Parse HTML output for free users | |
# | |
# Arguments: | |
# $URL - Rapidshare-Mirror URL of the file | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# - Free user download URL on stdout | |
# | |
function RSParseHTMLFree() { | |
local download_url | |
local wait | |
while true ; do | |
wget -q --post-data="dl.start=Free" -O "${TMPFILE}" "${1}" | |
wait=$(sed -n -e 's/^var c=\([0-9]*\);.*/\1/p' "${TMPFILE}") | |
download_url=$(grep 'name="dlf"' "${TMPFILE}" | \ | |
sed -n -e 's/.*action="\(.*\)" method.*/\1/p') | |
if [ -n "${wait}" -a -n "${download_url}" ] ; then | |
echo "Ready. Waiting ${wait} seconds..." >&2 | |
sleep "${wait}" | |
break | |
elif grep -q 'download limit' "${TMPFILE}" ; then | |
if [ -z "${IP_RENEWAL_CMD}" ] ; then | |
wait=$(sed -n -e 's/.*in about \([0-9]*\) minutes.*/\1/p' "${TMPFILE}") | |
echo "Download limit reached. Waiting ${wait} minutes..." >&2 | |
sleep "${wait}m" | |
else | |
${IP_RENEWAL_CMD} | |
fi | |
elif grep -q -e 'a lot of users' -e 'no more download slots' \ | |
-e 'servers are overloaded' "${TMPFILE}" | |
then | |
echo "No more download slots. Waiting 2 minutes..." >&2 | |
sleep 2m | |
continue | |
elif grep -q 'is already downloading' "${TMPFILE}" ; then | |
echo "IP Address is already downloading. Waiting 30 minutes..." >&2 | |
sleep 30m | |
continue | |
else | |
local dumpfile="rserror_${filename}.html" | |
cp "${TMPFILE}" "${dumpfile}" | |
echo "Error: Unknown Rapidshare error. \""${dumpfile}"\" dumped.">&2 | |
return 1 | |
fi | |
done | |
echo "${download_url}" | |
return 0 | |
} | |
# | |
# RSParseHTMLFree $URL $File | |
# | |
# Rapidshare Download as Free User | |
# | |
# Arguments: | |
# $URL - Rapidshare-Mirror URL of the file | |
# $File - Download file destination | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# | |
function RSDownloadFree() { | |
local url=$1 | |
local filename=$2 | |
local download_url | |
if ! download_url=$(RSParseHTMLFree ${url}) ; then | |
return 1 | |
elif [ -e "${filename}" ] ; then | |
echo -n "Warning: ${filename} already exists - " >&2 | |
filename=$(mktemp -u "${filename}.XXXX") | |
echo "saving as ${filename}" >&2 | |
fi | |
wget ${WGET_ARGS} -a "${LOGFILE}" -O "${filename}" "${download_url}" | |
return $? | |
} | |
# | |
# RSLoginPA $User $Password | |
# | |
# Rapidshare Premium User Login | |
# | |
# Arguments: | |
# $User - Rapidshare-Mirror URL of the file | |
# $Password - Download file destination | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# | |
function RSLoginPA() { | |
wget -q --save-cookies="${COOKIE}" --post-data="login=${1}&password=${2}" -O /dev/null \ | |
"https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi" | |
grep -q ".rapidshare.com" "${COOKIE}" | |
return $? | |
} | |
# | |
# RSLogoutPA | |
# | |
# Rapidshare Premium User Logout | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# | |
function RSLogoutPA() { | |
wget -q --load-cookies="${COOKIE}" --save-cookies="${COOKIE}" \ | |
"https://ssl.rapidshare.com/cgi-bin/premium.cgi?logout=1" | |
return $? | |
} | |
# | |
# RSDownloadFile $URL $File | |
# | |
# Start Rapidshare Download (as free- or premium-user) | |
# | |
# Arguments: | |
# $URL - Rapidshare URL of the file | |
# | |
# Returns: | |
# - return value 0 on success, non-zero on failure | |
# | |
function RSDownloadFile() { | |
local -a fileinfo | |
fileinfo=( `RSFileInfo $1` ) | |
if [ $? -gt 0 ] ; then | |
# print error msg | |
echo ${fileinfo[@]} | |
return 1 | |
fi | |
local filesize=$(expr ${fileinfo[2]} / 1024 / 1024) | |
echo "Downloading \"${fileinfo[1]}\" (${filesize} MiB)" >&2 | |
if [ "${MD5SUM}" = 1 ] ; then | |
echo "${fileinfo[3]} ${fileinfo[1]}" >>MD5SUMS | |
fi | |
if [ -n "${PREMIUM_USER}" ] && [ -n "${PREMIUM_PASS}" ] ; then | |
RSDownloadPA ${fileinfo[0]} ${fileinfo[1]} | |
else | |
RSDownloadFree ${fileinfo[0]} ${fileinfo[1]} | |
fi | |
return $? | |
} | |
# | |
# Main programm | |
# | |
while [ -n "${1}" ] ; do | |
case ${1} in | |
-l|--log) | |
LOGFILE=${2?"Missing logfile"} | |
shift | |
;; | |
-n|--no-md5) | |
MD5SUM=0 | |
;; | |
-i|--input) | |
INPUT=${2?"Missing inputfile"} | |
shift | |
;; | |
-u|--user) | |
PREMIUM_USER=${2?"Missing username"} | |
shift | |
;; | |
-p|--pass) | |
PREMIUM_PASS=${2?"Missing password"} | |
shift | |
;; | |
http://*) | |
URL=${1} | |
;; | |
-h|--help) | |
echo "Usage:" | |
echo | |
echo " ${0} [OPTIONS] [URL]" >&2 | |
echo | |
echo "Available Options:" | |
echo " -i --input INPUTFILE Read urls from input file" | |
echo " -l --log LOGFILE Write output to log file" | |
echo " -n --no-md5 Don't create MD5SUMS file" | |
echo | |
echo " -u --user USERNAME Premium account username" | |
echo " -p --pass PASSWORD Premium account password" | |
echo | |
echo " -h --help Show this help page" | |
echo | |
exit | |
;; | |
*) | |
echo "${0}: unknown option: ${1}" >&2 | |
;; | |
esac | |
shift | |
done | |
# Initialize logfile | |
exec 2>>${LOGFILE} | |
if [ -f "${LOGFILE}" ] ; then | |
WGET_ARGS="-q ${WGET_ARGS}" | |
else | |
WGET_ARGS="--progress=bar:force ${WGET_ARGS}" | |
fi | |
# Delete cookie afterwards | |
trap "rm ${COOKIE} ${TMPFILE} 2>/dev/null; exit" EXIT SIGINT SIGTERM SIGKILL | |
# Premium Account login, if necessary | |
if [ -n "${PREMIUM_USER}" ] ; then | |
echo "Notice: Logging in as premium user \"${PREMIUM_USER}\"... " | |
if [ -z "${PREMIUM_PASS}" ] ; then | |
read -s -p "RSPA Password: " PREMIUM_PASS | |
fi | |
if ! RSLoginPA ${PREMIUM_USER} ${PREMIUM_PASS} ; then | |
echo "Error: Premium Account login failed!" | |
exit 1 | |
fi | |
fi | |
if [ -z "${URL}" ] && [ -z "${INPUT}" ] ; then | |
echo "Error: No url or inputfile specified." >&2 | |
exit 1 | |
fi | |
# Start downloading one URL | |
if [ -n "${URL}" ] ; then | |
RSDownloadFile "${URL}" | |
fi | |
# Or start reading from the input list | |
if [ -n "${INPUT}" ] ; then | |
while read url ; do | |
if [ "${url:0:4}" = 'http' ] ; then | |
RSDownloadFile "${url}" | |
fi | |
done < "${INPUT}" | |
fi | |
# Premium Account logout | |
if [ -n "${PREMIUM_USER}" ] && [ -n "${PREMIUM_PASS}" ] ; then | |
RSLogoutPA | |
fi | |
# Close logfile | |
exec 2>&- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment