Skip to content

Instantly share code, notes, and snippets.

@dbiesecke
Created August 15, 2021 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbiesecke/16221c3a5ee28d728fde6489710f840b to your computer and use it in GitHub Desktop.
Save dbiesecke/16221c3a5ee28d728fde6489710f840b to your computer and use it in GitHub Desktop.
#!/bin/bash
#######################################
### NZBGET POST-PROCESSING SCRIPT ###
# Moves downloads to a configured location using rclone and load balances over
# multiple remotes
#
#
# NOTE: Requires sh, coreutils or busybox, screen and rclone. Remotes must be setup in rclone.conf.
########################################
### OPTIONS
# Options to add to the end of the rclone command
#
#
# NOTE: Specify the location of rclone.conf here if it is not in the default
# location for the user running nzbget.
# INFO: Increasing drive-chunk-size will
# increase speed but also increase memory usage.
#
### NZBGET POST-PROCESSING SCRIPT ###
#######################################
export PATH="/shared/bin:$PATH"
RCLONE_DRIVE_CLIENT_ID=456XXXXXXX46v34m6.apps.googleusercontent.com
RCLONE_DRIVE_CLIENT_SECRET=XXXXX
RCLONE_LBA_METHOD=move
OPTIONS="--transfers 2 --drive-chunk-size 64M -P"
CONFIG=/shared/rclone/conf
PROJECT_ID=5dd
NAME=pre
WKEY=
AGENT=`uname -io -sr `
#exit 93
if [ -d "$1" ]; then
NZBPP_FINALDIR=$1
cd "$NZBPP_FINALDIR/../"
PWD=$(pwd)
# NZBPP_CATEGORY=$(basename $PWD)
NZBPP_CATEGORY=$2
echo "$NZBPP_FINALDIR $NZBPP_CATEGORY\n"
FILESIZE=$(du . -c -s | tail -1 | awk '{print $1}')
rclone="rclone"
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
index_curr=0
while $(test -d "$NZBPP_FINALDIR")
do
index_curr=$((index_curr + 1))
if ((index_curr >= 5)); then
exit $POSTPROCESS_ERROR
else
index_curr=$((index_curr + 1))
echo "next loop"
fi
echo "[INFO] Remove samples,sfv and other crap "
find "$NZBPP_FINALDIR" -type d -iname "Sample" -exec rm -fR "{}" \; 2>/dev/null
find "$NZBPP_FINALDIR" -type f -iname "*.sample.*" -exec rm "{}" \; 2>/dev/null
find "$NZBPP_FINALDIR" -type f -iname "*.url" -exec rm "{}" \; 2>/dev/null
find "$NZBPP_FINALDIR" -type f -iname "*.nzb" -exec rm "{}" \; 2>/dev/null
find "$NZBPP_FINALDIR" -type f -iname "*.sfv" -exec rm "{}" \; 2>/dev/null
find "$NZBPP_FINALDIR" -type f -iname "*.exe" -exec rm "{}" \; 2>/dev/null
echo "[INFO] Flatten dir: $NZBPP_FINALDIR"
find "$NZBPP_FINALDIR" -type f -exec mv "{}" "$NZBPP_FINALDIR" \; 2>/dev/null
echo "[INFO] Unpack rar's if found..."
for i in $(find . -type f -iname "*.rar"); do unrar x -y "$i" && echo "[X] Unpack OK, remove file" && rm "$i" ;done
echo "[INFO] remove empty dirs: $NZBPP_FINALDIR"
find "$NZBPP_FINALDIR" -type d -exec rmdir "{}" \; 2>/dev/null
REMOTES=$($rclone listremotes --config $CONFIG| xargs | sed -r 's/ /,/g' | sed -r 's/://g')
REMOTE=$(echo $REMOTES | tr , '\n' | shuf | sed -n '1p')
MOVEPATH=""$REMOTE":"${NZBPP_CATEGORY^^}-iNCOMiNG"/"
MSIZE=$((15 * 1024 * 1024))
USPACE=$($rclone --config "$CONFIG" size "$REMOTE:" --json | jq '.bytes')
FREESPACE=$(($MSIZE - $USPACE))
echo "[INFO] REMOTE: "$REMOTE" Free Space: $(($FREESPACE/1024))MB UploadSize: $(($FILESIZE/1024/1024))GB $MOVEPATH"
if (($FREESPACE > $FILESIZE)); then
echo "[INFO] REMOTE:YES"
else
index_curr=$((index_curr + 1))
echo "next loop"
fi
BASE=$(basename "$NZBPP_FINALDIR")
FILE=$(find "$NZBPP_FINALDIR" -type f -printf '%TY-%Tm-%Td %TH:%TM: %p\n'| sort -n | tail -n1 | awk -F' ' '{print $3}' | sed -r 's/^\.//')
#CAT=${NZBPP_CATEGORY^^}
#NZBPP_CATEGORY=$CAT
#ls -lah $NZBPP_FINALDIR
#MOVEPATH=""$REMOTE":"${NZBPP_CATEGORY}"/"$(basename "$NZBPP_FINALDIR")
# CLOUD="/storage/rclone/"${NZBPP_CATEGORY}"/$FILEBASE"
COMMAND="$rclone --config $CONFIG $RCLONE_LBA_METHOD \""$NZBPP_FINALDIR"\" \""$MOVEPATH/"\" "$OPTIONS""
echo "[INFO] RCLONE: "$COMMAND""
eval "$COMMAND"
#msg=$($COMMAND 2>&1)
exit_code=$?
if [ $exit_code -ne 0 ] ; then
echo "ERROR $msg"
# index_curr=$((index_curr + 1))
# echo "next loop"
else
#if no errors occured just log the message
echo "[X] Upload $msg"
# find "$NZBPP_FINALDIR" -type d -exec rmdir "{}" \; 2>/dev/null
# rmdir "$NZBPP_FINALDIR" >/dev/null 2>/dev/null
test -d "$NZBPP_FINALDIR" && echo "[X] Upload ... $(basename $NZBPP_FINALDIR) SUCCESS"
fi
#eval "$COMMAND"
# export NZBPP_FINALDIR="$CLOUD"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment