Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #! /bin/bash
  2. # posttorrent.sh by Killemov
  3. {
  4. # Log file, file where we tell what events have been processed.
  5. LOG_FILE=/var/log/posttorrent.log
  6. # Username for transmission remote.
  7. TR_USERNAME="username"
  8. # Password for transmission remote.
  9. TR_PASSWORD="password"
  10. # Get current time.
  11. NOW=$(date +%Y-%m-%d\ %H:%M:%S)
  12. # Source directory, should not be changed.
  13. SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}"
  14. # Directory to store the un-compressed files in..
  15. DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/"
  16. # This parameter string could be passed from Transmission in the future.
  17. TR_TORRENT_PARAMETER="EXTRACT SLEEP1h"
  18.  
  19. if [ -e "$SRC_DIR/keep" ]; then
  20. TR_TORRENT_PARAMETER="$TR_TORRENT_PARAMETER KEEP"
  21. fi
  22.  
  23. if [ -e "$SRC_DIR/exit" ]; then
  24. TR_TORRENT_PARAMETER="EXIT"
  25. fi
  26.  
  27. # Actual processing starts here.
  28. if [[ "$TR_TORRENT_PARAMETER" =~ "EXIT" ]]; then
  29. echo $NOW "Exiting $TR_TORRENT_NAME" >> $LOG_FILE
  30. exit 0
  31. fi
  32.  
  33. if [[ "$TR_TORRENT_PARAMETER" =~ "EXTRACT" ]]; then
  34. cd $TR_TORRENT_DIR
  35. if [ -d "$SRC_DIR" ]; then
  36. IFS=$'\n'
  37. unset RAR_FILES i
  38. for RAR_FILE in $( find "$SRC_DIR" -type f -iname "*.rar" ); do
  39. if [[ $RAR_FILE =~ .*part.*.rar ]]; then
  40. if [[ $RAR_FILE =~ .*part0*1.rar ]]; then
  41. RAR_FILES[i++]=$RAR_FILE
  42. fi
  43. else
  44. RAR_FILES[i++]=$RAR_FILE
  45. fi
  46. done
  47. unset IFS
  48.  
  49. if [ ${#RAR_FILES} -gt 0 ]; then
  50. for RAR_FILE in "${RAR_FILES[@]}"; do
  51. unrar x -inul "$RAR_FILE" "$DEST_DIR"
  52. if [ $? -gt 0 ]; then
  53. echo $NOW "Error unrarring $TR_TORRENT_NAME" >> $LOG_FILE
  54. transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t $TR_TORRENT_HASH --verify --start
  55. exit 0
  56. fi
  57. done
  58. if [[ ! "$TR_TORRENT_PARAMETER" =~ "KEEP" ]]; then
  59. SLEEP=$(expr match "$TR_TORRENT_PARAMETER" '.*SLEEP\([0-9a-zA-Z]*\)')
  60. if [ ${#SLEEP} -gt 0 ]; then
  61. sleep $SLEEP
  62. fi
  63. transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t $TR_TORRENT_HASH --remove-and-delete
  64. fi
  65. echo $NOW "Unrarred $TR_TORRENT_NAME" >> $LOG_FILE
  66. fi
  67. fi
  68. fi
  69. } &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement