#!/bin/bash HOST="localhost" PORT="8080" SABPATH="/opt/sabnzbd" SABCFG="/etc/sabnzbd.ini" usage() { echo >&2 "`basename $0` - SABnzbd+ Command Line Interface options: -h host Connect to this host/ip (currently: $HOST) -p port Connect on this port (currently: $PORT) valid commands: list Prints current SAB queue force id Force-starts the specified nzb next id Moves the specified nzb to the top of the queue last id Moves the specified nzb to the end of the queue up id Moves the specified nzb up in the queue down id Moves the specified nzb down in the queue dequeue id Removes the specified nzb from the queue cancel Cancels the currently running nzb and removes it from the queue enqueue url [cat] Download and add the specified url to the queue, with optional category. enqueuenewzbin nzbid [cat] Download and add the specified newzbin id to the queue, with optional category. maxrate rate Changes the speed limit for SABnzbd+ to rate, specified in KB/s pause Pauses downloading continue Resumes downloading shutdown Shutdown SABnzbd+. You must specify \"True\" as the second argument for this to take effect. start Start SABnzbd+. SABPATH must be properly set in the script for this to work." 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 wget -q -O /dev/null "http://${HOST}:${PORT}/sabnzbd/queue/switch?uid1=${SABID}&uid2=${NEW}" 2>/dev/null echo ok 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" == "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 wget -q -O /dev/null "http://${HOST}:${PORT}/sabnzbd/queue/delete?uid=${SABID}" 2>/dev/null echo ok exit } start () { CURDIR=$PWD cd "${SABPATH}" ./SABnzbd.py -d -f "${SABCFG}" cd "${CURDIR}" exit } enqueue () { MODE=$1 NAME=$2 if [ "$3" != "" ]; then CAT="&cat=$3" else CAT="" fi wget -q -O /dev/null "http://${HOST}:${PORT}/sabnzbd/api?mode=${MODE}&name=${NAME}${CAT}" 2>/dev/null echo ok 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;; enqueuenewzbin) enqueue addid $2 $3;; enqueue) enqueue addurl $2 $3;; down) requeue $2 $(($2+1));; move) requeue $2 $3;; shutdown) shutdown $2;; start) start;; up) requeue $2 $(($2-1));; *) usage;; esac done usage exit 0