Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fryfrog
Last active March 29, 2023 17:21
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fryfrog/d1f38b805a270a92bff085536dcfd7e2 to your computer and use it in GitHub Desktop.
Save fryfrog/d1f38b805a270a92bff085536dcfd7e2 to your computer and use it in GitHub Desktop.
A Radarr post processing script to remove the video file from packed torrents, modified version of subzero79/87a347a07964390884c9
#!/bin/bash
# Examples for testing
# radarr_moviefile_sourcefolder="/data/torrent/movies/Justice.League.2017.1080p.HDRip.X264.AAC-m2g" radarr_moviefile_sourcepath="/data/torrent/movies/Justice.League.2017.1080p.HDRip.X264.AAC-m2g/Justice.League.2017.1080p.HDRip.X264.AAC-m2g.mkv"
# Instructions
# Put this script somewhere on your file system like /usr/local/bin and make it executable.
#
# In Radarr, Settings -> Connect add a Custom Script
# On Grab: No
# On Download: Yes
# On Upgrade: Yes
# On Rename: No
# Path: /path/to/where/script/is/radarr_cleanup_packed_torrent.sh
# Arguments:
# Tune values below to protect your torrents w/ small rar files or non-torrent download client.
# In *bytes*, the biggest rar file size limit to prevent video deletion from torrents with unrelated rar files (like subs)
# 25 * 1024 * 1024
rar_min_size=26214400
# Seconds to wait between size checks for in progress unpack
unpack_time=5
# The final base directory torrents end up in, for example "movies" from /data/torrents/movies
radarr_final_dir="movies"
# Identifiable portion of path to torrents, so it will only run on torrents.
# For example, with a path of "/data/torrents/movies", "torrents" is a good choice.
torrent_path_portion="torrents"
# Test that this is a download event, so we don't run on grab or rename.
# shellcheck disable=SC2154
if [[ "${radarr_eventtype}" != "Download" ]]; then
echo "[Torrent Cleanup] Sonarr Event Type is NOT Download, exiting."
exit
fi
# Test this file exists, no point running on a file that isn't there.
# shellcheck disable=SC2154
if ! [[ -f "${radarr_moviefile_sourcepath}" ]]; then
echo "[Torrent Cleanup] File ${radarr_moviefile_sourcepath} does not exist, exiting."
exit
fi
# Test that this is a torrent, so we don't run on usenet downloads.
# shellcheck disable=SC2154
if ! [[ "${radarr_moviefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
echo "[Torrent Cleanup] Path ${radarr_moviefile_sourcepath} does not contain \"torrent\", exiting."
exit
fi
# Test that this is a multi-file torrent, so we don't run on single file torrents.
# shellcheck disable=SC2154
base_dir=$( basename "${radarr_moviefile_sourcefolder}" )
if [ "${base_dir}" == "${radarr_final_dir}" ]; then
echo "[Torrent Cleanup] Single file torrent, exiting."
exit
fi
# We might run while the unpack is still happening, so wait for that before removing.
echo "[Torrent Cleanup] Starting wait for ${radarr_moviefile_sourcepath} unpacking..."
file_size_start=$( stat --printf="%s" "${radarr_moviefile_sourcepath}" )
sleep ${unpack_time}
file_size_end=$( stat --printf="%s" "${radarr_moviefile_sourcepath}" )
until [[ ${file_size_start} -eq ${file_size_end} ]]; do
file_size_start=$( stat --printf="%s" "${radarr_moviefile_sourcepath}" )
sleep ${unpack_time}
file_size_end=$( stat --printf="%s" "${radarr_moviefile_sourcepath}" )
done
echo "[Torrent Cleanup] Finished wait for ${radarr_moviefile_sourcepath} unpacking..."
# Test for rar and r## files and check the *size* of the biggest one so we don't run due to packed subs or something.
# shellcheck disable=SC2154
if find "${radarr_moviefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
# shellcheck disable=SC2154
rar_size="$( find "${radarr_moviefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -nk 7 | tail -1 | awk '{ print $7 }' )"
if [[ ${rar_size} -gt ${rar_min_size} ]]; then
echo "[Torrent Cleanup] Rar file size ${rar_size} exceeds minimum of ${rar_min_size}, deleting video file."
rm "${radarr_moviefile_sourcepath}"
else
echo "[Torrent Cleanup] Rar file size ${rar_size} DOES NOT MEET minimum of ${rar_min_size}, skipping deletion of video file."
fi
else
echo "[Torrent Cleanup] No rar files, exiting."
fi
@3rdStng
Copy link

3rdStng commented May 6, 2022

Been loving this script for many months now, but I'm thinking it needs a little tweaking that is above my scripting knowledge. If a non-RAR file is downloaded and not placed into a subdirectory, this script purges the video file. Resulting in errors in my seeds. It's a simple fix. Copy the file back, rename it, and force recheck it. But it's a little time consuming. Would it be possible to add some check that both the RAR and video file need to exist before processing? Or a way to skip if only a video file that was not placed into a sub directory? Sonarr has the same problem from time to time too.

@fryfrog
Copy link
Author

fryfrog commented May 6, 2022

That is what line 56 is trying to do, it should be causing it to not run on torrents not in a folder. Torrents that are single file. Do you have your torrent client setup to use the original torrent format or does it force single file torrents to not be in folders?

But really, you should move to unpackerr, it does everything correctly. Including removing the video file after import.

@3rdStng
Copy link

3rdStng commented May 6, 2022

I saw that line after I posted. Completely missed it the first pass. I'm running deluge and to my knowledge I don't have anything special set. It does retain the same format. The only forcing may be due to the "extractor" plugin, which is set to "create torrent name sub-folder". But without this, radarr or sonarr won't import/move the file into place. Thinking a little more, maybe the extractor plugin is the issue and not this script. Although the script is the only piece that I know would delete a file.

I will have to look into unpackerr. I mentally can't wrap my mind around how it will work yet as I know the other xxxarr dockers monitor their respective download location.

EDIT: So am I correct in that I just need to disable the extractor plugin within Deluge and have unpackerr do the monitoring?

@fryfrog
Copy link
Author

fryfrog commented May 6, 2022

Yeah, if you switch to unpackerr you would disable this cleanup script and disable any unpacking plugin in Deluge.

@3rdStng
Copy link

3rdStng commented May 6, 2022

It's running through my entire /downloads/ directory and unpacking everything. Is that normal for the first use? It's been less than 5 minutes, which is what the default delete delay appears to be. But did I just create a mess I need to clean up for everything that was existing?

@fryfrog
Copy link
Author

fryfrog commented May 6, 2022

It should only unpack things that show up in Activity in sonarr/radarr if you set it up right. Did you point it at sonarr/radarr? Or did you point it at a folder? Also, switch to unpackerr's Discord. :)

@3rdStng
Copy link

3rdStng commented May 6, 2022

Thanks for the help and sorry to get off topic. I think I figured out the issue. Sonarr/Radarr had a hiccup on some imports and re-queued things that had already been done and processed. Being back in the queue, unpackerr picked them up. Cleaning up my queues now. Thanks again for the pointer to this docker/app.

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