Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # posttorrent.sh created by Killemov
- # updated by Me 2015-02-23 rev 0.1
- #
- # env variables passed by transmission when calling this script:
- #
- # TR_APP_VERSION
- # TR_TIME_LOCALTIME
- # TR_TORRENT_DIR
- # TR_TORRENT_HASH
- # TR_TORRENT_ID
- # TR_TORRENT_NAME
- #
- {
- umask 0002
- # Username for transmission remote
- TR_USERNAME="REDACTED"
- # Password for transmission remote
- TR_PASSWORD="REDACTED"
- # Directory torrent is in to assume it's a TV show
- TV_SHOW_DIR="/mnt/big1/tvshows"
- KIDS_TV_SHOW_DIR="/mnt/big1/kids/TV"
- # Directory torrent is in to assume it's a movie
- MOVIE_DIR="/mnt/big1/movies"
- KIDS_MOVIE_DIR="/mnt/big1/kids/Movies"
- # Directory to move post-post-processed torrent data to
- SEED_DIR="/mnt/big1/seeding"
- # Match the types of files we like
- tv_show_extensions="avi,mkv,divx,xvid,mp4,mpg,mpeg,ts,srt,idx,sub"
- movie_extensions="avi,mkv,divx,mp4,ts,mpg,mpeg,srt,idx,sub"
- tv_show_extensions_rev="\.$(echo $tv_show_extensions | sed 's;,;\$\|\\\.;g')$"
- movie_extensions_rev="\.$(echo $movie_extensions | sed 's;,;\$\|\\\.;g')$"
- # Temp Log file to send to PushBullet after script completion
- LOG_ONE=$(mktemp)
- # Global log file, file where all script output can be found
- LOG_MAIN="/var/log/transmission-daemon/posttorrent.log"
- # Get current time.
- NOW=$(date +%Y-%m-%d\ %H:%M:%S)
- # Build Transmission command
- TR_CMD="/usr/bin/transmission-remote -n "${TR_USERNAME}":"${TR_PASSWORD}" -t ${TR_TORRENT_HASH}"
- # Source directory, should not be changed.
- SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}"
- # Directory to store the un-compressed files in..
- DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/"
- # How to log and display output
- log() {
- echo $NOW "$@" | tee -a $LOG_ONE $LOG_MAIN
- }
- ## Actual processing starts here.
- log "Initializing posttorrent script for $TR_TORRENT_NAME"
- # Check that files and folders are read/writeable
- FAILDIR=0
- for i in "$TR_TORRENT_DIR" "$TV_SHOW_DIR" "$KIDS_TV_SHOW_DIR" "$MOVIE_DIR" "$KIDS_MOVIE_DIR" "$SEED_DIR" "$SRC_DIR"; do
- if [[ ! -r "$i" || ! -w "$i" ]]; then
- log "$i is either not readable or not writeable by this script"
- FAILDIR=1
- fi
- done
- if [ ${FAILDIR} -ne 0 ]; then
- log $NOW "Encountered errors. Exiting"
- cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
- exit 1
- fi
- cd "$TR_TORRENT_DIR"
- # Check for any RAR files
- if [ -d "$SRC_DIR" ]; then
- IFS=$'\n'
- unset RAR_FILES i
- for RAR_FILE in $( find "$SRC_DIR" -type f -iname "*.rar" ); do
- if [[ "$RAR_FILE" =~ .*part.*.rar ]]; then
- if [[ "$RAR_FILE" =~ .*part0*1.rar ]]; then
- RAR_FILES[i++]="$RAR_FILE"
- fi
- else
- RAR_FILES[i++]="$RAR_FILE"
- fi
- done
- # UnRAR any RAR files to the parent directory
- if [ ${#RAR_FILES} -eq 0 ]; then
- log "Did not identify RAR files in $TR_TORRENT_NAME"
- else
- log "Identified RAR files in ${TR_TORRENT_NAME}. Beginning extraction"
- for RAR_FILE in "${RAR_FILES[@]}"; do
- nice -n15 unrar x -y -o+ -p- "$RAR_FILE" "${TR_TORRENT_DIR}" | tee -a $LOG_ONE $LOG_MAIN
- if [ $? -gt 0 ]; then
- log "Error extracting ${TR_TORRENT_NAME}. Forcing torrent re-verification and exiting script"
- eval $TR_CMD --verify --start | tee -a $LOG_ONE $LOG_MAIN
- cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
- exit 1
- fi
- done
- log "File extraction from RAR files in ${TR_TORRENT_NAME} complete"
- fi
- fi
- ## TV Show specific actions
- if [[ "$SRC_DIR" =~ "$TV_SHOW_DIR" || "$SRC_DIR" =~ "$KIDS_TV_SHOW_DIR" ]]; then
- log "Identified $TR_TORRENT_NAME as a TV show"
- # Actions to perform if the torrent is data files in a directory
- # The unrarred files should already be in the correct $TVSHOW_DIR now.
- # Hard link supported filetypes to $TR_TORRENT_DIR. Move torrent data to $SEED_DIR.
- if [ -d "$SRC_DIR" ]; then
- find "${SRC_DIR}" -type f | egrep -i "${tv_show_extensions_rev}" | while IFS= read _supported_file; do
- shopt -s nocasematch
- if [[ "${_supported_file}" == *"sample"* ]] || [[ "${_supported_file}" == "rarbg.com.mp4" ]] || [[ "${_supported_file}" == "ETRG.mp4" ]]; then
- log "Not hardlinking ${_supported_file}"
- elif [ ! -z "${_supported_file}" ]; then
- log "Creating hardlink to $_supported_file"
- ln -f "${_supported_file}" "${TR_TORRENT_DIR}/$(basename "${_supported_file}")"
- if [ $? -gt 0 ]; then
- log "Failed to create hard link from ${_supported_file} to ${TR_TORRENT_DIR}/$(basename ${_supported_file}). Exiting"
- cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
- exit 1
- fi
- else
- log "No files to symlink in ${TR_TORRENT_NAME}"
- fi
- shopt -u nocasematch
- done
- log "Telling Transmission to move $TR_TORRENT_NAME to $SEED_DIR"
- eval $TR_CMD --move "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
- # Actions to perform if TV show is a single file
- # Make hard link to seeding dir and tell Transmission to look for it there instead
- elif [ -f "${SRC_DIR}" ]; then
- log "Creating hardlink to ${SEED_DIR}/${TR_TORRENT_NAME}"
- ln -f "${SRC_DIR}" "${SEED_DIR}/${TR_TORRENT_NAME}"
- log "Having Transmission find $TR_TORRENT_NAME in $SEED_DIR"
- eval $TR_CMD --find "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
- fi
- ## Movie specific commands
- elif [[ "$SRC_DIR" =~ "$MOVIE_DIR" || "$SRC_DIR" =~ "$KIDS_MOVIE_DIR" ]]; then
- log "Identified $TR_TORRENT_NAME as a movie"
- # Actions to perform if the torrent data is fils in a directory
- # Hard link supported filetypes to $TR_TORRENT_DIR. Move torrent data to $SEED_DIR.
- if [ -d "$SRC_DIR" ]; then
- find "${SRC_DIR}" -type f | egrep -i "${movie_extensions_rev}" | while IFS= read _supported_file; do
- shopt -s nocasematch
- if [[ "${_supported_file}" == *"sample"* ]] || [[ "${_supported_file}" == "rarbg.com.mp4" ]] || [[ "${_supported_file}" == "ETRG.mp4" ]]; then
- log "Not hardlinking ${_supported_file}"
- elif [ ! -z ${_supported_file} ]; then
- log "Creating hardlink to $_supported_file"
- ln -f "${_supported_file}" "${TR_TORRENT_DIR}/$(basename ${_supported_file})"
- if [ $? -gt 0 ]; then
- log "Failed to create hard link from ${_supported_file} to ${TR_TORRENT_DIR}/$(basename ${_supported_file}). Exiting"
- cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
- exit 1
- fi
- else
- log "No files to symlink in ${TR_TORRENT_NAME}"
- fi
- shopt -u nocasematch
- done
- log "Telling Transmission to move $TR_TORRENT_NAME to $SEED_DIR"
- eval $TR_CMD --move "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
- # Actions to perform if movie is a single file
- # Make hard link to seeding dir and tell Transmission to look for it there instead
- elif [ -f "${SRC_DIR}" ]; then
- log "Creating hardlink to ${SEED_DIR}/${TR_TORRENT_NAME}"
- ln -f "${SRC_DIR}" "${SEED_DIR}/${TR_TORRENT_NAME}"
- log "Having Transmission find $TR_TORRENT_NAME in $SEED_DIR"
- eval $TR_CMD --find "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
- fi
- ## Not a TV show or movie
- else
- log "${TR_TORRENT_NAME} was not identified as a tv or movie"
- fi
- log "Completed post-processing of $TR_TORRENT_NAME"
- # cat "$LOG_ONE"| /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent.sh successful for ${TR_TORRENT_NAME}"
- /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "${TR_TORRENT_NAME}" "$(date)\nPosttorrent success\n${TR_TORRENT_NAME}\n${TR_TORRENT_DIR}"
- rm "$LOG_ONE"
- }
- # To Do
- # make script more robust
- # propers, repacks, etc?
- # did we unrar any new rars files to be processed? (subs, usually. dumb.)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement