Skip to content

docs: document Import Using Script for Radarr and Sonarr - #477

Open
6801318d8d wants to merge 1 commit into
Servarr:masterfrom
6801318d8d:docs-import-using-script
Open

6801318d8d wants to merge 1 commit into
Servarr:masterfrom
6801318d8d:docs-import-using-script

Conversation

@6801318d8d

Copy link
Copy Markdown

"Import Using Script" (Settings > Media Management > Importing) is documented with one line in Sonarr's page and not at all in Radarr's, so writing a script means reading ScriptImportDecider.cs. This adds what the script receives and what it can answer, from the Radarr and Sonarr sources:

  • the two arguments (source and destination path) and the environment variables, with a warning that their names arrive lowercase (radarr_sourcepath, not Radarr_SourcePath as spelled in the source: they are passed through a .NET StringDictionary, which lowercases keys). I lost some time on this: a hand test with the source spelling worked, the real import failed with "unbound variable";
  • the lines the script can print ([MoveStatus] MoveComplete|RenameRequested|DeferMove, [MediaFile], [ExtraFile], [PreventExtraImport]) and that printing nothing means MoveComplete;
  • that a non-zero exit code fails the import.

Checked against ScriptImportDecider.cs and UpgradeMediaFileService.cs of both apps (current develop) and tested with Radarr 6.4.4 and Sonarr 4.0.20. The pre-commit hooks pass on both files.

@bakerboy448 bakerboy448 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this against ScriptImportDecider.cs, UpgradeMediaFileService.cs, the *FileMovingService.cs transfer path, ImportApproved*.cs and ProcessProvider.StartAndCapture on Radarr develop and Sonarr main/develop. Most of it is accurate: the arguments, the env var list, the output tokens, the exit-code check, the RenameRequested media info re-read, the | separator, the season pack note, and the StringDictionary explanation.

A few fixes, all small:

  1. A manual import of a file already inside the movie/series folder skips the script, unless it comes from a queue item.
  2. HardLink is never passed as the transfer mode.
  3. On upgrades the old files are removed first (Recycling Bin if set), so a failing script leaves no file.
  4. radarr_moviefile_id is always 0.
  5. Stderr is parsed for the [...] tokens too.
  6. [MediaFile] has to be inside the movie/series folder.
  7. A failed script shows only "Failed to import" in the queue; the exit code is in the logs.
  8. Nit: the lowercase note can be one line under Environment variables instead of a warning under Exit code.

Suggestions inline. Frontmatter date doesn't need a bump; recent PRs to these pages (#455, #466) left it alone.

Verdict: Comment. Good to merge once those are in.

Comment thread radarr/settings.md

### Import Using Script

Radarr runs the script once per imported movie file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A manual import of a file that is already inside the movie folder is passed with newDownload = false and never reaches the script (ManualImportService.cs:419,470, ImportApprovedMovie.cs:131-149). The exception is an import from a queue item, which sets newDownload = true (:474).

Suggested change
Radarr runs the script once per imported movie file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself.
Radarr runs the script once per imported movie file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself. A manual import of a file that is already inside the movie folder does not run the script (unless it is imported from a queue item).

Comment thread sonarr/settings.md

### Import Using Script

Sonarr runs the script once per imported episode file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself. The source paths of all the files of a download are worked out before the first call, so a script must not move the other files of a season pack (for example by renaming the download's folder).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Radarr: ManualImportService.cs:495,548 on main, queue imports set newDownload = true at :552.

Suggested change
Sonarr runs the script once per imported episode file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself. The source paths of all the files of a download are worked out before the first call, so a script must not move the other files of a season pack (for example by renaming the download's folder).
Sonarr runs the script once per imported episode file, for downloads and for manual imports, instead of copying, hardlinking or moving the file itself. A manual import of a file that is already inside the series folder does not run the script (unless it is imported from a queue item). The source paths of all the files of a download are worked out before the first call, so a script must not move the other files of a season pack (for example by renaming the download's folder).

Comment thread radarr/settings.md
| -------- | ------- |
| `radarr_sourcepath` | Same as `$1` |
| `radarr_destinationpath` | Same as `$2` |
| `radarr_transfermode` | What Radarr would have done: `Move`, `Copy`, `HardLink`, `HardLinkOrCopy` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HardLink exists in the enum but nothing passes it. MoveMovieFile sends Move, and CopyMovieFile sends HardLinkOrCopy when hardlinks are enabled and Copy otherwise (MovieFileMovingService.cs:82,95,99).

Suggested change
| `radarr_transfermode` | What Radarr would have done: `Move`, `Copy`, `HardLink`, `HardLinkOrCopy` |
| `radarr_transfermode` | What Radarr would have done: `Move`, `Copy`, or `HardLinkOrCopy` when Use Hardlinks instead of Copy is enabled |

Comment thread sonarr/settings.md
| -------- | ------- |
| `sonarr_sourcepath` | Same as `$1` |
| `sonarr_destinationpath` | Same as `$2` |
| `sonarr_transfermode` | What Sonarr would have done: `Move`, `Copy`, `HardLink`, `HardLinkOrCopy` |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Radarr (EpisodeFileMovingService.cs:91,103,107).

Suggested change
| `sonarr_transfermode` | What Sonarr would have done: `Move`, `Copy`, `HardLink`, `HardLinkOrCopy` |
| `sonarr_transfermode` | What Sonarr would have done: `Move`, `Copy`, or `HardLinkOrCopy` when Use Hardlinks instead of Copy is enabled |

Comment thread radarr/settings.md
| `radarr_movie_id`, `radarr_movie_title`, `radarr_movie_year`, `radarr_movie_path`, `radarr_movie_tmdbid`, `radarr_movie_imdbid`, `radarr_movie_tags` | The movie |
| `radarr_moviefile_relativepath`, `radarr_moviefile_quality`, `radarr_moviefile_releasegroup`, `radarr_moviefile_scenename` | The file being imported |
| `radarr_download_client`, `radarr_download_client_type`, `radarr_download_id` | Download client name and type, and the download's id (the torrent hash for torrent clients); empty for imports that are not downloads |
| `radarr_deletedpaths`, `radarr_deletedrelativepaths` | On upgrades, the replaced files, separated by `\|`; Radarr has already deleted them when the script runs |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpgradeMovieFile removes the old file through _recycleBinProvider.DeleteFile before the mover runs (UpgradeMediaFileService.cs:58-72), so with a Recycling Bin set the file is moved there, not deleted. It also means a script that fails on an upgrade leaves the movie with no file.

Suggested change
| `radarr_deletedpaths`, `radarr_deletedrelativepaths` | On upgrades, the replaced files, separated by `\|`; Radarr has already deleted them when the script runs |
| `radarr_deletedpaths`, `radarr_deletedrelativepaths` | On upgrades, the replaced files, separated by `\|`. Radarr has already removed them (deleted, or moved to the Recycling Bin if one is set) when the script runs, so a failing script leaves the movie without a file |

Comment thread sonarr/settings.md

#### Exit code

Anything other than `0` fails the import (shown in Activity > Queue and in the logs).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Radarr; Sonarr's message is "Failed to import episode".

Suggested change
Anything other than `0` fails the import (shown in Activity > Queue and in the logs).
Anything other than `0` fails the import of that file. For a download, Activity > Queue only shows "Failed to import episode"; the exit code is only in the logs (Warn level).

Comment thread radarr/settings.md

#### Environment variables

The most useful ones (the names arrive in **lowercase**, see the warning below):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the Custom Scripts page already uses these lowercase names, so one line here can replace the warning under Exit code.

Suggested change
The most useful ones (the names arrive in **lowercase**, see the warning below):
The most useful ones. The names arrive in lowercase (`radarr_sourcepath`, not `Radarr_SourcePath`), the same as on [Custom Scripts](/radarr/custom-scripts):

Comment thread radarr/settings.md
Comment on lines +359 to +360
> The variable names are lowercase although the source code spells them `Radarr_SourcePath` etc.: Radarr passes them in a .NET `StringDictionary`, which lowercases keys. In a shell script use `$radarr_sourcepath`.
{.is-warning}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the warning here once the note above is in.

Suggested change
> The variable names are lowercase although the source code spells them `Radarr_SourcePath` etc.: Radarr passes them in a .NET `StringDictionary`, which lowercases keys. In a shell script use `$radarr_sourcepath`.
{.is-warning}

Comment thread sonarr/settings.md

#### Environment variables

The most useful ones (the names arrive in **lowercase**, see the warning below):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nit as Radarr.

Suggested change
The most useful ones (the names arrive in **lowercase**, see the warning below):
The most useful ones. The names arrive in lowercase (`sonarr_sourcepath`, not `Sonarr_SourcePath`), the same as on [Custom Scripts](/sonarr/custom-scripts):

Comment thread sonarr/settings.md
Comment on lines +419 to +420
> The variable names are lowercase although the source code spells them `Sonarr_SourcePath` etc.: Sonarr passes them in a .NET `StringDictionary`, which lowercases keys. In a shell script use `$sonarr_sourcepath`.
{.is-warning}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the warning here once the note above is in.

Suggested change
> The variable names are lowercase although the source code spells them `Sonarr_SourcePath` etc.: Sonarr passes them in a .NET `StringDictionary`, which lowercases keys. In a shell script use `$sonarr_sourcepath`.
{.is-warning}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants