Skip to content

Instantly share code, notes, and snippets.

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 techinpark2/6671840734018c2da5ce87416e2cd1ea to your computer and use it in GitHub Desktop.
Save techinpark2/6671840734018c2da5ce87416e2cd1ea to your computer and use it in GitHub Desktop.
Transmission download complete shell script for moving complete files and removing its torrent entry
#!/bin/bash
#
# 2017. 08. 31 Written by Awesometic
# 2017. 09. 25 Updated
# 2018. 01. 17 Updated
# Split log file when the file size is bigger than 100 KB
# Remove unimportant comments
# Clean up the descriptions
# Clean up the codes
# Add datetime for each log
# Add a guide for applying this script
# 2018. 10. 08 Updated
# Add double quotes to prevent splitting words
#
# Check before use this script:
# A. Add debian-transmission into sudores file to grant sudo permission without password.
# - Follow a guide below.
# 1. $ sudo visudo
# 2. $ sudo usermod -a -G sudo debian-transmission
# 3. Paste it at the end of the file: debian-transmission ALL=(ALL:ALL) NOPASSWD:ALL
# 4. Save and quit.
# B. Change written paths to yours.
# C. Make sure that debian-transmission has proper permission for working directories.
# D. Make sure that giving "execute" permission to this script and debian-transmission can execute this.
# E. Enable using "done script" and type absolute path of this script, and restart transmission-daemon service.
#
# Plz feel free to give any suggestions and feedbacks.
# - http://awesometic.tistory.com
# - https://gist.github.com/awesometic/253b740d45f8e5f95b56ec24f33a9444
# Enjoy.
DATETIME=$(date '+%Y-%m-%d %H:%M:%S')
# User define constants
SERVER="9091 --auth awesometic:password"
OWNER_UID=awesometic
OWNER_GID=nas
TORRENT_DIR="/media/nas/torrent"
DEST_DIR="/media/nas/TV"
LOG_FILE_NAME="auto_move.log"
# Array that has managed file names
# Type your watching program's name wrapping with double quotation.
# It is case sensitive, so I recommend you refer to torrent's file name in advance,
# before type the program's name.
#
# For example, when you'd like to write in "Walking Dead",
# but the real downloaded file name contains "walkingdead",
# the name you wrote "Walking Dead" is ignored
# and writing in "walkingdead" is right.
#
# So, please be careful when you write.
MANAGED=( "라디오스타" "해피 투게더" "나 혼자 산다" "무한도전" "아는 형님" "어서와 한국은 처음이지" "미운 우리 새끼" )
# DO IT
LOG_FILE_PATH="$TORRENT_DIR"/"$LOG_FILE_NAME"
if [ -f "$LOG_FILE_PATH" ] && [ "$(stat -c%s $LOG_FILE_PATH)" -gt 100000 ]; then
LOG_FILE_COUNT=$(find "$TORRENT_DIR" -maxdepth 1 -name "$LOG_FILE_NAME*" | wc -l)
sudo mv "$LOG_FILE_PATH" "$LOG_FILE_PATH"."$LOG_FILE_COUNT"
fi
if ! [ -f "$LOG_FILE_PATH" ]; then
sudo touch "$LOG_FILE_PATH"
sudo chmod 664 "$LOG_FILE_PATH"
sudo chown "$OWNER_UID":"$OWNER_GID" "$LOG_FILE_PATH"
fi
echo "[""$DATETIME""] Begin: ""$TR_TORRENT_ID":"$TR_TORRENT_NAME" >> "$LOG_FILE_PATH"
transmission-remote "$SERVER" --list >> "$LOG_FILE_PATH"
for NAME in "${MANAGED[@]}"; do
if [[ "$TR_TORRENT_NAME" == *"$NAME"* ]]; then
MOVEDIR="$DEST_DIR"/"$NAME"
if ! [ -d "$MOVEDIR" ]; then
echo "[""$DATETIME""] Create a new directory: ""$MOVEDIR" >> "$LOG_FILE_PATH"
sudo mkdir -p "$MOVEDIR"
fi
if ! [ -w "$MOVEDIR" ]; then
echo "[""$DATETIME""] Change ownership temporary to move file" >> "$LOG_FILE_PATH"
sudo chown debian-transmission:nogroup "$MOVEDIR"
fi
echo "[""$DATETIME""] Move managed file: ""$MOVEDIR"/"$TR_TORRENT_NAME" >> "$LOG_FILE_PATH"
transmission-remote "$SERVER" --torrent "$TR_TORRENT_ID" --move "$MOVEDIR" >> "$LOG_FILE_PATH"
echo "[""$DATETIME""] Change/Recover ownership and permission" >> "$LOG_FILE_PATH"
sudo chmod -R 755 "$MOVEDIR"
sudo chown -R "$OWNER_UID":"$OWNER_GID" "$MOVEDIR"
break
fi
done
echo "[""$DATETIME""] Remove completed torrent" >> "$LOG_FILE_PATH"
transmission-remote "$SERVER" --torrent "$TR_TORRENT_ID" --remove >> "$LOG_FILE_PATH"
echo "[""$DATETIME""] Finish" >> "$LOG_FILE_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment