#!/bin/bash # utorrent postprocess script # requires jq # finished: torrenttomedia_utorrent.sh 0 "%S" "%D" "%N" "(%L)" "%I" "%M" # changedstate: torrenttomedia_utorrent.sh 1 "%S" "%D" "$N" "(%L)" "%I" "%M" ################# ### Variables ### ################# data="$*" logfile="/var/log/torrenttomedia_utorrent.log" job="$1" state="$2" directory="$3" title="$4" label=" $5" hexhash="$6" statestring="$7" botid="$utorrentbotid" chatid="$telegramchatid" url="https://api.telegram.org/bot$botid/sendMessage" sent=0 torrenttomedia="nzbToMedia/TorrentToMedia.py" ################# ### Functions ### ################# telegram(){ OUTPUT=$(curl -sq --data "chat_id=$chatid&text=$data" "$url") } statuscheck(){ OK=$(jq .ok <<< "$OUTPUT") if [[ "$OK" == "true" ]]; then post_log "$OUTPUT" sent=1 elif [[ "$OK" == "false" ]]; then ERRCODE=$(jq .error_code <<< "$OUTPUT") if [[ "$ERRCODE" -eq 429 ]]; then post_log "$ERRCODE :: $OUTPUT" SLEEPTIME=$(jq .retry_after <<< "$OUTPUT") sleep "$((SLEEPTIME+1))" elif [[ "$ERRCODE" -eq 400 ]]; then echo "[DEBUG] $ERRCODE: No message provided." post_log "$ERRCODE :: $OUTPUT" sent=1 else echo "[DEBUG] Error Code: $ERRCODE" post_log "$ERRCODE :: $OUTPUT" sent=1 fi fi } post_log(){ echo "$@" >> "$logfile" } pp(){ label=${label:2:-1} python "$torrenttomedia" "$directory" "$title" "$label" "$hexhash" } messagecode(){ case $state in 1) message="errored out ($statestring)." ;; 12) message="joined the queue." ;; 6) message="is out finding peers." ;; 11) message="finished downloading." ;; 5) message="is ready for seeding." ;; 10) message="queued up for seeding." ;; 5) message="is moving to its final destination." ;; 13) message="has been stopped." ;; esac } buildmsg(){ if [[ "$label" == "()" ]]; then unset label fi if [ "$job" == "0" ]; then data="$title$label finished downloading." elif [ "$job" == "1" ]; then messagecode data="$title$label $message" fi } ##################### ### Main Function ### ##################### post_log "$*" buildmsg while [ $sent -eq 0 ]; do telegram statuscheck done if [ "$job" == 0 ]; then pp fi