Skip to content

Instantly share code, notes, and snippets.

@goose-ws
Last active May 24, 2024 10:20
Show Gist options
  • Save goose-ws/fb5ba309081b03f77ff39127f7bc563f to your computer and use it in GitHub Desktop.
Save goose-ws/fb5ba309081b03f77ff39127f7bc563f to your computer and use it in GitHub Desktop.
Script to fix TBA titled episodes in Sonarr
This script has been rewritten and improved, and is now located in my `bash scripts` repository: https://github.com/goose-ws/bash-scripts
More specifically, here is the script: https://github.com/goose-ws/bash-scripts/blob/main/sonarr-update-tba.bash
And here is the corresponding `.env` file you need to put in the same directory as the script: https://github.com/goose-ws/bash-scripts/blob/main/sonarr-update-tba.env.example
@Sixcurses
Copy link

Thanks for making this
I've changed docker exec -it to docker exec -t to make it work for unraid user scripts.

@goose-ws
Copy link
Author

@Sixcurses Thanks for the comment. Both -i and -t were actually unnecessary, given the way the script works. I've updated the gist to the most recent version.

@Hannah-GBS
Copy link

This doesn't seem to be working for me any more. It doesn't rename anything, and I get no console output of any errors.

@goose-ws
Copy link
Author

@Hannah-GBS - I actually had on my to-do list to do a total rewrite of this script this weekend to be v3/v4 compatible and generally clean up/standardized to the way I script now. I'll let you know when it's done and published, I'd expect in the next week or so.

@goose-ws
Copy link
Author

@Hannah-GBS and @Sixcurses - I ended up with some free time today, and went ahead with a rewrite.

This script has been rewritten and improved, and is now located in my bash scripts repository: https://github.com/goose-ws/bash-scripts

More specifically, here is the script: https://github.com/goose-ws/bash-scripts/blob/main/sonarr-update-tba.bash

And here is the corresponding .env file you need to put in the same directory as the script: https://github.com/goose-ws/bash-scripts/blob/main/sonarr-update-tba.env.example

Let me know if you need further assistance with it.

@DeuX01
Copy link

DeuX01 commented May 17, 2024

Thanks for the updated script, how fisable would it be to add an ignore root folder? I have a library with PT-PT with shows that most of the time have TBA as the title and never gets updated, so I would like to ignore the entire root folder. Thanks in advance :)

@goose-ws
Copy link
Author

@DeuX01, sure, open a feature request issue on the repo and I'll get to it in the next week or so.

@DeuX01
Copy link

DeuX01 commented May 17, 2024

Just did! Thanks a bunch :)

@SlingerAJ
Copy link

Compliments on the script, it's amazing and infinitely better than my few admin hacks.

I read it to try to understand all of it then just gave up and ran it! Worked perfectly first time.

I came across this chat in search of a solution to a similar problem, I have Unmanic transcoding new Sonarr / Radarr downloads in Unraid. The issue i'm trying to fix is getting those files renamed after the transcoding has replaced the original file with the smaller (usually) transcoded file.

After the daily refresh, Sonarr correctly identifies the video/sound codecs ("h265" and "AAC") but the file name still reflects the old codecs (for example "h264" and "EAC3")

I do a bulk rename in Sonarr / Radarr every morning to fix this and it works just fine, producing the following for Sonarr.

  • Old name: "The Test - S02E01 - Own Your Own Space (2023-01-13) WEBDL-720p x264 EAC3.mp4"
  • New name: "The Test - S02E01 - Own Your Own Space (2023-01-13) WEBDL-720p h265 AAC.mkv"

It's a bit of a hack, but gets the job done - I do the following;

  1. Custom filter new shows/movies added in the last 3 days and run the rename on those shows/movies.
  2. Custom filter shows with new episodes added in the last 3 days and run the rename on those shows.

There's a fair bit of redundancy as I do it daily on 3 day's worth of additions, and it renames all the episodes of a series when only one really probably needs renaming for existing shows (spinning up the array as only 7 days worth of new content lives in the cache), and it's renaming 3 days' worth of new shows in case I forget one day. The alternative is a bulk re-name of all shows, which also spins up the array disks unnecessarily.

Solution:
I'd like to modify your script to do this in Sonarr (and Radarr but Sonarr good for a start)

  1. Find all episodes added in the last day (since it last ran, assuming I run it daily) - this will find new episodes in old shows and new episodes in new shows.
  2. Rename them to reflect the correct codecs

I'd probably then run the original script to solve the TBA issue (which i'd never even thought of - thanks again)

Does this sound possible? it would require a search for the episodes by date added (or file date as this will be a few minutes later after Unmanic has done transcoding it)

I'll have a look myself and try to change it but don't want to break anything and my scripting is a bit basic.

Best Regards and thanks again for the script!

Aidan

@DeuX01
Copy link

DeuX01 commented May 23, 2024

@SlingerAJ no need for that, install the Sonarr plugin in unmanic and enable the option to trigger a rename in sonarr

@SlingerAJ
Copy link

Tried that and played with the settings for a week or two with no luck, importing / refresh & rename. Nothing seemed to work. After it ran the files were unchanged - had it at the end of the sequence of plugins.

Was wondering if it was an API access issue. How is yours set up?

@DeuX01
Copy link

DeuX01 commented May 23, 2024

#!/bin/bash

/usr/bin/apt-get update

python3 -m pip install pyarr
python3 -m pip install humanfriendly

should do it - put that in a file named startup.sh, make it executable (chmod +x startup.sh) and put it in /config in the container. restart unmanic. everytime unmanic is started &/or recreated it'll install these. You won't need to worry about it any more. You can add other things to this if there are other utilities you need to install in your unmanic container. This should fix the api issue

@SlingerAJ
Copy link

After download:
image

After Transcode
image

Let me try the above script, sit tight :)

@SlingerAJ
Copy link

Hmm - didn't seem to work

After download and transcode
image

After manual Series Refresh
image

After manual series rename
image

Then - success.
image

Doesn't seem to be automatically triggering refresh / rename

Let me look at the logs - Unmanic or Sonarr (or both)? Trouble is I changed the minimum file size from 500M to 300m and Unmanic has now grabbed a load of existing files to transcode so might need to dig to find this specific example.

@SlingerAJ
Copy link

#!/bin/bash

/usr/bin/apt-get update

python3 -m pip install pyarr python3 -m pip install humanfriendly

should do it - put that in a file named startup.sh, make it executable (chmod +x startup.sh) and put it in /config in the container. restart unmanic. everytime unmanic is started &/or recreated it'll install these. You won't need to worry about it any more. You can add other things to this if there are other utilities you need to install in your unmanic container. This should fix the api issue

I created this, the config folder is in /appdata/unmanic/.unmanic/config

So I added the script in here - that correct?

When I run it manually i get
image

@SlingerAJ
Copy link

Thought i'd installed this in Geek Tools but seems not to have taken
image

When I hit apply....
image

@DeuX01
Copy link

DeuX01 commented May 23, 2024

You need to put the script inside /mnt/user/appdata/unmanic and then restart the container. When you restart the container the proper modules will be installed inside the container

@SlingerAJ
Copy link

Here's what is installed for Python
image

@DeuX01
Copy link

DeuX01 commented May 23, 2024

Thats for your OS, modules inside the container are independent from the modules installed in the host. Point being is that the missing modules need to be installed inside the container not in the host

@SlingerAJ
Copy link

You need to put the script inside /mnt/user/appdata/unmanic and then restart the container. When you restart the container the proper modules will be installed inside the container

OK, moved the script to /mnt/user/appdata/unmanic/startup.sh - it's the only thing in there except the two hidden folders.

Just ran it, one error and two warnings. Can I now restart unmanic or do we need to resolve the problem in line 3?

image

@DeuX01
Copy link

DeuX01 commented May 23, 2024

Restart the container and check the logs. You should see both modules being pulled, then make sure that Sonarr plugin in unmanic has the rename option enabled and it should work.

@SlingerAJ
Copy link

Restart the container and check the logs. You should see both modules being pulled, then make sure that Sonarr plugin in unmanic has the rename option enabled and it should work.

Container startup logs - looks ok?

**** (permissions_config) Setting permissions ****
**** (permissions_config) Adding run user to video group ****
**** (custom-setup-script) Calling custom user setup script ****
Get:1 https://deb.nodesource.com/node_20.x nodistro InRelease [12.1 kB]
Get:2 https://deb.nodesource.com/node_20.x nodistro/main amd64 Packages [7,133 B]
Get:3 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
Get:4 https://repo.jellyfin.org/ubuntu jammy InRelease [6,660 B]
Get:5 https://sgp1.mirror.jellyfin.org/files/ubuntu jammy/main amd64 Packages [1,895 B]
Get:6 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:7 https://repositories.intel.com/gpu/ubuntu jammy InRelease [15.4 kB]
Err:7 https://repositories.intel.com/gpu/ubuntu jammy InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 28DA432DAAC8BAEA
Get:8 http://archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:9 http://archive.ubuntu.com/ubuntu jammy/multiverse Sources [361 kB]
Get:10 http://archive.ubuntu.com/ubuntu jammy/main Sources [1,668 kB]
Get:11 http://archive.ubuntu.com/ubuntu jammy/restricted Sources [28.2 kB]
Get:12 http://archive.ubuntu.com/ubuntu jammy/universe Sources [22.0 MB]
Get:13 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1,792 kB]
Get:15 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB]
Get:16 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB]
Get:17 http://archive.ubuntu.com/ubuntu jammy-updates/universe Sources [415 kB]
Get:18 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse Sources [21.9 kB]
Get:19 http://archive.ubuntu.com/ubuntu jammy-updates/main Sources [611 kB]
Get:20 http://archive.ubuntu.com/ubuntu jammy-updates/restricted Sources [78.2 kB]
Get:21 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [2,125 kB]
Get:22 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [2,468 kB]
Get:23 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,375 kB]
Get:24 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [51.1 kB]
Get:25 http://archive.ubuntu.com/ubuntu jammy-security/universe Sources [238 kB]
Get:26 http://archive.ubuntu.com/ubuntu jammy-security/restricted Sources [73.9 kB]
Get:27 http://archive.ubuntu.com/ubuntu jammy-security/main Sources [337 kB]
Get:28 http://archive.ubuntu.com/ubuntu jammy-security/multiverse Sources [12.2 kB]
Get:29 http://archive.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [2,395 kB]
Get:30 http://archive.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1,853 kB]
Get:31 http://archive.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [1,083 kB]
Get:32 http://archive.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [44.7 kB]
Reading package lists...
Collecting pyarr
Downloading pyarr-5.2.0-py3-none-any.whl (39 kB)
Requirement already satisfied: requests<3.0.0,>=2.28.2 in /usr/local/lib/python3.10/dist-packages (from pyarr) (2.31.0)
Collecting types-requests<3.0.0.0,>=2.28.11.17
Downloading types_requests-2.32.0.20240523-py3-none-any.whl (15 kB)
Collecting overrides<8.0.0,>=7.3.1
Downloading overrides-7.7.0-py3-none-any.whl (17 kB)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.28.2->pyarr) (2024.2.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.28.2->pyarr) (3.3.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.28.2->pyarr) (2.2.1)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.28.2->pyarr) (3.7)
Installing collected packages: types-requests, overrides, pyarr
Successfully installed overrides-7.7.0 pyarr-5.2.0 types-requests-2.32.0.20240523
Collecting humanfriendly
Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 86.8/86.8 KB 2.4 MB/s eta 0:00:00
Installing collected packages: humanfriendly
Successfully installed humanfriendly-10.0
[custom-init] No custom files found, skipping...
[ls.io-init] done.

@DeuX01
Copy link

DeuX01 commented May 23, 2024

Yep it should work now

@SlingerAJ
Copy link

It works - YOU BEAUTY!

Thanks for all the support, so what did we just do, install python3 in the container so python based plugins can work? I wonder why this isn't installed as standard if it's a dependency for some of the plugins.

For some reason Unmanic (since the first re-start) has now found 1500 files to transcode, most of which are failing (output file larger than the input file). The only prior change was to set the minimum file size to 300M (it was 500M). Might change it back as small x264 files don't seem to benefit from transcoding in terms of size reduction.

Thanks so much, wouldn't have figured this out myself in a month of Sundays and didn't find anything when i googled the problem for the plugin/unmanic.

@DeuX01
Copy link

DeuX01 commented May 23, 2024

We just installed the missing modules needed by the Sonarr and Radarr plugins. Python3 already comes installed by default, but the modules were missing. By keeping the startup script there it will always reinstall them even if you rebuild the container. Enjoy

@SlingerAJ
Copy link

Brilliant, thanks again!

@SlingerAJ
Copy link

Been watching it for a day now, it's very cool to see it all working properly.

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