Two Sonarr instances same files

Hey

It is a weird question. A friend of mine wants to create his own library on my server, which is fine for me, but we want to avoid having duplicate files as much as possible.
Currently I am running an Unraid Server.

ChatGPT wrote me this scripts (I have not tested the code and I am not really a developer.):

import os
import shutil
import requests
import json

# Sonarr API configuration
sonarr_url = 'http://localhost:8989/api'  # Replace with your Sonarr URL
api_key = 'YOUR_API_KEY'  # Replace with your Sonarr API key

# Path to the folder where the show is located
existing_show_folder = '/path/to/existing/show/folder'

def series_added_handler(series_id):
    headers = {'X-Api-Key': api_key}
    response = requests.get(sonarr_url + f'/series/{series_id}', headers=headers)
    series = json.loads(response.content.decode('utf-8'))

    series_title = series['title']
    print(f"Series added: {series_title}")

    # Check if the show already exists in the specified folder
    existing_show_path = os.path.join(existing_show_folder, series_title)
    if os.path.exists(existing_show_path):
        print("Show found in the existing folder. Creating hardlink...")
        new_show_path = os.path.join('/path/to/sonarr/library', series_title)  # Replace with your Sonarr library path
        os.link(existing_show_path, new_show_path)
        print("Hardlink created successfully.")

        # Refresh the show in Sonarr
        refresh_url = sonarr_url + f'/series/{series_id}/refresh'
        response = requests.post(refresh_url, headers=headers)
        if response.status_code == 200:
            print("Show refreshed in Sonarr.")
        else:
            print("Failed to refresh the show in Sonarr.")

    else:
        print("Show not found in the existing folder.")

def event_handler(event):
    event_type = event['eventType']
    if event_type == 'SeriesAdded':
        series_id = event['series']['id']
        series_added_handler(series_id)

def listen_to_events():
    headers = {'X-Api-Key': api_key}
    response = requests.get(sonarr_url + '/event', headers=headers, stream=True)

    for line in response.iter_lines():
        if line:
            event = json.loads(line.decode('utf-8'))
            event_handler(event)

if __name__ == '__main__':
    listen_to_events()

I know that there is an event on the wiki if a show is added, but if I want to run a custom script in Sonarr I do not have the option to trigger the event on show added.

Does anyone have any experience with this? On how to set this up properly? Can this be achieved with sonarr sync?

I was thinking of just running a sync between the files and then hardlinking them like this:

import os
import shutil

def compare_and_link(dir1, dir2):
    # Get the filenames in each directory
    dir1_files = set(os.listdir(dir1))
    dir2_files = set(os.listdir(dir2))

    # Find the common files
    common_files = dir1_files.intersection(dir2_files)

    for filename in common_files:
        file1 = os.path.join(dir1, filename)
        file2 = os.path.join(dir2, filename)

        # Ensure both paths are files (not directories) before proceeding
        if os.path.isfile(file1) and os.path.isfile(file2):
            # Remove the file in the second directory
            os.remove(file2)

            # Create a hard link in the second directory to the file in the first directory
            os.link(file1, file2)

# Example usage
compare_and_link('/path/to/dir1', '/path/to/dir2')

I would be appreciative of any feedback!

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