An easy(ish) fix for bad indexer search terms/custom search terms

I haven’t been playing around with Sonarr for too long, but I almost right away ran into a situation where Sonarr used a “wrong” search term for my tracker: it tried to find search for “Foundation (2021)” to get the new TV adaptation, but it was only ever uploaded on my tracker as “Foundation”. Not really understanding the scene renaming process (as led by Sonarr and released as an update), I thought I’d see if I could easily fix it myself.

Turns out, it’s pretty easy to do with mitmproxy (man-in-the-middle proxy). This is a tool for proxying http requests that uses self-signed certificates to allow you modify requests over https, such as the endpoint Sonarr uses to get the scene mapping adjustments. Install mitmproxy however makes sense for your setup. I run all my services as docker containers so it’s as easy as adding this container to docker, but it should work with direct installed setups as well. I mounted a volume for /home/mitmproxy/.mitmproxy so I had an easy place to drop addon scripts. You can also use this directory to get the self-signed certs out if needed, but Sonarr has an option ignore certs, which means I don’t have to do any manual setup/customization of the Sonarr docker image (and mitmproxy will check the real certs on the actual endpoints anyways). I don’t even need to publish the ports on docker as Sonarr and mitmproxy share a manual bridge network so all connections are internal. Just point Sonarr (Settings -> General -> Proxy) to the location of your mitmproxy install (probably localhost or 127.0.0.1 when using directly installed software, using docker on a bridge network the container engine automatically does dns discovery between containers, so you can literally just enter mitmproxy [or whatever you name your proxy container]), set the port to 8080 and your proxy type to HTTP(S). If you aren’t going to manually install the self signed certs from mitmproxy, in the Security section, set Certificate Validation to Disabled.

Finally, setup mitmproxy to modify the returned scene renames. My script looks like: add_scene_renames.py. It simply looks for the endpoint that Sonarr uses to get the scene renames, and adds any found locally in the file scene_renames.json. You can start that up with something like this (which is what I use as the run command for my mitmproxy docker container): mitmdump --flow-detail 0 -s /home/mitmproxy/.mitmproxy/add_scene_renames.py. Checking the log of that command will confirm if Sonarr is using mitmproxy to retrieve this info. Then, just load up scene_renames.json with your new modified searches, for example:

[
        {
                "tvdbId":366972,
                "title":"Foundation",
                "searchTitle":"Foundation",
                "season":-1
        }
]

I just copied this format from the Sonarr supplied endpoint, which you can see here. As far as I know, both title and searchTitle should be present, although they never seem to have values that are different.

Hopefully this will help someone out if their indexer isn’t quite giving the result they want, and you can’t wait for a Sonarr update to add your renames!

P.S. In the time it took me to develop this solution and write this post, looks like Sonarr has been updated to have correct searches for Foundation anyways, haha :man_shrugging: