Jump to content

change sort title of an item using api


Recommended Posts

danielromerohn
Posted

I created a script in chatgpt because I don't know how to program, I don't speak English either, I use Google Translate.

I want to make a script that gets the date an item was added and then stores that value in the sortname field. But for now I just want to change the sort title and when I manage to do that I will adapt the script for my final purpose.

Can someone tell me what is wrong?
 



Script python:
import requests

# Configura estos valores según tu servidor Emby
EMBY_SERVER_URL = 'https://XXXXXXXXX.com'
API_KEY = '5d95c9fa770e4fddbbd224db882aXXXX'
USER_ID = 'c2e0b7e5700b4dad9fea3affe388XXXX'
ITEM_ID = 62086

def update_sorttitle(item_id, sorttitle):
    get_item_url = f'{EMBY_SERVER_URL}/Users/{USER_ID}/Items/{item_id}'
    headers = {'X-MediaBrowser-Token': API_KEY}
    
    # Obtener el ítem actual
    response = requests.get(get_item_url, headers=headers)
    try:
        response.raise_for_status()
        item = response.json()
    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred while getting item: {err}")
        return
    except requests.exceptions.RequestException as err:
        print(f"Error occurred while getting item: {err}")
        return
    except ValueError:
        print("Error decoding JSON response while getting item")
        return

    # Actualizar el campo SortTitle
    item['SortTitle'] = sorttitle
    
    # Enviar la actualización
    update_url = f'{EMBY_SERVER_URL}/Items/{item_id}?api_key={API_KEY}'
    response = requests.post(update_url, json=item, headers=headers)
    try:
        response.raise_for_status()
        print(f'Successfully updated SortTitle to {sorttitle}')
    except requests.exceptions.HTTPError as err:
        print(f"HTTP error occurred while updating item: {err}")
    except requests.exceptions.RequestException as err:
        print(f"Error occurred while updating item: {err}")
    except ValueError:
        print("Error decoding JSON response while updating item")

# Llamar a la función para actualizar el SortTitle
update_sorttitle(ITEM_ID, 'Felicitaciones')


Sincerely

Captura de pantalla 2024-12-06 114503.png

Captura de pantalla 2024-12-06 114550.png

Posted

Hi, you also need to lock the field or the server may end up changing it on you. Notice how when using the metadata editor, it auto locks any field you touch.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...