Advertisement
Guest User

posttorrent.sh

a guest
Jul 13th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.77 KB | None | 0 0
  1. #!/bin/bash
  2. # posttorrent.sh created by Killemov
  3. # updated by Me 2015-02-23 rev 0.1
  4. #
  5. # env variables passed by transmission when calling this script:
  6. #
  7. #    TR_APP_VERSION
  8. #    TR_TIME_LOCALTIME
  9. #    TR_TORRENT_DIR
  10. #    TR_TORRENT_HASH
  11. #    TR_TORRENT_ID
  12. #    TR_TORRENT_NAME
  13. #
  14. {
  15.         umask 0002
  16.  
  17.         # Username for transmission remote
  18.         TR_USERNAME="REDACTED"
  19.  
  20.         # Password for transmission remote
  21.         TR_PASSWORD="REDACTED"
  22.  
  23.         # Directory torrent is in to assume it's a TV show
  24.         TV_SHOW_DIR="/mnt/big1/tvshows"
  25.         KIDS_TV_SHOW_DIR="/mnt/big1/kids/TV"
  26.  
  27.         # Directory torrent is in to assume it's a movie
  28.         MOVIE_DIR="/mnt/big1/movies"
  29.         KIDS_MOVIE_DIR="/mnt/big1/kids/Movies"
  30.  
  31.         # Directory to move post-post-processed torrent data to
  32.         SEED_DIR="/mnt/big1/seeding"
  33.  
  34.         # Match the types of files we like
  35.         tv_show_extensions="avi,mkv,divx,xvid,mp4,mpg,mpeg,ts,srt,idx,sub"
  36.         movie_extensions="avi,mkv,divx,mp4,ts,mpg,mpeg,srt,idx,sub"
  37.  
  38.         tv_show_extensions_rev="\.$(echo $tv_show_extensions | sed 's;,;\$\|\\\.;g')$"
  39.         movie_extensions_rev="\.$(echo $movie_extensions | sed 's;,;\$\|\\\.;g')$"
  40.  
  41.         # Temp Log file to send to PushBullet after script completion
  42.         LOG_ONE=$(mktemp)
  43.  
  44.         # Global log file, file where all script output can be found
  45.         LOG_MAIN="/var/log/transmission-daemon/posttorrent.log"
  46.  
  47.         # Get current time.
  48.         NOW=$(date +%Y-%m-%d\ %H:%M:%S)
  49.  
  50.         # Build Transmission command
  51.         TR_CMD="/usr/bin/transmission-remote -n "${TR_USERNAME}":"${TR_PASSWORD}" -t ${TR_TORRENT_HASH}"
  52.  
  53.         # Source directory, should not be changed.
  54.         SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}"
  55.  
  56.         # Directory to store the un-compressed files in..
  57.         DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/"
  58.  
  59.         # How to log and display output
  60.         log() {
  61.                 echo $NOW "$@" | tee -a $LOG_ONE $LOG_MAIN
  62.         }
  63.  
  64.  
  65.         ## Actual processing starts here.
  66.  
  67.         log "Initializing posttorrent script for $TR_TORRENT_NAME"
  68.  
  69.         # Check that files and folders are read/writeable
  70.  
  71.         FAILDIR=0
  72.  
  73.         for i in "$TR_TORRENT_DIR" "$TV_SHOW_DIR" "$KIDS_TV_SHOW_DIR" "$MOVIE_DIR" "$KIDS_MOVIE_DIR" "$SEED_DIR" "$SRC_DIR"; do
  74.                 if [[ ! -r "$i" || ! -w "$i" ]]; then
  75.                         log "$i is either not readable or not writeable by this script"
  76.                         FAILDIR=1
  77.                 fi
  78.         done
  79.  
  80.         if [ ${FAILDIR} -ne 0 ]; then
  81.                         log $NOW "Encountered errors. Exiting"
  82.                         cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
  83.                         exit 1
  84.         fi
  85.  
  86.  
  87.         cd "$TR_TORRENT_DIR"
  88.  
  89.         # Check for any RAR files
  90.         if [ -d "$SRC_DIR" ]; then
  91.                 IFS=$'\n'
  92.                 unset RAR_FILES i
  93.                 for RAR_FILE in $( find "$SRC_DIR" -type f -iname "*.rar" ); do
  94.                         if [[ "$RAR_FILE" =~ .*part.*.rar ]]; then
  95.                                 if [[ "$RAR_FILE" =~ .*part0*1.rar ]]; then
  96.                                         RAR_FILES[i++]="$RAR_FILE"
  97.                                 fi
  98.                         else
  99.                                 RAR_FILES[i++]="$RAR_FILE"
  100.                         fi
  101.                 done
  102.  
  103.         # UnRAR any RAR files to the parent directory
  104.                 if [ ${#RAR_FILES} -eq 0 ]; then
  105.                         log "Did not identify RAR files in $TR_TORRENT_NAME"
  106.                 else
  107.                         log "Identified RAR files in ${TR_TORRENT_NAME}. Beginning extraction"
  108.                         for RAR_FILE in "${RAR_FILES[@]}"; do
  109.                                 nice -n15 unrar x -y -o+ -p- "$RAR_FILE" "${TR_TORRENT_DIR}" | tee -a $LOG_ONE $LOG_MAIN
  110.                                 if [ $? -gt 0 ]; then
  111.                                         log "Error extracting ${TR_TORRENT_NAME}. Forcing torrent re-verification and exiting script"
  112.                                         eval $TR_CMD --verify --start | tee -a $LOG_ONE $LOG_MAIN
  113.                                         cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
  114.                                         exit 1
  115.                                 fi
  116.                         done
  117.                                 log "File extraction from RAR files in ${TR_TORRENT_NAME} complete"
  118.                 fi
  119.         fi
  120.  
  121.         ## TV Show specific actions
  122.         if [[ "$SRC_DIR" =~ "$TV_SHOW_DIR" || "$SRC_DIR" =~ "$KIDS_TV_SHOW_DIR" ]]; then
  123.  
  124.                 log "Identified $TR_TORRENT_NAME as a TV show"
  125.  
  126.         # Actions to perform if the torrent is data files in a directory
  127.         # The unrarred files should already be in the correct $TVSHOW_DIR now.
  128.         # Hard link supported filetypes to $TR_TORRENT_DIR. Move torrent data to $SEED_DIR.
  129.                 if [ -d "$SRC_DIR" ]; then
  130.                                 find "${SRC_DIR}" -type f | egrep -i "${tv_show_extensions_rev}" | while IFS= read _supported_file; do
  131.                                         shopt -s nocasematch
  132.                                         if [[ "${_supported_file}" == *"sample"* ]] || [[ "${_supported_file}" == "rarbg.com.mp4" ]] || [[  "${_supported_file}" == "ETRG.mp4" ]]; then
  133.                                                 log "Not hardlinking ${_supported_file}"
  134.                                         elif [ ! -z "${_supported_file}" ]; then
  135.                                                 log "Creating hardlink to $_supported_file"
  136.                                                 ln -f "${_supported_file}" "${TR_TORRENT_DIR}/$(basename "${_supported_file}")"
  137.                                                         if [ $? -gt 0 ]; then
  138.                                                                 log "Failed to create hard link from ${_supported_file} to ${TR_TORRENT_DIR}/$(basename ${_supported_file}). Exiting"
  139.                                                                 cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
  140.                                                                 exit 1
  141.                                                         fi
  142.                                         else
  143.                                                 log "No files to symlink in ${TR_TORRENT_NAME}"
  144.                                         fi
  145.                                         shopt -u nocasematch
  146.                                 done
  147.                                 log "Telling Transmission to move $TR_TORRENT_NAME to $SEED_DIR"
  148.                                 eval $TR_CMD --move "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
  149.  
  150.         # Actions to perform if TV show is a single file
  151.         # Make hard link to seeding dir and tell Transmission to look for it there instead
  152.                 elif [ -f "${SRC_DIR}" ]; then
  153.                         log "Creating hardlink to ${SEED_DIR}/${TR_TORRENT_NAME}"
  154.                         ln -f "${SRC_DIR}" "${SEED_DIR}/${TR_TORRENT_NAME}"
  155.                         log "Having Transmission find $TR_TORRENT_NAME in $SEED_DIR"
  156.                         eval $TR_CMD --find "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
  157.                 fi
  158.  
  159.         ## Movie specific commands
  160.         elif [[ "$SRC_DIR" =~ "$MOVIE_DIR" || "$SRC_DIR" =~ "$KIDS_MOVIE_DIR" ]]; then
  161.  
  162.                 log "Identified $TR_TORRENT_NAME as a movie"
  163.  
  164.         # Actions to perform if the torrent data is fils in a directory
  165.         # Hard link supported filetypes to $TR_TORRENT_DIR. Move torrent data to $SEED_DIR.
  166.                 if [ -d "$SRC_DIR" ]; then
  167.                                 find "${SRC_DIR}" -type f | egrep -i "${movie_extensions_rev}" | while IFS= read _supported_file; do
  168.                                         shopt -s nocasematch
  169.                                         if [[ "${_supported_file}" == *"sample"* ]] || [[ "${_supported_file}" == "rarbg.com.mp4" ]] || [[ "${_supported_file}" == "ETRG.mp4" ]]; then
  170.                                                 log "Not hardlinking ${_supported_file}"
  171.                                         elif [ ! -z ${_supported_file} ]; then
  172.                                                 log "Creating hardlink to $_supported_file"
  173.                                                 ln -f "${_supported_file}" "${TR_TORRENT_DIR}/$(basename ${_supported_file})"
  174.                                                         if [ $? -gt 0 ]; then
  175.                                                                 log "Failed to create hard link from ${_supported_file} to ${TR_TORRENT_DIR}/$(basename ${_supported_file}). Exiting"
  176.                                                                 cat "$LOG_ONE" | /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent failed for ${TR_TORRENT_NAME}"
  177.                                                                 exit 1
  178.                                                         fi
  179.                                         else
  180.                                                 log "No files to symlink in ${TR_TORRENT_NAME}"
  181.                                         fi
  182.                                         shopt -u nocasematch
  183.                                 done
  184.                                 log "Telling Transmission to move $TR_TORRENT_NAME to $SEED_DIR"
  185.                                 eval $TR_CMD --move "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
  186.  
  187.         # Actions to perform if movie is a single file
  188.         # Make hard link to seeding dir and tell Transmission to look for it there instead
  189.                 elif [ -f "${SRC_DIR}" ]; then
  190.                         log "Creating hardlink to ${SEED_DIR}/${TR_TORRENT_NAME}"
  191.                         ln -f "${SRC_DIR}" "${SEED_DIR}/${TR_TORRENT_NAME}"
  192.                         log "Having Transmission find $TR_TORRENT_NAME in $SEED_DIR"
  193.                         eval $TR_CMD --find "${SEED_DIR}" | tee -a $LOG_ONE $LOG_MAIN
  194.                 fi
  195.  
  196.         ## Not a TV show or movie
  197.         else
  198.                 log "${TR_TORRENT_NAME} was not identified as a tv or movie"
  199.         fi
  200.  
  201.         log "Completed post-processing of $TR_TORRENT_NAME"
  202. #       cat "$LOG_ONE"| /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "posttorrent.sh successful for ${TR_TORRENT_NAME}"
  203.         /usr/bin/pushbullet push PUSHBULLETCHANNELNAME note "${TR_TORRENT_NAME}" "$(date)\nPosttorrent success\n${TR_TORRENT_NAME}\n${TR_TORRENT_DIR}"
  204.  
  205.         rm "$LOG_ONE"
  206. }
  207.  
  208.         # To Do
  209.         # make script more robust
  210.         # propers, repacks, etc?
  211.         # did we unrar any new rars files to be processed? (subs, usually. dumb.)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement