Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using maxresults in fetchItems() tramples over results #1393

Closed
Touchstone64 opened this issue Apr 9, 2024 · 0 comments · Fixed by #1394
Closed

Using maxresults in fetchItems() tramples over results #1393

Touchstone64 opened this issue Apr 9, 2024 · 0 comments · Fixed by #1394

Comments

@Touchstone64
Copy link
Contributor

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

Touchstone64 pushed a commit to Touchstone64/python-plexapi that referenced this issue Apr 9, 2024
Touchstone64 pushed a commit to Touchstone64/python-plexapi that referenced this issue Apr 9, 2024
Touchstone64 pushed a commit to Touchstone64/python-plexapi that referenced this issue Apr 9, 2024
JonnyWong16 added a commit that referenced this issue Apr 19, 2024
* Fix: stop maxresults from overwriting previous results

* Added test for use of maxresults in fetchItems #1393

* Removed the need for last_container_size #1393

* Renamed collections to better represen what is being tested

* Renamed collections to better represent what is being tested #1393

* Update tests/test_library.py with cleaner test

Co-authored-by: JonnyWong16 <[email protected]>

---------

Co-authored-by: Graham Thompson <[email protected]>
Co-authored-by: JonnyWong16 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant