Jump to content

Changing genres and tags with API


Go to solution Solved by Killface69,

Recommended Posts

Killface69
Posted (edited)

I'm trying to update Emby genres and tags with the API in python.

Please see below the code in question. 

I tried setting only all the genres/tags at a time with no succuees, I also tried "GenreItems" and "TagItems" instead.
I also converted the list to string items with ', ' and '; ' as separators.
I was able to actually get updated genres for my library items yesterday, but I have no clue what I was doing different.

Do I need to use the TagService for the tags, including Name and Id?

EDIT: Maybe I'm missing this? https://betadev.emby.media/reference/RestAPI/ItemUpdateService/postItemsByItemid.html

Quote

postItemsByItemid

Requires authentication as administrator

 

                print(f"\r{filter_tdb_new_genres}")
  				['ᵃᵈᵛ Adventure', 'ᵃᶜᵗ Action', 'ᶜʳⁱ Crime']
                print(f"\r{tdb_new_tags}")
      			[' TMDB 1. Top', ' MEGA Blockbuster', ' Hollywood Film', ' Big Budget Film', ' Academy Award - Winner', ' Academy Award - Nomination', ' Golden Globe - Nomination']
                emby_server.set_genres_and_tags(media_item.emby_item_id, filter_tdb_new_genres, tdb_new_tags)
      
      ---------------------------


    def set_genres_and_tags(self, item_id, genres: list, tags: list):
        return self.update_item(item_id, {
            "Genres": genres,
            "Tags": tags})
    
    def get_item(self, item_id) -> dict:
        endpoint = f"/emby/users/{self.emby_user_id}/items/{item_id}"
        url = self.emby_server_url + endpoint
        try:
            response = requests.get(url, headers=self.headers)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            print(f"Error while getting item {item_id}: {e}")
            return None

    
        def update_item(self, item_id, updates: dict, max_retries: int = 3, base_delay: float = 0.1,
                    max_delay: float = 2.0) -> bool:
        item = self.get_item(item_id)
        if not item:
            tqdm.write(f"\rItem {item_id} not found.")
            return False

        # Update LockedFields if needed
        if "ForcedSortName" in updates and "SortName" not in item.get("LockedFields", []):
            item.setdefault("LockedFields", []).append("SortName")
        # print(item)
        # Combine updates
        item.update(updates)
        # print(updates)
        # print(item)
        update_url = f"{self.emby_server_url}/emby/Items/{item_id}?api_key={self.api_key}"
        delay = base_delay

        for attempt in range(1, max_retries + 1):
            try:
                response = requests.post(update_url, json=item, headers=self.headers)
                response.raise_for_status()  # Raise error for non-2xx responses
                # print(response.text)
                tqdm.write(f"\rUpdated item {item_id} with {updates}.")
                time.sleep(self.seconds_between_requests)
                return True
            except requests.RequestException as e:
                tqdm.write(f"\rError while updating item {item_id} (Attempt {attempt}/{max_retries}): {e}")
                if attempt < max_retries:
                    time.sleep(delay)
                    delay = min(delay * 5, max_delay)  # Erhöhe das Delay exponentiell
                else:
                    tqdm.write(f"\rFailed to update item {item_id} after {max_retries} attempts.")
                    return False

 

Edited by Killface69
  • Solution
Killface69
Posted

Found a solution, it's only working if both xyz and xyzItems are updated at once.

Working example:

        return self.update_item(item_id, {
            "Genres": genres,
            "GenreItems": genres,
            "Tags": tags,
            "TagItems": tags,
        }

 

  • Thanks 1

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...