Improve handling of associated/extra files

Sonarr version: 2.0.0.4949
Mono version: 4.6.2
OS: DSM 6.1.3-15152 Update 8 (Synology DS216j)
Description:
I have Download Station configured as download client, everything regarding downloading of torrents works great. However when it comes to Completed Download Handling and Importing there are a few issues.

  1. Specified extra file extensions are not copied
    If the name of the extra file does not match the video file (e.g. The.Big.Bang.Theory.S11E01.720p.HDTV.X264-DIMENSION.mkv <> the.big.bang.theory.1101.720p-dimension.nfo) it is not moved.
    Also when a download contains extra files in sub-directories (e.g Subs) it does not copy (all) the desired extra file extensions to target folder.
    I tried specifying nfo,srt,Subs,subs and * as Extra File Extensions, both did not copy extra files within sub-directories.

  2. Optional rename .nfo to .nfo-orig
    Renaming of .nfo to .nfo-orig completely makes sense if you have Metadata generation enabled within Sonarr. However personally I use Plex Media Center, which fetches metadata and stores it elsewhere, instead of alongside with media.
    In this case I would like to move .nfo without renaming, or at least allow the option to specify the suffix (e.g. -original, for the OCD among us).

  3. Delete only empty download directories
    Upon Download Station finishes the download it will (hopefully) import all the specified files, despite it being successful it will always remove/delete the download directory even if the folder still contains any associated/extra files.
    It would be great if this could be limited to only delete empty download directories or at least be able to specify/respect Recycling Bin directory where to delete/move download directories.

  4. When episodes are upgraded to a better quality extra files remain
    Prior moved extra/associated files are not deleted when episode is upgraded, this needs to be manually removed.
    It is also not possible to remove them with custom script, as sonarr_deletedrelativepaths and sonarr_deletedpaths environment variables only contains video files.

    It seems this does work, see item 7 though regarding additional/extra files being removed on upgrade.

  5. Group sc-media needs to have read+write access to downloads folder.
    There is currently no way for an user to post process the downloads folder without write access.
    Unlike Blackhole download clients, allowing you to specify a “read-only” option in case sc-media does not have write access.

  6. Misleading configuration label
    Looking at Importing section in Media Management settings, there is a option “Use Hardlinks instead of Copy”, however as far I’m aware Sonarr does not Copy, with the exception of read-only Blackholes, but always Move the files, if anything this label should be “Use Hardlinks instead of Move”.

  7. Extra files environment variable
    Only video file is passed as environment variable, there is no way of which other associated/extra files where moved/removed upon download/upgrade in custom script post processing.

  1. This is expected, no plans to alter this.
  2. No plans to support this, at least at this time.
  3. A custom script could be used to preserve that folder, no plans to make it customizable in Sonarr at this time.
  4. Assuming torrents, for download station, Sonarr always moves the file because the file itself is a hardlink of the file seeding in the download client. For usenet clients, Sonarr always moves.
  5. See 5.
  6. Filed a GHI for this: https://github.com/Sonarr/Sonarr/issues/2239

In regard to the misleading configuration label (item 6.) shouldn’t this then be renamed to “Use Hardlinks instead of Move” as you mentioned (in item 5.) Sonarr always Moves.
Or something else more descriptive, perhaps with a tooltip, explaining when it actually would Copy (e.g. torrent seeding or read-only blackhole configuration) to make clear that Sonarr always Moves.

No, it’s hardlink instead of copy, that is correct. Most torrent clients are treated as read-only when seeding so Sonarr will copy or hardlink (depending on Sonarr’s config and the system config, since hardlink must be on the same volume).

Sonarr always moves for Download Station clients because it’s an odd one that does it’s own internal hardlinking and when telling DS to remove a download it doesn’t remove the hardlinked copy (AFAIK), so you’d end up with a file sitting there until manually removed.

I’m playing around with a custom script as suggested, I noticed that on upgrade the video file and subtitle (<episode>.en.srt) are moved accordingly to the specified “Recylce Bin” configured in settings.
Despite the subtitle being downloaded after import process, using Sub-Zero in Plex Media Server.

However in my custom (post-process) script, when I move the original NFO file into target folder and name it as <episode>.nfo it does not get moved to “Recycle Bin” on quality upgrade.
Funny thing is that if I move the original NFO file into target folder and name it as <episode>.nfo-orig it is moved to the configured “Recycle Bin” alongside with the video file and subtitle.

For whoever is looking to keep non-imported files upon download completion.
I’m posting my custom script which moves non-imported files to a ‘processed’ folder which is by default path of download source folder name with ‘.processed’ suffix.
It is possible to specify target processed directory as script argument, which will create a new folder with same name as download source folder name in the processed directory.

I recommend having this setup without “Import Extra Files” enabled and copy any files from ‘processed’ folder accordingly to episode folder as not all extra files are properly moved to “Recycle Bin” upon upgrade.

#!/usr/bin/env python
import os
import shutil
import sys

# https://github.com/Sonarr/Sonarr/blob/0ec64e704368206cb91619858b06be26bc2b1cd1/src/NzbDrone.Core/MediaFiles/MediaFileExtensions.cs
media_file_extensions = ('.webm','.m4v','.3gp','.nsv','.ty','.strm','.rm','.rmvb','.m3u','.ifo','.mov','.qt','.divx','.xvid','.bivx','.nrg','.pva','.wmv','.asf','.asx','.ogm','.ogv','.m2v','.avi','.bin','.dat','.dvr-ms','.mpg','.mpeg','.mp4','.avc','.vp3','.svq3','.nuv','.viv','.dv','.fli','.flv','.wpl','.img','.iso','.vob','.mkv','.ts','.wtv','.m2ts')

source_folder = os.getenv('sonarr_episodefile_sourcefolder', '')
if not os.path.isdir(source_folder):
    print('[POST-PROCESS] Source/download directory does not exist')
    sys.exit()

if not os.listdir(source_folder):
    print('[POST-PROCESS] Source/download directory is empty')
    sys.exit()

# Do not move source folder as it can contain other video files that still need to be imported,
# instead create a new processed folder and move non-imported files instead of deleting them.
# By default processed folder is path of download source folder name with '.processed' suffix.
# It is possible to specify target processed directory as script argument, which will create
# a new folder with same name as download source folder name in the processed directory.
source_processed_folder = source_folder + '.processed'
if len(sys.argv) > 1 and os.path.isdir(os.path.abspath(sys.argv[1])):
    source_processed_folder = os.path.join(os.path.abspath(sys.argv[1]), os.path.basename(source_folder))

print('[POST-PROCESS] Scanning source/download directory for non-imported files')
for item in os.listdir(source_folder):
    item_source_path = os.path.join(source_folder, item)
    item_destination_path = os.path.join(source_processed_folder, item)

    if os.path.isdir(item_source_path) or not item.endswith(media_file_extensions):
        print('[POST-PROCESS] Moving: ' + item)
        if not os.path.exists(source_processed_folder):
            os.makedirs(source_processed_folder)

        if os.path.exists(item_destination_path):
            try:
                shutil.rmtree(item_destination_path)
            except:
                os.unlink(item_destination_path)

        shutil.move(item_source_path, item_destination_path)

print('[POST-PROCESS] Done!')

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.