From 364597a7056ce2008831d962a26252cb155999b2 Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Wed, 20 Nov 2019 22:45:32 -0800 Subject: [PATCH] Add new file --- torrenttomedia_utorrent.sh | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 torrenttomedia_utorrent.sh diff --git a/ torrenttomedia_utorrent.sh b/ torrenttomedia_utorrent.sh new file mode 100644 index 0000000..f38f5d2 --- /dev/null +++ b/ torrenttomedia_utorrent.sh @@ -0,0 +1,119 @@ +#!/bin/bash +# utorrent postprocess script +# 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="$utorentbotid" +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 \ No newline at end of file