Guest User

geirha - trqueue

a guest
Sep 10th, 2010
6,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # The default number of active torrents to keep running
  4. max_active=5
  5. # Host, port and authentication arguments for transmission-remote.
  6. #options=( localhost:9091 -n transmission:transmission )
  7.  
  8. do_start=1
  9. do_pause=1
  10.  
  11. usage() {
  12.   cat << EOF
  13. Usage: ${0##*/}Β [OPTIONS] [transmission-remote args]
  14.  
  15.   -h          Print this help message and exit
  16.   -m number   Set maximum number of active torrents (default: 5)
  17.   -s          Only start torrents
  18.   -p          Only pause torrents
  19.  
  20.   Remaining arguments will be passed on to transmission-remote.
  21.  
  22.   Example usage:
  23.  
  24.   Make sure 10 torrents are active, starting and stopping torrents as
  25.   necessary:
  26.     trqueue -m 10
  27.  
  28.   Make sure at most 10 torrents are active, but do not start any.
  29.     requeue -m 10 -p
  30.  
  31.   Provide username and password for transmission
  32.     trqueue -- -n transmission-user:secret
  33. EOF
  34. }
  35.  
  36. remote() { transmission-remote "${options[@]}" "$@"; }
  37.  
  38. # Parses out 4 values for each torrent from transmission-remote -l and sorts
  39. # numerically
  40. # <running (0/1)> <percentage done> <ratio> <torrent id>
  41. filter() {
  42.   remote -l | awk -F'^ +|  +' 'NF>9{print ($9!="Stopped"),$3+0,$8+0,$2}' |
  43.     sort -n -k1,1 -k2,2 -k3,3
  44. }
  45.  
  46.  
  47. while getopts "hm:ps" opt; do
  48.   case $opt in
  49.     h) usage; exit;;
  50.     m)
  51.       if [[ $OPTARG = *[![:digit:]]* ]]; then
  52.         usage
  53.         exit 1
  54.       fi
  55.       max_active=$OPTARG
  56.       ;;
  57.     p) do_pause=1 do_start=0;;
  58.     s) do_pause=0 do_start=1;;
  59.     \?) usage; exit 1;;
  60.   esac
  61. done
  62. shift "$((OPTIND-1))"
  63. [[ $1 = -- ]] && shift
  64. (( $# )) && options=( "$@" )
  65.  
  66. num_active=0 num_stopped=0 active=() stopped=()
  67. while read running percent ratio id; do
  68.   if (( running )); then
  69.     active[num_active++]=$id
  70.   else
  71.     stopped[num_stopped++]=$id
  72.   fi
  73. done < <(filter)
  74.  
  75. if (( do_pause && num_active > max_active )); then
  76.   (IFS=,; remote -t "${active[*]:max_active}" --stop)
  77. elif (( do_start && num_active < max_active )); then
  78.   (IFS=,; remote -t "${stopped[*]:0:max_active-num_active}" --start)
  79. fi
Advertisement
Add Comment
Please, Sign In to add comment