Skip to content

Instantly share code, notes, and snippets.

@mylogon341
Last active March 8, 2024 10:23
Show Gist options
  • Save mylogon341/e74d25cdec7c2ebe56a5f13163e1a099 to your computer and use it in GitHub Desktop.
Save mylogon341/e74d25cdec7c2ebe56a5f13163e1a099 to your computer and use it in GitHub Desktop.
qBittorrent RARBG subtitle import
#!/bin/bash
root=$1
echo $root
function copy_from_directory() {
video_name=$1
directory=$2
for file in $directory/*; do
subtitle=$(basename "$file")
cp $file "$root/${video_name}_$subtitle"
done
}
function handle_film() {
# just gets the first path film=${videos[0]}
film=$1
echo "film param is is ${film}"
# gets the name of the file
file_name=$(basename "$film")
# name of the file, less the extension
film_name=${file_name%.*}
subtitle_dir="${root}/Subs"
copy_from_directory $film_name $subtitle_dir
}
function handle_tv() {
episode_name_arr=("$@")
for name in "${episode_name_arr[@]}"; do
# gives filename without relative path
file_name=$(basename "$name")
# gives filename without extension. subtitle directories have this exact name.
sans_extension="${file_name%.*}"
subtitle_dir=$root/Subs/$sans_extension
copy_from_directory $sans_extension $subtitle_dir
done
}
if [[ "$root" != *"RARBG"* ]]; then
echo "Not a rarbg directory. Skipping."
exit 0
fi
if [[ ! -d "$root/Subs" ]]; then
echo "No subtitles directory. Skipping"
exit 0
fi
# finds video files
videos=( $(find "$root" -maxdepth 1 -type f | grep -E "\.mp4$|\.mkv$|\.mov$") )
subtitles_directories=($( ls -d ${root}/Subs/*/ ))
directories_count=${#subtitles_directories[@]}
if test $directories_count -eq 0; then
echo "there is no subdirectories. assume its a film"
handle_film ${videos[0]}
else
echo "there are $directories_count subdirectories. assume tv show."
handle_tv "${videos[@]}"
fi
copied=($( ls $root/*.srt ))
echo "Copied ${#copied[@]} subtitles to the root directory for ${directories_count} shows/film"
@mylogon341
Copy link
Author

Make sure to add the import .srt setting in Sonarr as well as Radarr

@mylogon341
Copy link
Author

In my instance I have it so it seeds for x hours and then pauses. Once it's done, I have the media management in sonarr/ radarr remove it from qBittorrent, which should then delete the directory. However, the above script causes the root directory to stick around with all the copied subtitles files. This is just not tidy.

To resolve issue of directories not deleting properly from torrent complete dir, you will need radarr/ sonarr to clean up after themselves.
I have added a "Custom Script" in the "Connect" submenu. The trigger is setup as follows
Screenshot 2022-07-05 at 11 46 17 am

I created that bash file in the docker config with the contents

#!/bin/bash

if [[ $radarr_moviefile_sourcefolder == "" ]]; then 
    exit 0
fi

rm $radarr_moviefile_sourcefolder/*RARBG*.srt

and

#!/bin/bash

if [[ $sonarr_episodefile_sourcefolder == "" ]]; then 
    exit 0
fi

rm $sonarr_episodefile_sourcefolder/*RARBG*.srt

(The if checks are so that the scripts will save. Otherwise it will error when trying to save in both systems)

The last thing to do to make sure it saves/ runs properly is to run
chmod +x import_clear.sh
to make the script executable. This should then save correctly in radarr/sonarr.

Once a film/show has been imported with it's subtitles, it will then clear all the .srt files the qBittorrent script created. This will then allow qBittorrent to successfully delete the directory once it has met the seed criteria.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment