#!/bin/bash
# Wed Aug 26 19:25:48 PDT 2009 by epixoip (epixoip@hush.com) and xirgot (jburks@gmail.com)
function usage() {
cat <<EOF
powerboost - ensures comcast powerboost speeds for duration of download
Syntax: $0 [options] URL
Options:
-t Number of concurrent "threads"
-o Output file name
-h Display this message
EOF
exit 1
}
function error() {
echo "error: $1" >&2
exit 1
}
function warn() {
echo "warning: $1" >&2
}
function spawn_thread() {
if [[ $got -lt $length ]]; then
for ((i=1;i<=$1;i++)) {
sbyte=$((got + 1))
remain=$(expr $length - $sbyte)
if [[ $remain -lt 9999999 ]]; then
ebyte=$length
got=$length
elif [[ $remain -ge 9999999 ]]; then
ebyte=$(expr $got + 9999999)
got=$(expr $got + 9999999)
fi
chunk=$((chunk + 1))
curl -s -o ${outfile}.${chunk} -r ${sbyte}-${ebyte} $url &
echo "Retrieving chunk ${sbyte}-${ebyte}..."
}
fi
}
function thread_exit() {
spawn_thread 1
}
while getopts "ht:o:" opts; do
case $opts in
t) threads="$OPTARG";;
o) outfile="$OPTARG";;
h) usage;;
v) verbose=1;;
?) usage;;
esac
x="$OPTIND"
done
if [[ "$x" > 0 ]]; then shift $(($x-1)); fi
if [[ -z "$1" ]]; then usage; fi
url="$1"
threads="${threads:-4}"
outfile="${outfile:-$(basename $url)}"
length="$(curl -I $url 2>&1 | grep -i 'content-length:' | awk '{print $2}' | sed -e 's/\n//g' -e 's/\r//g')"
if [[ -z "$length" ]]; then error "$(basename $url) does not exist on server $(dirname $url)"; fi
maxthreads=$((length / 10000000))
if [[ $threads > $maxthreads ]]; then
warn "more threads were called than needed; reducing threads to $maxthreads"
threads=$maxthreads
fi
if [[ $length -le 10000000 ]]; then
warn "file is less than 10MB; no benefit from using this script. downloading anyway."
curl -o ${outfile} ${url} 2>&1
else
got=-1
chunk=0
spanwed=0
stime=$(date +%s)
set -o monitor
spawn_thread $threads
trap thread_exit CHLD
wait
etime=$(date +%s)
elapsed=$((etime - stime))
arate=$(echo "scale=2; $length / $elapsed" | bc -l)
echo "Retrieved $length bytes in $elapsed seconds [avg $arate bytes/sec]"
echo "Concatenating chunks and cleaning up..."
for ((i=1;i<=$chunk;i++)) { cat ${outfile}.${i} >>${outfile}; rm -f ${outfile}.${i}; }
echo "Done!"
fi