Skip to content

Using maxresults in fetchItems() tramples over results #1393

@Touchstone64

Description

@Touchstone64

Describe the Bug

plexapi.server.library.fetchItems() supports a maxresults parameter which, when specified, limits the number of results returned. In code, this is implemented by reducing the size of the container used when there are fewer than container_size results that need to be fetched.

If maxresults were set to 123, for example, then the first fetch would get 100 items (because of the default container size), and the second only 23. In this example, on the second fetch the container size would be reduced to 23.

At the end of the fetchItems loop, container_start is advanced by container_size ready to receive the next batch of results. Unfortunately this is done after container_size is modified to support maxresults. So, in the example above, container_start is set to 23, so the next batch of results overwrites whatever results were already 'at 23' in the container.

Code Snippets

# where plex is an authorised instance of plexapi.server and Plex has more than 110 TV shows:

def demonstrate_duplicates(self) -> None:
    def build_shows_by_id(shows: list) -> dict:
        shows_by_id = {}
        for show in shows:
            if show.key in shows_by_id:
                print(f"Duplicate: {show.key} = {show.title}")
            else:
                shows_by_id[show.key] = show
        return shows_by_id

    items = plex.library.search(libtype="show", limit=110)
    ids = [item.key for item in items]

    int_ids = [int(id.rsplit("/", 1)[-1]) for id in ids]
    bad_shows = plex.library.fetchItems(ekey=int_ids, maxresults=len(int_ids))
    good_shows = plex.library.fetchItems(ekey=int_ids)

    good_shows_by_id = build_shows_by_id(good_shows)
    bad_shows_by_id = build_shows_by_id(bad_shows)

Expected Behavior

fetchItems() in PlexObject would return the media requested whether maxresults is specified or not.

Additional Context

No response

Operating System and Version

macOS 14.3.1

Plex Media Server Version

4.125.1

Python Version

3.12.2

PlexAPI Version

4.15.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions