Advertisement
Guest User

Enhanced Posttorrent Transmission script

a guest
Feb 23rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.28 KB | None | 0 0
  1. #!/bin/bash
  2. # posttorrent.sh created by Killemov
  3. # updated by Jordan Oxborrow 2015-02-23
  4. {
  5.         umask 0002
  6.  
  7.         # Log file, file where we tell what events have been processed.
  8.         LOG_FILE="/var/log/transmission-daemon/posttorrent.log"
  9.  
  10.         # Username for transmission remote
  11.         TR_USERNAME="username"
  12.  
  13.         # Password for transmission remote
  14.         TR_PASSWORD="password"
  15.  
  16.         # Directory torrent is in to assume it's a TV show
  17.         TV_SHOW_DIR="/path/to/tvshows"
  18.  
  19.         # Directory torrent is in to assume it's a movie
  20.         MOVIE_DIR="/path/to/movies"
  21.  
  22.         # Directory to move post-post-processed torrent data to
  23.         SEED_DIR="/path/to/seeding"
  24.  
  25.         # Haven't done anything  with this information yet
  26.         tv_show_extensions="avi,mkv,divx,mp4,srt,idx,sub"
  27.         movie_extensions="avi,mkv,divx,mp4,ts,iso,img,srt,idx,sub"
  28.  
  29.         tv_show_extensions_rev="\.$(echo $tv_show_extensions | sed 's;,;\$\|\\\.;g')$"
  30.         movie_extensions_rev="\.$(echo $movie_extensions | sed 's;,;\$\|\\\.;g')$"
  31.  
  32.         # Get current time.
  33.         NOW=$(date +%Y-%m-%d\ %H:%M:%S)
  34.  
  35.         # Build Transmission command
  36.         TR_CMD="transmission-remote -n "${TR_USERNAME}":"${TR_PASSWORD}" -t ${TR_TORRENT_HASH}"
  37.  
  38.         # Source directory, should not be changed.
  39.         SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}"
  40.  
  41.         # Directory to store the un-compressed files in..
  42.         DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/"
  43.  
  44.         ## Actual processing starts here.
  45.  
  46.         echo $NOW "Initializing posttorrent script for $TR_TORRENT_NAME" >> $LOG_FILE
  47.  
  48.         cd "$TR_TORRENT_DIR"
  49.  
  50.         # Check for any RAR files
  51.         if [ -d "$SRC_DIR" ]; then
  52.                 IFS=$'\n'
  53.                 unset RAR_FILES i
  54.                 for RAR_FILE in $( find "$SRC_DIR" -type f -iname "*.rar" ); do
  55.                         if [[ $RAR_FILE =~ .*part.*.rar ]]; then
  56.                                 if [[ $RAR_FILE =~ .*part0*1.rar ]]; then
  57.                                         RAR_FILES[i++]=$RAR_FILE
  58.                                 fi
  59.                         else
  60.                                 RAR_FILES[i++]=$RAR_FILE
  61.                         fi
  62.                 done
  63.                 unset IFS
  64.  
  65.         # UnRAR any RAR files to the parent directory
  66.                 if [ ${#RAR_FILES} -eq 0 ]; then
  67.                         echo $NOW "Did not identify RAR files in $TR_TORRENT_NAME" >> $LOG_FILE
  68.                 else
  69.                         echo $NOW "Identified RAR files in $TR_TORRENT_NAME" >> $LOG_FILE
  70.                         for RAR_FILE in "${RAR_FILES[@]}"; do
  71.                                 nice -n15 unrar x -y -o+ -p- -inul "$RAR_FILE" "${TR_TORRENT_DIR}"
  72.                                 if [ $? -gt 0 ]; then
  73.                                         echo $NOW "Error unrarring $TR_TORRENT_NAME: Forcing torrent re-verify and exiting script" >> $LOG_FILE
  74.                                         transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t $TR_TORRENT_HASH --verify --start
  75.                                         exit 1
  76.                                 fi
  77.                         done
  78.                                 echo $NOW "Unrarred $TR_TORRENT_NAME" >> $LOG_FILE
  79.                 fi
  80.  
  81.         fi
  82.  
  83.         ## TV Show specific actions
  84.         if [[ "$SRC_DIR" =~ "$TV_SHOW_DIR" ]]; then
  85.  
  86.                 echo $NOW "Identified $TR_TORRENT_NAME as a TV show" >> $LOG_FILE
  87.  
  88.         # Actions to perform if the torrent data is fils in a directory
  89.         # The unrarred files should already be in $TVSHOW_DIR now.
  90.         # Hard link supported filetypes to $TVSHOW_DIR. Move torrent data to $SEED_DIR.
  91.                 if [ -d "$SRC_DIR" ]; then
  92.                                 for _supported_file in $(find "${SRC_DIR}" -type f | egrep -i "${tv_show_extensions_rev}"); do
  93.                                         echo $NOW "Creating hardlink to $_supported_file" >> $LOG_FILE
  94.                                         ln -f "${_supported_file}" "${TR_TORRENT_DIR}/$(basename ${_supported_file})"
  95.                                 done
  96.                                 echo $NOW "Telling Transmission to move $TR_TORRENT_NAME to $SEED_DIR" >> $LOG_FILE
  97.                                 $TR_CMD --move "${SEED_DIR}"
  98.  
  99.         # Actions to perform if TV show is a single file
  100.         # Make hard link to seeding dir and tell Transmission to look for it there instead
  101.                 elif [ -f "${SRC_DIR}" ]; then
  102.                         echo $NOW "Creating hardlink to ${SEED_DIR}/${TR_TORRENT_NAME}" >> $LOG_FILE
  103.                         ln -f "${SRC_DIR}" "${SEED_DIR}/${TR_TORRENT_NAME}"
  104.                         echo $NOW "Having Transmission find $TR_TORRENT_NAME in $SEED_DIR" >> $LOG_FILE
  105.                         $TR_CMD --find "${SEED_DIR}"
  106.                 fi
  107.  
  108.         ## Movie specific commands
  109.         elif [[ "${SRC_DIR}" =~ "${MOVIE_DIR}" ]]; then
  110.                 echo its a movie
  111.  
  112.         ## Not a TV show or movie
  113.         else
  114.                 echo "its not a tv or movie. dont touch it."
  115.         fi
  116.  
  117.         # To Do
  118.         # go through showrss and change feed
  119.         # Do any tv shows actually come with Samples and subs?  How to deal?
  120.  
  121.         echo $NOW "Completed post-processing of $TR_TORRENT_NAME" >> $LOG_FILE
  122.  
  123. } &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement