Skip to content

Commit d2c849f

Browse files
authored
Improve getGuid method (#862)
* Improve getGuid method * Update getGuid test
1 parent fcbea70 commit d2c849f

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

plexapi/library.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,7 @@ def get(self, title):
578578

579579
def getGuid(self, guid):
580580
""" Returns the media item with the specified external IMDB, TMDB, or TVDB ID.
581-
Note: This search uses a PlexAPI operator so performance may be slow. All items from the
582-
entire Plex library need to be retrieved for each guid search. It is recommended to create
583-
your own lookup dictionary if you are searching for a lot of external guids.
581+
Note: Only available for the Plex Movie and Plex TV Series agents.
584582
585583
Parameters:
586584
guid (str): The external guid of the item to return.
@@ -593,20 +591,23 @@ def getGuid(self, guid):
593591
594592
.. code-block:: python
595593
596-
# This will retrieve all items in the entire library 3 times
597594
result1 = library.getGuid('imdb://tt0944947')
598595
result2 = library.getGuid('tmdb://1399')
599596
result3 = library.getGuid('tvdb://121361')
600597
601-
# This will only retrieve all items in the library once to create a lookup dictionary
598+
# Alternatively, create your own guid lookup dictionary for faster performance
602599
guidLookup = {guid.id: item for item in library.all() for guid in item.guids}
603600
result1 = guidLookup['imdb://tt0944947']
604601
result2 = guidLookup['tmdb://1399']
605602
result3 = guidLookup['tvdb://121361']
606603
607604
"""
608-
key = '/library/sections/%s/all?includeGuids=1' % self.key
609-
return self.fetchItem(key, Guid__id__iexact=guid)
605+
try:
606+
dummy = self.search(maxresults=1)[0]
607+
match = dummy.matches(agent=self.agent, title=guid.replace('://', '-'))
608+
return self.search(guid=match[0].guid)[0]
609+
except IndexError:
610+
raise NotFound("Guid '%s' is not found in the library" % guid) from None
610611

611612
def all(self, libtype=None, **kwargs):
612613
""" Returns a list of all items from this library section.

tests/test_library.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def test_library_section_get_movie(movies):
6060
def test_library_MovieSection_getGuid(movies, movie):
6161
result = movies.getGuid(guid=movie.guids[0].id)
6262
assert result == movie
63+
with pytest.raises(NotFound):
64+
movies.getGuid(guid='imdb://tt00000000')
6365

6466

6567
def test_library_section_movies_all(movies):

0 commit comments

Comments
 (0)