Share Pastebin
Guest
Public paste!

epixoip

By: a guest | Aug 27th, 2009 | Syntax: Bash | Size: 2.89 KB | Hits: 105 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #!/bin/bash
  2. # Wed Aug 26 19:25:48 PDT 2009 by epixoip (epixoip@hush.com) and xirgot (jburks@gmail.com)
  3.  
  4. function usage() {
  5. cat <<EOF
  6. powerboost - ensures comcast powerboost speeds for duration of download
  7. Syntax: $0 [options] URL
  8.  
  9. Options:
  10.         -t      Number of concurrent "threads"
  11.         -o      Output file name
  12.         -h      Display this message
  13.  
  14. EOF
  15. exit 1
  16. }
  17.  
  18. function error() {
  19.         echo "error: $1" >&2
  20.         exit 1
  21. }
  22.  
  23. function warn() {
  24.         echo "warning: $1" >&2
  25. }
  26.  
  27. function spawn_thread() {
  28.         if [[ $got -lt $length ]]; then
  29.                 for ((i=1;i<=$1;i++)) {
  30.                         sbyte=$((got + 1))
  31.                         remain=$(expr $length - $sbyte)
  32.                         if [[ $remain -lt 9999999 ]]; then
  33.                                 ebyte=$length
  34.                                 got=$length
  35.                         elif [[ $remain -ge 9999999 ]]; then
  36.                                 ebyte=$(expr $got + 9999999)
  37.                                 got=$(expr $got + 9999999)
  38.                         fi
  39.                         chunk=$((chunk + 1))
  40.                         curl -s -o ${outfile}.${chunk} -r ${sbyte}-${ebyte} $url &
  41.                         echo "Retrieving chunk ${sbyte}-${ebyte}..."
  42.                 }
  43.         fi
  44. }
  45.  
  46. function thread_exit() {
  47.         spawn_thread 1
  48. }
  49.  
  50. while getopts "ht:o:" opts; do
  51.         case $opts in
  52.                 t) threads="$OPTARG";;
  53.                 o) outfile="$OPTARG";;
  54.                 h) usage;;
  55.                 v) verbose=1;;
  56.                 ?) usage;;
  57.         esac
  58.         x="$OPTIND"
  59. done
  60.  
  61. if [[ "$x" > 0 ]]; then shift $(($x-1)); fi
  62. if [[ -z "$1" ]]; then usage; fi
  63.  
  64. url="$1"
  65. threads="${threads:-4}"
  66. outfile="${outfile:-$(basename $url)}"
  67. length="$(curl -I $url 2>&1 | grep -i 'content-length:' | awk '{print $2}' | sed -e 's/\n//g' -e 's/\r//g')"
  68.  
  69. if [[ -z "$length" ]]; then error "$(basename $url) does not exist on server $(dirname $url)"; fi
  70.  
  71. maxthreads=$((length / 10000000))
  72. if [[ $threads > $maxthreads ]]; then
  73.         warn "more threads were called than needed; reducing threads to $maxthreads"
  74.         threads=$maxthreads
  75. fi
  76.  
  77. if [[ $length -le 10000000 ]]; then
  78.         warn "file is less than 10MB; no benefit from using this script. downloading anyway."
  79.         curl -o ${outfile} ${url} 2>&1
  80. else
  81.         got=-1
  82.         chunk=0
  83.         spanwed=0
  84.         stime=$(date +%s)
  85.  
  86.         set -o monitor
  87.         spawn_thread $threads
  88.         trap thread_exit CHLD
  89.         wait
  90.  
  91.         etime=$(date +%s)
  92.         elapsed=$((etime - stime))
  93.         arate=$(echo "scale=2; $length / $elapsed" | bc -l)
  94.         echo "Retrieved $length bytes in $elapsed seconds [avg $arate bytes/sec]"
  95.  
  96.         echo "Concatenating chunks and cleaning up..."
  97.         for ((i=1;i<=$chunk;i++)) { cat ${outfile}.${i} >>${outfile}; rm -f ${outfile}.${i}; }
  98.  
  99.         echo "Done!"
  100. fi