Skip to content

Instantly share code, notes, and snippets.

@Ravencentric
Last active December 5, 2025 21:32
Show Gist options
  • Select an option

  • Save Ravencentric/38002d8e056fd6c226d4460fb82f251a to your computer and use it in GitHub Desktop.

Select an option

Save Ravencentric/38002d8e056fd6c226d4460fb82f251a to your computer and use it in GitHub Desktop.
Script to refresh monitored downloads in Sonarr/Radarr
# Script to trigger the RefreshMonitoredDownloads task in Sonarr and Radarr
# Sonarr/Radarr only checks the monitored downloads every 60 seconds which
# means that your download could be done in 20 seconds but it'll sit there
# for an additional 40 seconds before Sonarr/Radarr check again. This script
# will run when a download finishes and trigger the RefreshMonitoredDownloads
# task.
import os
import requests
def RefreshMonitoredDownloads(API_KEY: str, URL: str) -> None:
PAYLOAD = {"name": "RefreshMonitoredDownloads"}
headers = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
"X-Api-Key": API_KEY,
"X-Requested-With": "XMLHttpRequest",
}
requests.post(URL, headers=headers, json=PAYLOAD)
SONARR_API_KEY = "abc123"
SONARR_URL = "http://127.0.0.1:8989/api/v3/command"
RADARR_API_KEY = "abc123"
RADARR_URL = "http://127.0.0.1:7878/api/v3/command"
RefreshMonitoredDownloads(SONARR_API_KEY, SONARR_URL)
RefreshMonitoredDownloads(RADARR_API_KEY, RADARR_URL)
@LukasdeBoer
Copy link

Nice start. If you want to use this in nzbget as a postprocessing script, create one or two scripts in the scripts folder for nzbget (be sure to update your URL and api key):

refresh-sonarr.py:

#!/usr/bin/env python
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###


# Script to trigger the RefreshMonitoredDownloads task in Sonarr and Radarr
#
# Sonarr/Radarr only checks the monitored downloads every 60 seconds which
# means that your download could be done in 20 seconds but it'll sit there
# for an additional 40 seconds before Sonarr/Radarr check again. This script
# will run when a download finishes and trigger the RefreshMonitoredDownloads
# task.

##############################################################################

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import os
import requests
import sys

def RefreshMonitoredDownloads(API_KEY: str, URL: str) -> None:
    PAYLOAD = {"name": "RefreshMonitoredDownloads"}

    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
        "X-Api-Key": API_KEY,
        "X-Requested-With": "XMLHttpRequest",
    }

    requests.post(URL, headers=headers, json=PAYLOAD)


SONARR_API_KEY = "<redacted>"
SONARR_URL = "http://sonarr:8989/api/v3/command"

RefreshMonitoredDownloads(SONARR_API_KEY, SONARR_URL)

NZBGET_POSTPROCESS_SUCCESS = 93
sys.exit(NZBGET_POSTPROCESS_SUCCESS)

refresh-radarr.py:

#!/usr/bin/env python
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###


# Script to trigger the RefreshMonitoredDownloads task in Sonarr and Radarr
#
# Sonarr/Radarr only checks the monitored downloads every 60 seconds which
# means that your download could be done in 20 seconds but it'll sit there
# for an additional 40 seconds before Sonarr/Radarr check again. This script
# will run when a download finishes and trigger the RefreshMonitoredDownloads
# task.

##############################################################################

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import os
import requests
import sys

def RefreshMonitoredDownloads(API_KEY: str, URL: str) -> None:
    PAYLOAD = {"name": "RefreshMonitoredDownloads"}

    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
        "X-Api-Key": API_KEY,
        "X-Requested-With": "XMLHttpRequest",
    }

    requests.post(URL, headers=headers, json=PAYLOAD)


RADARR_API_KEY = "<redacted>"
RADARR_URL = "http://radarr:7878/api/v3/command"

RefreshMonitoredDownloads(RADARR_API_KEY, RADARR_URL)

NZBGET_POSTPROCESS_SUCCESS = 93
sys.exit(NZBGET_POSTPROCESS_SUCCESS)

and for nzbget (i'm using nzbgetcom/nzbget), I've had to remove the entrypoint (extra parameters in unraid)

--entrypoint ''

and add post arguments:

sh -c 'apk add py-pip && pip install requests --break-system-packages && /entrypoint.sh'

Then it should detect the script as an extension in nzbget. Be sure to enable the extension for the categories you have set (e.g. TV shows -> refresh-sonarr.py and Movies -> refresh-radarr.py) and then it should automatically trigger sonarr/radarr after a download is complete.

@dodofarm
Copy link

Here's the same but for SABnzbd, just removed NZBGet specific stuff and that's basically it:

Sonarr:

#!/usr/bin/env python3
"""
SABnzbd Post-Processing Script

Script to trigger the RefreshMonitoredDownloads task in Sonarr after
a download completes. This helps reduce the delay between download completion
and Sonarr processing, as they only check monitored downloads every 60 seconds
by default.

Place this script in your SABnzbd scripts directory and configure it in
SABnzbd's Config > Categories > Post-Processing Scripts
"""

import os
import sys
import requests

def refresh_monitored_downloads(api_key: str, url: str) -> None:
    """
    Trigger the RefreshMonitoredDownloads command in Sonarr
    
    Args:
        api_key: Sonarr API key
        url: Full API URL for the command endpoint
    """
    payload = {"name": "RefreshMonitoredDownloads"}
    
    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
        "X-Api-Key": api_key,
        "X-Requested-With": "XMLHttpRequest",
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        print(f"Error refreshing monitored downloads: {e}")
        sys.exit(1)

SONARR_API_KEY = "<redacted>"
SONARR_URL = "http://sonarr:8989/api/v3/command"

# Refresh Sonarr
refresh_monitored_downloads(SONARR_API_KEY, SONARR_URL)

sys.exit(0)

Radarr:

#!/usr/bin/env python3
"""
SABnzbd Post-Processing Script

Script to trigger the RefreshMonitoredDownloads task in Radarr after
a download completes. This helps reduce the delay between download completion
and Radarr processing, as they only check monitored downloads every 60 seconds
by default.

Place this script in your SABnzbd scripts directory and configure it in
SABnzbd's Config > Categories > Post-Processing Scripts
"""

import os
import sys
import requests

def refresh_monitored_downloads(api_key: str, url: str) -> None:
    """
    Trigger the RefreshMonitoredDownloads command in Radarr
    
    Args:
        api_key: Radarr API key
        url: Full API URL for the command endpoint
    """
    payload = {"name": "RefreshMonitoredDownloads"}
    
    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
        "X-Api-Key": api_key,
        "X-Requested-With": "XMLHttpRequest",
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        print(f"Error refreshing monitored downloads: {e}")
        sys.exit(1)

RADARR_API_KEY = "<redacted>"
RADARR_URL = "http://sonarr:8989/api/v3/command"

# Refresh Radarr
refresh_monitored_downloads(RADARR_API_KEY, RADARR_URL)

sys.exit(0)

@Warbs816
Copy link

Thanks @dodofarm - this does work to refresh monitored downloads in Sonarr/Radarr but because when the script runs the download is still technically not complete in SABnzbd (not complete until after the script has finished), Sonarr still thinks the item is downloading and does not mark it as complete/waiting to import. I'm struggling to work out a workaround for this. Any ideas?

@dodofarm
Copy link

Hey @Warbs816,
Hmm interesting I didn't even realize that, maybe because I have a few different download clients and my Sonarr check SAB last?

But even manually checking it seems like SAB first completes all the needed steps like unpacking etc and then triggers the script which is where radarr etc. jumps in and does a perfect job with importing (at least in my case). Not sure why in your case SAB would trigger the script before being done with processing.

The simplest solution I can think of just adding a 5 second sleep timer in the script before making the call to Radarr. If that doesn't work, make the above script trigger a second script (at this point the execution of the first script is finished). The second script can then wait 5 seconds before sending the API call.

The 5 seconds is just an arbitrary number, if what you're saying is true and SAB executes the script as the last step then you can also wait for just 1 second or even less before triggering the second script.

@Warbs816
Copy link

Warbs816 commented Mar 24, 2025

Thanks @dodofarm - sleeping in the same script didn't work, SABnzbd again just waits for the entire script to finish before marking a job as complete.

I had tried a second trigger script, but initially the first script that SABnzbd runs was waiting for that to finish before also completing, but after some tweaks I got it to work!

Thanks again!

Trigger script:

import subprocess
import os

subprocess.Popen(
    ["/config/scripts/sonarr_refresh_downloads.py"],
    stdout=subprocess.DEVNULL,
    stderr=subprocess.DEVNULL,
    stdin=subprocess.DEVNULL,
    start_new_session=True,
    close_fds=True
)

@dodofarm
Copy link

Great @Warbs816! Yea what you posted is something I had in mind.

Maybe worth checking if you have some old version of SAB or some switch flipped in the settings that marks a download only complete after a the script finishes running?

But obviously if SAB waits for the script to finish it will also wait for the sleep call to finish hence rendering it useless. Triggering a second script might be the only way for you then.

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