Skip to content

Instantly share code, notes, and snippets.

@fryfrog
Last active April 13, 2024 11:06
Show Gist options
  • Select an option

  • Save fryfrog/94716e7e27ba38dff57c7631d9f58bed to your computer and use it in GitHub Desktop.

Select an option

Save fryfrog/94716e7e27ba38dff57c7631d9f58bed to your computer and use it in GitHub Desktop.

Revisions

  1. fryfrog revised this gist Apr 7, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,13 @@ if [[ "${sonarr_eventtype}" != "Download" ]]; then
    exit
    fi

    # Test this file exists, no point running on a file that isn't there.
    # shellcheck disable=SC2154
    if ! [[ -f "${sonarr_episodefile_sourcepath}" ]]; then
    echo "[Torrent Cleanup] File ${sonarr_episodefile_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 ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
  2. fryfrog revised this gist Aug 19, 2019. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,9 @@
    # 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 "tv" from /data/torrents/tv
    sonarr_final_dir="tv"

    @@ -49,6 +52,18 @@ if [[ "${base_dir}" == "${sonarr_final_dir}" ]]; then
    exit
    fi

    # We might run while the unpack is still happening, so wait for that before removing.
    echo "[Torrent Cleanup] Starting wait for ${sonarr_episodefile_sourcepath} unpacking..."
    file_size_start=$( stat --printf="%s" "${sonarr_episodefile_sourcepath}" )
    sleep ${unpack_time}
    file_size_end=$( stat --printf="%s" "${sonarr_episodefile_sourcepath}" )
    until [[ ${file_size_start} -eq ${file_size_end} ]]; do
    file_size_start=$( stat --printf="%s" "${sonarr_episodefile_sourcepath}" )
    sleep ${unpack_time}
    file_size_end=$( stat --printf="%s" "${sonarr_episodefile_sourcepath}" )
    done
    echo "[Torrent Cleanup] Finished wait for ${sonarr_episodefile_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 "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
  3. fryfrog revised this gist Aug 14, 2019. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,19 @@
    # Examples for testing
    # sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    # Instructions
    # Put this script somewhere on your file system like /usr/local/bin and make it executable.
    #
    # In Sonarr, Settings -> Connect add a Custom Script
    # On Grab: No
    # On Download: Yes
    # On Upgrade: Yes
    # On Rename: No
    # Path: /path/to/where/script/is/sonarr_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
    @@ -49,4 +62,4 @@ if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$
    fi
    else
    echo "[Torrent Cleanup] No rar files, exiting."
    fi
    fi
  4. fryfrog revised this gist Jul 29, 2019. No changes.
  5. fryfrog revised this gist Jul 29, 2019. No changes.
  6. fryfrog revised this gist Jun 21, 2019. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -14,21 +14,32 @@ sonarr_final_dir="tv"
    # For example, a path of "/data/torrents/tv", "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 [[ "${sonarr_eventtype}" != "Download" ]]; then
    echo "[Torrent Cleanup] Sonarr Event Type is NOT Download, exiting."
    exit
    fi

    # Test that this is a torrent, so we don't run on usenet downloads.
    # shellcheck disable=SC2154
    if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_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 "${sonarr_episodefile_sourcefolder}" )
    if [[ "${base_dir}" == "${sonarr_final_dir}" ]]; then
    echo "[Torrent Cleanup] Single file torrent, exiting."
    exit
    fi

    # 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 "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    # shellcheck disable=SC2154
    rar_size="$( find "${sonarr_episodefile_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."
  7. fryfrog revised this gist Jun 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ fi

    # shellcheck disable=SC2154
    if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    rar_size="$( find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | tail -1 | awk '{ print $7 }' )"
    rar_size="$( find "${sonarr_episodefile_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 "${sonarr_episodefile_sourcepath}"
  8. fryfrog revised this gist Jun 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ fi

    # shellcheck disable=SC2154
    if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    rar_size="$( find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | head -1 | awk '{ print $7 }' )"
    rar_size="$( find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 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 "${sonarr_episodefile_sourcepath}"
  9. fryfrog revised this gist Jun 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ fi
    # shellcheck disable=SC2154
    if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    rar_size="$( find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | head -1 | awk '{ print $7 }' )"
    if [[ ${rar_size} > ${rar_min_size} ]]; then
    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 "${sonarr_episodefile_sourcepath}"
    else
  10. fryfrog revised this gist Jun 21, 2019. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -14,20 +14,23 @@ sonarr_final_dir="tv"
    # For example, a path of "/data/torrents/tv", "torrents" is a good choice.
    torrent_path_portion="torrents"

    # shellcheck disable=SC2154
    if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    # shellcheck disable=SC2154
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )
    if [[ "${base_dir}" == "${sonarr_final_dir}" ]]; then
    echo "[Torrent Cleanup] Single file torrent, exiting."
    exit
    fi

    # shellcheck disable=SC2154
    if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    rar_size="$( find . -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | head -1 | awk '{ print $7 }' )"
    if [[ rar_size > rar_min_size ]]; then
    rar_size="$( find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | head -1 | awk '{ print $7 }' )"
    if [[ ${rar_size} > ${rar_min_size} ]]; then
    echo "[Torrent Cleanup] Rar file size ${rar_size} exceeds minimum of ${rar_min_size}, deleting video file."
    rm "${sonarr_episodefile_sourcepath}"
    else
  11. fryfrog revised this gist Jun 21, 2019. 1 changed file with 13 additions and 5 deletions.
    18 changes: 13 additions & 5 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    #!/bin/bash
    # Setup in Sonarr: Settings -> Connect -> Add Custom Script, enable Download and Upgrade (or Import and Upgrade in v3)

    # Examples for testing
    # sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    # 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

    # The final base directory torrents end up in, for example "tv" from /data/torrents/tv
    sonarr_final_dir="tv"

    @@ -12,7 +15,7 @@ sonarr_final_dir="tv"
    torrent_path_portion="torrents"

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain ${torrent_path_portion}, exiting."
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    @@ -22,9 +25,14 @@ if [[ "${base_dir}" == "${sonarr_final_dir}" ]]; then
    exit
    fi

    if find "${sonarr_episodefile_sourcefolder}" -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    echo "[Torrent Cleanup] Found rar or r00 files, deleting video file."
    rm "${sonarr_episodefile_sourcepath}"
    if find "${sonarr_episodefile_sourcefolder}" -type f -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    rar_size="$( find . -type f -iregex '.*\.r[0-9a][0-9r]$' -ls | sort -k 7 | head -1 | awk '{ print $7 }' )"
    if [[ rar_size > rar_min_size ]]; then
    echo "[Torrent Cleanup] Rar file size ${rar_size} exceeds minimum of ${rar_min_size}, deleting video file."
    rm "${sonarr_episodefile_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
  12. fryfrog revised this gist Nov 13, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/bin/bash
    # Setup in Sonarr: Settings -> Connect -> Add Custom Script, enable Download and Upgrade (or Import and Upgrade in v3)

    # Examples for testing
    # sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"
    @@ -11,7 +12,7 @@ sonarr_final_dir="tv"
    torrent_path_portion="torrents"

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain ${torrent_path_portion}, exiting."
    exit
    fi

  13. fryfrog revised this gist Jun 18, 2018. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,20 @@
    # Examples for testing
    # sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    sonarr_label="tv"
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )
    # The final base directory torrents end up in, for example "tv" from /data/torrents/tv
    sonarr_final_dir="tv"

    # Identifiable portion of path to torrents, so it will only run on torrents.
    # For example, a path of "/data/torrents/tv", "torrents" is a good choice.
    torrent_path_portion="torrents"

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ torrent ]]; then
    if ! [[ "${sonarr_episodefile_sourcepath}" =~ ${torrent_path_portion} ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    if [[ "${base_dir}" == "${sonarr_label}" ]]; then
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )
    if [[ "${base_dir}" == "${sonarr_final_dir}" ]]; then
    echo "[Torrent Cleanup] Single file torrent, exiting."
    exit
    fi
  14. fryfrog revised this gist Jun 18, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ if ! [[ "${sonarr_episodefile_sourcepath}" =~ torrent ]]; then
    exit
    fi

    if [ "${base_dir}" == "${sonarr_label}" ]; then
    if [[ "${base_dir}" == "${sonarr_label}" ]]; then
    echo "[Torrent Cleanup] Single file torrent, exiting."
    exit
    fi
  15. fryfrog revised this gist Jun 13, 2018. 2 changed files with 24 additions and 28 deletions.
    28 changes: 0 additions & 28 deletions sonarr unpacked torrent cleanup script
    Original file line number Diff line number Diff line change
    @@ -1,28 +0,0 @@
    #!/bin/bash

    # Examples for testing
    #sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    sonarr_label="tv"
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ torrent ]]; then
    echo "Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    if [ "${base_dir}" == "${sonarr_label}" ]; then
    echo "Single file torrent, exiting."
    exit
    fi

    find "${sonarr_episodefile_sourcefolder}" -regex '.*\.r[0-9][0-9]$' &>/dev/null
    case $? in
    0)
    echo "Rar files present, deleting video file."
    rm -rv "${sonarr_episodefile_sourcepath}"
    ;;
    *)
    echo "No rar files, exiting."
    ;;
    esac
    24 changes: 24 additions & 0 deletions sonarr_cleanup_packed_torrent.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/bin/bash

    # Examples for testing
    # sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    sonarr_label="tv"
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ torrent ]]; then
    echo "[Torrent Cleanup] Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    if [ "${base_dir}" == "${sonarr_label}" ]; then
    echo "[Torrent Cleanup] Single file torrent, exiting."
    exit
    fi

    if find "${sonarr_episodefile_sourcefolder}" -iregex '.*\.r[0-9a][0-9r]$' | grep -Eq '.*'; then
    echo "[Torrent Cleanup] Found rar or r00 files, deleting video file."
    rm "${sonarr_episodefile_sourcepath}"
    else
    echo "[Torrent Cleanup] No rar files, exiting."
    fi
  16. fryfrog renamed this gist May 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. fryfrog revised this gist May 11, 2018. No changes.
  18. fryfrog created this gist May 11, 2018.
    28 changes: 28 additions & 0 deletions Clean source
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/bin/bash

    # Examples for testing
    #sonarr_episodefile_sourcefolder="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD" sonarr_episodefile_sourcepath="/data/torrent/tv/Penny.Dreadful.S01E01.720p.HDTV.x264-2HD/penny.dreadful.s01e01.720p.hdtv.x264-2hd.mkv"

    sonarr_label="tv"
    base_dir=$( basename "${sonarr_episodefile_sourcefolder}" )

    if ! [[ "${sonarr_episodefile_sourcepath}" =~ torrent ]]; then
    echo "Path ${sonarr_episodefile_sourcepath} does not contain \"torrent\", exiting."
    exit
    fi

    if [ "${base_dir}" == "${sonarr_label}" ]; then
    echo "Single file torrent, exiting."
    exit
    fi

    find "${sonarr_episodefile_sourcefolder}" -regex '.*\.r[0-9][0-9]$' &>/dev/null
    case $? in
    0)
    echo "Rar files present, deleting video file."
    rm -rv "${sonarr_episodefile_sourcepath}"
    ;;
    *)
    echo "No rar files, exiting."
    ;;
    esac