Deluge: Auto Extract and Rename Archives after download complete

With deluge, there is a plugin called Extrator. We decided to write our own script instead. It gives much more flexibility. First of all, more than often, the content of the archives are named with irrelevant names. We know that our torrents have the good naming since Flexget downloaded it, so we have a script to rename the extracted videos with the torrent name. We could enhance it to place the subtitles with the good naming, but this was never achieved.

The script needs improvement, but it works for me on almost every situations.

This script produce logs that will be useful to keep track of the downloaded torrent: Monitor your downloads with a PHP webpage


nano /home/user01/.config/deluge/scripts/extractor.sh

#!/bin/bash # ------------ SET THIS VARIABLES ---------- # SET THE FOLDER TO EXTRACT THE CONTENT TO extractFolder=/home/user/data/extracted # SET THE GLOBAL LOG FILE syslog=$extractFolder/unrarzip_sys.log # SET THE TVSHOWS FOLDER SEARCH WE DON'T TO EXTRACT THE TVSHOWS # THE SAME WE EXTRACT MOVIES tvshowFolder=/tvshows/ # RENAMER SCRIPT PATH renamerScript=/home/user01/.config/deluge/scripts/renamer.sh # ---------------------- # SET THE PROCESS AND HARD DRIVE PRIORITIES VERY LOW # THIS WILL AVOID LAG WHILE PLAYING VIDEOS sudo renice 19 $$ ionice -c3 -p$$ #LOGING THE DELUGE PARAMATERS # $1 is the Torrent ID # $2 is the folder where the torrent was saved # $3 is the torrent name and the folder name where the torrent is saved echo Params are: >> "$syslog" echo -1: "$1" >> "$syslog" echo -2: "$2" >> "$syslog" echo -3: "$3" >> "$syslog" #SET THE TORRENT FILES DIR dir="$3/$2" extracted="" renamerCommand="" #SET THE DATE : WHEN THE DOWNLOAD WAS COMPLETED echo "INFO: |date: `date '+%Y-%m-%d'`|Download:$2" >> "$syslog" # IF THE TORRENT FILES ARE IN A FOLDER IT IS # BECAUSE THERE IS MORE THAN 1 FILES IN THE TORRENT if [[ -d $dir ]]; then # WE LOG THAT IS A MULTI FILE TORRENT AND THE DIR LOCATION echo "is DIR: $dir" >> "$syslog" # CHANGE AND LOG THE CURRENT DIR cd "$3/$2" echo "cd "`dirs` >> "$syslog" # WE CHECK IF THE PATH AS /tvshows/ IN IT if `echo ${dir} | grep $tvshowFolder 1>/dev/null 2>&1` then # CHANGE THE MODE TO TVSHOW AND LOG IT isTvShow=True echo "isTVSHOW" >> "$syslog" fi # LOOP ALL FILES IN TORRENT FOLDER find ./ | # $i BECOMES OUR FILENAME while read i; do # GET CURRENT FILE EXTENTION fileExt="${i##*.}" #CHECK IF RAR OR ZIP if [ "$fileExt" == "rar" ] || [ "$fileExt" == "zip" ] ; then if [[ $i == *.part*.rar ]] ; then # IT IS A MULTI PART RAR if [[ $i == *.part1.rar ]] || [[ $i == *.part01.rar ]] || [[ $i == *.part001.rar ]]; then # WE WANT TO EXTRACT JUST THE part1 if [ "$isTvShow" == True ]; then # IF IN TVSHOWS MODE, DON'T CHANGE DIR newDir=$dir else # ELSE CHANGE FOLDER TO THE EXTRACT FOLDER AND CREATE # A FOLDER WITH THE TORRENT NAME / LOG IT newDir=$extractFolder/$2 mkdir -p "$newDir" >> "$syslog" echo "mkdir -p $newDir" >> "$syslog" fi # CREATE A NEW LOG INSIDE THE EXTRACTED DIR log=$newDir/unrarzip.log # LOG THE DESTINATION PATH echo "destination path: $newDir" >> "$syslog" # EXTRACT part1.rar or part01.rar or part001.rar AND LOG THE OUPUT TO THE NEW LOG unrar x -y -p- -o- "$dir/$i" "$newDir" >> "$log" echo "unrar >> $dir/$i
" >> "$syslog" # LAUNCH THE RENAMER source $renamerScript fi else # NOT A MULTI PART RAR if [ "$isTvShow" == True ]; then # IF IN TVSHOWS MODE, DON'T CHANGE DIR newDir=$dir else # ELSE CHANGE FOLDER TO THE EXTRACT FOLDER AND CREATE # A FOLDER WITH THE TORRENT NAME / LOG IT newDir=$extractFolder/$2 mkdir -p "$newDir" >> "$syslog" echo "mkdir -p $newDir" >> "$syslog" fi # CREATE A NEW LOG INSIDE THE EXTRACTED DIR log=$newDir/unrarzip.log # LOG THE DESTINATION PATH echo "destination path: $newDir" >> "$syslog" if [ "$fileExt" == "rar" ]; then # IS RAR -> EXTRACT AND LOG extracted="unrar x -y -p- -o+ $dir/$i $newDir" unrar x -y -p- -o- "$dir/$i" "$newDir" >> "$log" echo "unrar >> $dir/$i" >> "$syslog" # LAUNCH THE RENAMER source $renamerScript elif ["$fileExt" == "zip" ]; then # IS ZIP -> EXTRACT AND LOG extracted="unzip -o $dir/$i -d $newDir" unzip -o "$dir/$i" -d "$newDir" >> "$log" echo "unzip >> $dir/$i" >> "$syslog" # LAUNCH THE RENAMER source $renamerScript fi fi fi done elif [[ -f $dir ]]; then # SINGLE FILE TORRENT # GET FILENAME AND EXTENTION fileExt="${2##*.}" echo "is FILE: $dir" >> "$syslog" echo "EXT is : $fileExt" >> "$syslog" #IF RAR OR ZIP if [ "$fileExt" == "rar" ] || [ "$fileExt" == "zip" ] ; then # CREATE A NEW DIR newDir=$extractFolder/"${2:2:${#2}-6}" #CREATE A NEW LOG FILE log=$newDir/unrarzip.log # CREATE A NEW FOLDER INSIDE THE EXTRACTED FOLDER mkdir -p "$newDir" >> "$syslog" echo "mkdir -p $newDir" >> "$syslog" if [ "$fileExt" == "rar" ]; then # IF RAR EXTRACT unrar x -y -p- -o+ $dir "$newDir" >> "$log" extracted="unrar x -y -p- -o+ $dir $newDir" echo "unrar >> $dir" >> "$syslog" elif ["$fileExt" == "zip" ]; then # IF ZIP EXTRACT extracted="unzip -o $dir -d $newDir" unzip -o "$dir" -d "$newDir" >> "$log" echo "unzip >> $dir" >> "$syslog" fi else # SINGLE FILE, not ZIP OR RAR echo "$1 Not a rar or zip!" >> "$syslog" fi RETVAL=$? else # ERROR echo "$dir is not valid" #exit 1 fi # FINISHING THE LOG echo ---------------------- >> "$syslog"


nano /home/user01/.config/deluge/scripts/renamer.sh

# CHANGE THE CURRENT FOLDER TO THE EXTRACTED FOLDER cd $newDir find ./ | # CHECK ALL EXTRACTED FILES while read x; do # GET EXTENTION fileExt="${x##*.}" # IF VIDEO if [ "$fileExt" == "mkv" ] || [ "$fileExt" == "avi" ] || [ "$fileExt" == "mpg" ] || [ "$fileExt" == "mp4" ] ; then #RENAME ONLY MOVIES, SO WE CHECK IF WE ARE IN THE EXTRACTED FOLDER if [ "$newDir" == "$extractFolder/$2" ] ; then #RENAME VIDEO TO TORRENT NAME AND LOG IT - WE WISH THERE IS ONLY ONE VIDEO IN THIS FOLDER echo RENAMING FROM $newDir/$x >> "$syslog" echo TO $newDir/$2.$fileExt >> "$syslog" mv $newDir/$x $newDir/$2.$fileExt >> "$syslog" fi fi done

Install the script in deluge:

  1. Add the Execute plugin:execute
  2. Add the script to the complete command:extractor