#!/bin/bash exec 2>/dev/null if [ "$TERM" != "xterm-256color" ]; then export TERM="xterm-256color" fi DEL_CONSOLE_CMD="deluge-console connect 127.0.0.1:53032;" cond="" norm="$'\e[0m'" red="$'\e[1;31m'" if [ "$(hostname)" == "serverhost" ]; then duser="sudo /home/lucas/scripts/deluge_user_cmd.sh" elif [ "$(hostname)" == "quiethost" ]; then duser="" fi STARS_HANDY="************************" stars() { width=$(tput cols) counter=1 while [ $counter -le $width ]; do printf "*" ((counter++)) done } out="" SEARCH_MODE=false SLIM_MODE=false STATIC_MODE=false DOWN="" HANDY_MODE=false NAMES_MODE=false RM_MODE=false SLEEP_TIME=1 DOWNLOADING="ADCFEGCLG" RMS_MODE=false NORMAL_MODE=true GREP_ARR=() GREPV_ARR=() get_config() { while [[ ${1:0:1} == - ]]; do echo "Param: $1" [[ $1 == -t ]] && { if [ "$2" == "" ] ; then echo "Too few arguments"; usage; exit; fi echo "Search Torrent" SLEEP_TIME="$2" shift 2; continue; }; [[ $1 == -s ]] && { if [ "$2" == "" ] ; then echo "Too few arguments"; usage; exit; fi echo "Search Torrent" SEARCH_MODE=true NORMAL_MODE=false SEARCH="$2" shift 2; continue; }; [[ $1 == -detail ]] && { if [ "$2" == "" ] ; then echo "Too few arguments"; usage; exit; fi echo "Detail Torrent" DETAIL_MODE=true NORMAL_MODE=false SEARCH="$2" shift 2; while [[ ${1:0:1} == - ]]; do [[ $1 == -g ]] && { shift 1 while [[ ${1:0:1} != - ]]; do if [ "$1" == "" ]; then break; fi GREP_ARR+=("$1") shift 1 continue done } [[ $1 == -v ]] && { shift 1 while [[ ${1:0:1} != - ]]; do if [ "$1" == "" ]; then break; fi GREPV_ARR+=("$1") shift 1 continue done } done break }; [[ $1 == -slim ]] && { echo "Slim Mode" SLIM_MODE=true NORMAL_MODE=false shift 1; continue; }; [[ $1 == -ON ]] && { echo "Show only actual Downloading" DOWNLOADING="Down Speed: 0.0 KiB/s" shift 1; continue; }; [[ $1 == -on ]] && { echo "Show only State Downloading" DOWN="Speed" shift 1; continue; }; [[ $1 == -rm ]] && { echo "RM Mode" RM_MODE=true NORMAL_MODE=false shift 1; continue; }; [[ $1 == -rms ]] && { if [ "$2" == "" ] ; then echo "Too few arguments"; usage; exit; fi RMS_SEARCH="$2" echo "RMS Mode" RMS_MODE=true NORMAL_MODE=false shift 2; continue; }; [[ $1 == -static ]] && { echo "Static Mode" STATIC_MODE=true shift 1; continue; }; [[ $1 == -names ]] && { echo "Names Mode" NAMES_MODE=true NORMAL_MODE=false shift 1; continue; }; [[ $1 == -handy ]] && { echo "Handy Mode" HANDY_MODE=true NORMAL_MODE=false shift 1; continue; }; [[ $1 == -help ]] && { usage; exit; }; echo "Parameter $1 discarded" exit done } usage() { printf "Usage: -names\t\t\tfor display of all names and IDs -slim\t\t\tminimal display of torrents -on\t\t\tShow only State Downloading Torrents -ON\t\t\tShow only actual Downloading Torrents -static\t\t\tfor one time display of all deluge modes -handy\t\t\tfor custom smartphone display -rm\t\t\tremove Torrents -s \t\tSearch for one Torrent (Name or ID) -detail \tDeatiled Depiction of one Torrent -g \tParameters to grep -v \tParameters to grep -v -help\t\t\tdisplay this help\n" } print_string() { width=$(($(tput cols)/2-${#1}/2)) counter=1 while [ $counter -le $width ]; do printf " " ((counter++)) done printf "%s\n" "$1" } normal_deluge() { ${duser} ${DEL_CONSOLE_CMD} info | tr '\n' ';' | sed 's/\[\([~,#]*\)\];/\[\1\]\n/g' | grep -v "${DOWNLOADING}" | grep "${DOWN}" | tr ';' '\n' | sed 's/Size: /\nSize: /g' | sed 's/ Ratio/\nRatio/g' | sed 's/Up Speed/\nUp Speed/g'| sed 's/ETA:/\nETA:/g'| grep 'Name:\|Progress:\|State:\|Size:\|ETA:' | tr '\n' ' ' | sed 's/State/\nState/g' | sed 's/Progress/\nProgress/g' | sed 's/Name/\nName/g' | sed 's/Size/\nSize/g' | sed 's/Progress: \(.*%\)/\\\e[33mProgress: \1\\\e[0m/g' | sed -e "s/Down Speed:\(.*\/s\)/\\\e[31m Speed: \1\\\e[0m/g" } slim_deluge() { ${duser} ${DEL_CONSOLE_CMD} info | tr '\n' ';' | sed 's/\[\([~,#]*\)\];/\[\1\]\n/g' | grep -v "${DOWNLOADING}" | grep "${DOWN}" | tr ';' '\n' | sed 's/Down Speed:/\nDown Speed:/g' | sed 's/Up Speed/\nUp Speed/g' | sed 's/ETA/\nETA/g'| sed 's/\[.*\]//g' | grep 'Progress\|Name\|Down Speed\|ETA'| tr --delete '\n' | sed 's/Name:/\nName:/g' | sed 's/Progress: \(.*%\)/ \\\e[33m\1\\\e[0m/g' | sed -e "s/Down Speed:\(.*\/s\)/\tSpeed: \\\e[31m\1\\\e[0m/g" | sort } search_deluge() { ${duser} ${DEL_CONSOLE_CMD} info ${SEARCH} } detail_deluge() { STRING=$(${duser} ${DEL_CONSOLE_CMD} info -v ${SEARCH} | grep "Priority" | sed 's/Progress: \(.*%\)/Progress: \\\e[31m\1\\\e[0m/g' | sed 's/Priority: Normal/Normal\n/g' | sed 's/Priority: High/High\n/g' | sed 's/Priority: Highest/Highest\n/g') STRING=$(${duser} ${DEL_CONSOLE_CMD} info -v ${SEARCH} | grep "Priority" | sed 's/Progress: \(.*%\)/Progress: \\\e[31m\1\\\e[0m/g') for (( i=0; i<${#GREP_ARR[*]};i++ )); do STRING="$(printf "%s" "${STRING}" | grep "${GREP_ARR[$i]}")" done for (( i=0;i<${#GREPV_ARR[*]};i++ )); do STRING="$(printf "%s" "${STRING}" | grep -v "${GREPV_ARR[$i]}")" done printf "%s" "${STRING}" } handy_deluge() { STRING=$(${duser} ${DEL_CONSOLE_CMD} info | tr '\n' ';' | sed 's/\[\([~,#]*\)\];/\[\1\]\n/g' | grep -v "${DOWNLOADING}" | grep "${DOWN}" | tr ';' '\n' | sed 's/Size: /\nSize: /g' | sed 's/ Ratio/\nRatio/g' | sed 's/Up Speed/\nUp Speed/g'| sed 's/ETA:/\nETA:/g'| grep 'Name:\|State:\|Size:\|ETA:' | tr '\n' ' ' | sed 's/State/\nState/g' | sed 's/Progress/\nProgress/g' | sed 's/Name/\nName/g' | sed 's/Size/\nSize/g') if [ "${STRING}" == "" ]; then printf "%s\n" "${STARS_HANDY}" printf "\t\t\t\t\t\t No Torrents in deluged client\n" printf "%s\n" "${STARS_HANDY}" else printf "%s\n" "${STARS_HANDY}" printf "\t\t\t\t\t\tCustom Deluge Client\n" printf "%s\n" "${STARS_HANDY}" printf "%s\n" "${STRING}" fi } deluge_rms() { if [ "$1" != "" ]; then names=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading\|ID:" | grep -v "Queued" | grep -v "Paused" | grep -B2 "State: " | grep Name | sed 's/ */_/g' | sed 's/Name: //g')) ID=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading" | grep -v "Queued" | grep -v "Paused" | grep -B2 "State: " | grep 'ID: ' | sed 's/ID: //g')) STATE=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading" | grep -v "Queued" | grep -v "Paused" | sed 's/Up Speed/\nUp Speed/g' | grep "State: " | sed 's/ *//g' | sed 's/State://g')) else names=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading\|ID:" | grep -v "Paused" | grep -B2 "State: " | grep Name | sed 's/ */_/g' | sed 's/Name: //g')) ID=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading" | grep -v "Paused" | grep -B2 "State: " | grep 'ID: ' | sed 's/ID: //g')) STATE=($(${duser} ${DEL_CONSOLE_CMD} info | grep -v "State: Downloading" | grep -v "Paused" | sed 's/Up Speed/\nUp Speed/g' | grep "State: " | sed 's/ *//g' | sed 's/State://g')) fi #${DEL_CONSOLE_CMD} info | grep 'Name: ' | sed 's/ */_/g' | sed 's/Name: //g')) if [ ${#names[@]} -eq 0 ]; then stars print_string "No Finished Torrents in deluged client" stars else printf "Which Torrent shall we eliminate?(Enter leading number)\n" echo ${names[1]} for ((i = 0 ; i < ${#names[@]} ; i ++)); do printf "%s: %s\n" "$i" "${names[$i]}" printf "State: %s\n" "${STATE[$i]}" done read -t 10 -n 4 choice1 if [ $choice1 -le ${#names[@]} 2>/dev/null ]; then printf "Remove %s\n" "${names[$choice1]}" printf "Remove Data too?(y/q/*)(q for quit)\n" read -t 10 -n 2 choice2 if [ "$choice2" == "y" ]; then RM_DATA="--remove_data" elif [ "$choice2" == "q" ]; then echo "Cancell Removal" return else RM_DATA="" fi ${duser} ${DEL_CONSOLE_CMD} rm ${RM_DATA} ${ID[$choice1]} if [ $(${duser} ${DEL_CONSOLE_CMD} info | grep -c "Name: ") -eq $((${#names[@]} - 1)) ]; then printf "\e[32mDeletion Successfull\e[0m\n" fi elif [ "${choice1}" == "ALL" ]; then printf "Remove Data too?(y/*)\n" read -t 10 -n 2 choice2 if [ "$choice2" == "y" ]; then RM_DATA="--remove_data" else RM_DATA="" fi for ((i = 0 ; i < ${#names[@]} ; i ++)); do ${duser} ${DEL_CONSOLE_CMD} rm ${RM_DATA} ${ID[$i]} done else printf " is no valid choice\n" fi fi } deluge_rm() { names=($(${duser} ${DEL_CONSOLE_CMD} info | grep 'Name: ' | sed 's/ */_/g' | sed 's/Name: //g')) if [ ${#names[@]} -eq 0 ]; then stars print_string "No Torrents in deluged client" stars exit 0; fi ID=($(${duser} ${DEL_CONSOLE_CMD} info | grep 'ID: ' | sed 's/ID: //g')) printf "Which Torrent shall we eliminate?(Enter leading number)\n" for ((i = 0 ; i < ${#names[@]} ; i ++)); do printf "%s: %s\n" "$i" "${names[$i]}" done read -t 20 choice1 IFS=$';'; set -f array=(${choice1}) unset IFS; set +f for title in ${array[@]}; do if [ ${title} -le ${#names[@]} 2>/dev/null ]; then printf "Remove Torrent %d: %s\n" "${title}" "${names[${title}]}" printf "Remove Data too?(y/q/*)(q for quit)\n" read -t 10 -n 2 choice2 if [ "$choice2" == "y" ]; then RM_DATA="--remove_data" elif [ "$choice2" == "q" ]; then echo "Cancel Removal" return else RM_DATA="" fi ${duser} ${DEL_CONSOLE_CMD} rm ${RM_DATA} ${ID[$title]} if [ $(${duser} ${DEL_CONSOLE_CMD} info | grep -c "Name: ") -eq $((${#names[@]} - 1)) ]; then printf "\e[32mDeletion Successfull\e[0m\n" fi elif [ "${choice1}" == "ALL" ]; then printf "Remove Data too?(y/*)\n" read -t 10 -n 2 choice2 if [ "$choice2" == "y" ]; then RM_DATA="--remove_data" else RM_DATA="" fi for ((i = 0 ; i < ${#names[@]} ; i ++)); do ${duser} ${DEL_CONSOLE_CMD} rm ${RM_DATA} ${ID[$i]} done else printf " is no valid choice\n" fi done } get_config "$@" printf "Custom Deluged Torrent Client Starting\n" if ${HANDY_MODE}; then handy_deluge elif ${RM_MODE}; then deluge_rm elif ${RMS_MODE}; then deluge_rms "${RMS_SEARCH}" elif ${NAMES_MODE}; then printf "%s\n" "$(${duser} ${DEL_CONSOLE_CMD} info | grep 'Name:\|ID:')" elif [[ "${SLIM_MODE}" == true ]] || [[ "${NORMAL_MODE}" == true ]] ||[[ "${SEARCH_MODE}" == true ]] ||[[ "${DETAIL_MODE}" == true ]]; then while [ "${cond}" == "" ]; do if [[ "${SLIM_MODE}" == true ]]; then STRING=$(slim_deluge) elif [[ "${NORMAL_MODE}" == true ]]; then STRING=$(normal_deluge) elif [[ "${SEARCH_MODE}" == true ]]; then STRING=$(search_deluge) elif [[ "${DETAIL_MODE}" == true ]]; then STRING=$(detail_deluge) fi ${duser} find /home/deluge-user/Downloads/Torrents/ -type f -name *".torrent" | while read line do ${duser} mv "$line" "$(echo $line | sed 's/ /_/g')" done ${duser} find /home/deluge-user/Downloads/Torrents/ -type f -name *".torrent" -exec ${DEL_CONSOLE_CMD} add "{}" \; ${duser} find /home/deluge-user/Downloads/Torrents/ -type f -name *".torrent" -exec rm {} \; read -t 1 cond if [[ ${cond} =~ .*magnet.* ]]; then ${duser} ${DEL_CONSOLE_CMD} add "${cond}" cond="" elif [[ ${cond} =~ .*torrent.* ]]; then ${duser} ${DEL_CONSOLE_CMD} add "/home/deluge-user/Downloads/Torrents/${cond}" cond="" elif [ "${cond:0:4}" == "-rms" ]; then deluge_rms cond="" elif [ "${cond:0:3}" == "-rm" ]; then deluge_rm cond="" elif [ "${cond:0:5}" == "-slim" ]; then SLIM_MODE=true NORMAL_MODE=false; SEARCH_MODE=false; DETAIL_MODE=false; cond="" elif [ "${cond:0:5}" == "-norm" ]; then NORMAL_MODE=true SLIM_MODE=false; SEARCH_MODE=false; DETAIL_MODE=false; cond="" elif [ "${cond:0:3}" == "-ON" ]; then DOWNLOADING="Down Speed: 0.0 KiB/s" cond="" elif [ "${cond:0:4}" == "-OFF" ]; then DOWNLOADING="ADCFEGCLG" cond="" elif [ "${cond:0:3}" == "-on" ]; then DOWN="Speed" cond="" elif [ "${cond:0:4}" == "-off" ]; then DOWN="" cond="" elif [ "${cond:0:2}" == "-c" ]; then echo ${cond:3:$((${#cond}-3))} sleep 2 ${duser} ${DEL_CONSOLE_CMD} ${cond:3:$((${#cond}-3))} cond="" fi sleep ${SLEEP_TIME} clear if [ "${STRING}" != "" ]; then stars print_string "Custom Deluge Client" stars echo -e "${STRING}" else stars print_string "No Torrents in deluged client" stars fi if ${STATIC_MODE}; then break; fi done else usage fi