From 7853d8142ff9ad4b3b7e3f38e19b0dbdde5b1fa7 Mon Sep 17 00:00:00 2001 From: Graham Thompson Date: Tue, 9 Apr 2024 15:46:36 +0100 Subject: [PATCH 1/6] Fix: stop maxresults from overwriting previous results --- plexapi/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plexapi/base.py b/plexapi/base.py index 0852426d2..8c8dc8ce8 100644 --- a/plexapi/base.py +++ b/plexapi/base.py @@ -283,6 +283,8 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, results.extend(subresults) + last_container_size = container_size + wanted_number_of_items = total_size - offset if maxresults is not None: wanted_number_of_items = min(maxresults, wanted_number_of_items) @@ -291,7 +293,7 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, if wanted_number_of_items <= len(results): break - container_start += container_size + container_start += last_container_size if container_start > total_size: break From da82cc0457011340e88e6834bd044e4818824c24 Mon Sep 17 00:00:00 2001 From: Graham Thompson Date: Tue, 9 Apr 2024 20:26:51 +0100 Subject: [PATCH 2/6] Added test for use of maxresults in fetchItems #1393 --- tests/test_library.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_library.py b/tests/test_library.py index cc5289d5e..fbcafe1c3 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -120,6 +120,21 @@ def test_library_fetchItem(plex, movie): assert item1 == item2 == movie +def test_library_fetchItems_with_maxresults(plex): + items1 = plex.library.search(libtype="episode") + assert len(items1) > 5 + size = len(items1) - 5 + ids = [item.key for item in items1] + # Reduce '/library/metadata/123' to '123' + int_ids = [int(id.rsplit("/", 1)[-1]) for id in ids] + items2 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] + items3 = [item.key for item in plex.library.fetchItems( + container_size=size, ekey=int_ids, maxresults=len(int_ids) + )] + assert len(items2) == len(set(items2)) + assert len(items3) == len(set(items3)) + + def test_library_onDeck(plex, movie): movie.updateProgress(movie.duration // 4) # set progress to 25% assert movie in plex.library.onDeck() From 7f183d7538cc84a1d34a3112afe08bfbb6f9fa1d Mon Sep 17 00:00:00 2001 From: Graham Thompson Date: Tue, 9 Apr 2024 20:46:43 +0100 Subject: [PATCH 3/6] Removed the need for last_container_size #1393 --- plexapi/base.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/plexapi/base.py b/plexapi/base.py index 8c8dc8ce8..d35ea8f11 100644 --- a/plexapi/base.py +++ b/plexapi/base.py @@ -283,7 +283,10 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, results.extend(subresults) - last_container_size = container_size + container_start += container_size + + if container_start > total_size: + break wanted_number_of_items = total_size - offset if maxresults is not None: @@ -293,11 +296,6 @@ def fetchItems(self, ekey, cls=None, container_start=None, container_size=None, if wanted_number_of_items <= len(results): break - container_start += last_container_size - - if container_start > total_size: - break - return results def fetchItem(self, ekey, cls=None, **kwargs): From 1a7b2bd19d6e09843eb3235f3500702940551281 Mon Sep 17 00:00:00 2001 From: Graham Thompson Date: Tue, 9 Apr 2024 21:04:05 +0100 Subject: [PATCH 4/6] Renamed collections to better represen what is being tested --- tests/test_library.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_library.py b/tests/test_library.py index fbcafe1c3..45521eb74 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -127,12 +127,12 @@ def test_library_fetchItems_with_maxresults(plex): ids = [item.key for item in items1] # Reduce '/library/metadata/123' to '123' int_ids = [int(id.rsplit("/", 1)[-1]) for id in ids] - items2 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] - items3 = [item.key for item in plex.library.fetchItems( + keys1 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] + keys2 = [item.key for item in plex.library.fetchItems( container_size=size, ekey=int_ids, maxresults=len(int_ids) )] - assert len(items2) == len(set(items2)) - assert len(items3) == len(set(items3)) + assert len(keys1) == len(set(keys1)) + assert len(keys2) == len(set(keys2)) def test_library_onDeck(plex, movie): From 947882deaffc3454448f09f0c2ee0cdd06570034 Mon Sep 17 00:00:00 2001 From: Graham Thompson Date: Tue, 9 Apr 2024 21:04:05 +0100 Subject: [PATCH 5/6] Renamed collections to better represent what is being tested #1393 --- tests/test_library.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_library.py b/tests/test_library.py index fbcafe1c3..45521eb74 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -127,12 +127,12 @@ def test_library_fetchItems_with_maxresults(plex): ids = [item.key for item in items1] # Reduce '/library/metadata/123' to '123' int_ids = [int(id.rsplit("/", 1)[-1]) for id in ids] - items2 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] - items3 = [item.key for item in plex.library.fetchItems( + keys1 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] + keys2 = [item.key for item in plex.library.fetchItems( container_size=size, ekey=int_ids, maxresults=len(int_ids) )] - assert len(items2) == len(set(items2)) - assert len(items3) == len(set(items3)) + assert len(keys1) == len(set(keys1)) + assert len(keys2) == len(set(keys2)) def test_library_onDeck(plex, movie): From 25cc41bd90f8c9074a5a926de9114877209a3943 Mon Sep 17 00:00:00 2001 From: Touchstone64 <57038415+Touchstone64@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:56:00 +0100 Subject: [PATCH 6/6] Update tests/test_library.py with cleaner test Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> --- tests/test_library.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/test_library.py b/tests/test_library.py index 45521eb74..0bb2071f6 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -120,19 +120,14 @@ def test_library_fetchItem(plex, movie): assert item1 == item2 == movie -def test_library_fetchItems_with_maxresults(plex): - items1 = plex.library.search(libtype="episode") - assert len(items1) > 5 - size = len(items1) - 5 - ids = [item.key for item in items1] - # Reduce '/library/metadata/123' to '123' - int_ids = [int(id.rsplit("/", 1)[-1]) for id in ids] - keys1 = [item.key for item in plex.library.fetchItems(container_size=size, ekey=int_ids)] - keys2 = [item.key for item in plex.library.fetchItems( - container_size=size, ekey=int_ids, maxresults=len(int_ids) - )] - assert len(keys1) == len(set(keys1)) - assert len(keys2) == len(set(keys2)) +def test_library_fetchItems_with_maxresults(plex, tvshows): + items = tvshows.searchEpisodes() + assert len(items) > 5 + size = len(items) - 5 + ratingKeys = [item.ratingKey for item in items] + items1 = plex.fetchItems(ekey=ratingKeys, container_size=size) + items2 = plex.fetchItems(ekey=ratingKeys, container_size=size, maxresults=len(items)) + assert items1 == items2 == items def test_library_onDeck(plex, movie):