#!/bin/bashHOST="localhost"PORT="8080"usage() { echo >&2 "`basename $0` - SABnzbd+ Command Line Interfaceoptions:-h host Connect to this host/ip (currently: $HOST)-p port Connect on this port (currently: $PORT)valid commands:list Prints current SAB queueforce id Force-starts the specified nzbnext id Moves the specified nzb to the top of the queuelast id Moves the specified nzb to the end of the queueup id Moves the specified nzb up in the queuedown id Moves the specified nzb down in the queuedequeue id Removes the specified nzb from the queuecancel Cancels the currently running nzb and removes it from the queuemaxrate rate Changes the speed limit for SABnzbd+ to rate, specified in KB/spause Pauses downloadingcontinue Resumes downloadingshutdown Shutdown SABnzbd+. You must specify "True" as the second argument for this to take effect." exit 2}getSabID () { wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=qstatus&output=xml" 2>/dev/null | xmlstarlet sel -T -t -n -m queue/jobs/job -v id -n - | awk "NR==${1}+2"}printQueue () { wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=qstatus&output=xml" 2>/dev/null | xmlstarlet sel -T -t -n -m queue/jobs/job -v "concat(filename,'|',mbleft,'|',mb)" -n - 2>/dev/null | grep -v ^$ | awk 'BEGIN { FS = "|" OFMT = "%0.0f" OFS = "\t" {printf("%-5s%-58s\t%-15s\n", "ID", "Name", "Remain/Total")} {print("----------------------------------------------------------------------------")} } ; {printf("%-5s%-58s\t%0d/%0d MB\n", NR-1, substr($1,1,55), $2, $3)}' exit}requeue () { ORIG=$1 NEW=$2 if [ $NEW -eq -1 ]; then NEW=`wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=qstatus&output=xml" 2>/dev/null | xmlstarlet sel -T -t -n -m queue/jobs/job -n - | awk 'END{print NR-1}'` fi SABID=`getSabID $1` wget -q -O /dev/null "http://${HOST}:${PORT}/sabnzbd/api?mode=switch&value=${SABID}&value2=${NEW}" 2>/dev/null exit}pause () { if [ $1 -eq 0 ]; then wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=resume" 2>/dev/null else wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=pause" 2>/dev/null fi exit}shutdown () { if [ "$1" -eq "True" ]; then wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=shutdown" 2>/dev/null fi exit}maxrate () { wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=config&name=speedlimit&value=$1" 2>/dev/null exit}delete () { SABID=`getSabID $1` wget -q -O - "http://${HOST}:${PORT}/sabnzbd/api?mode=queue&name=delete&value=${SABID}" 2>/dev/null exit}while [ $# -gt 0 ]; do case "$1" in -h) HOST=$2; shift; shift;; -p) PORT=$2; shift; shift;; list) printQueue;; force) requeue $2 0;; next) requeue $2 1;; last) requeue $2 -1;; maxrate) maxrate $2;; cancel) delete 0;; pause) pause 1;; continue) pause 0;; resume) pause 0;; dequeue) delete $2;; delete) delete $2;; down) requeue $2 $(($2+1));; move) requeue $2 $3;; shutdown) shutdown $2;; up) requeue $2 $(($2-1));; *) usage;; esacdoneusageexit 0