Skip to content

Commit a3178f3

Browse files
authored
Merge pull request #831 from JonnyWong16/feature/includeGuids
Automatically retrieve external guids from libraries using includeGuids
2 parents 3fce245 + 105a156 commit a3178f3

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

plexapi/library.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,45 @@ def get(self, title):
456456
457457
Parameters:
458458
title (str): Title of the item to return.
459+
460+
Raises:
461+
:exc:`~plexapi.exceptions.NotFound`: The title is not found in the library.
459462
"""
460-
key = '/library/sections/%s/all?title=%s' % (self.key, quote(title, safe=''))
463+
key = '/library/sections/%s/all?includeGuids=1&title=%s' % (self.key, quote(title, safe=''))
461464
return self.fetchItem(key, title__iexact=title)
462465

466+
def getGuid(self, guid):
467+
""" Returns the media item with the specified external IMDB, TMDB, or TVDB ID.
468+
Note: This search uses a PlexAPI operator so performance may be slow. All items from the
469+
entire Plex library need to be retrieved for each guid search. It is recommended to create
470+
your own lookup dictionary if you are searching for a lot of external guids.
471+
472+
Parameters:
473+
guid (str): The external guid of the item to return.
474+
Examples: IMDB ``imdb://tt0944947``, TMDB ``tmdb://1399``, TVDB ``tvdb://121361``.
475+
476+
Raises:
477+
:exc:`~plexapi.exceptions.NotFound`: The guid is not found in the library.
478+
479+
Example:
480+
481+
.. code-block:: python
482+
483+
# This will retrieve all items in the entire library 3 times
484+
result1 = library.getGuid('imdb://tt0944947')
485+
result2 = library.getGuid('tmdb://1399')
486+
result3 = library.getGuid('tvdb://121361')
487+
488+
# This will only retrieve all items in the library once to create a lookup dictionary
489+
guidLookup = {guid.id: item for item in library.all() for guid in item.guids}
490+
result1 = guidLookup['imdb://tt0944947']
491+
result2 = guidLookup['tmdb://1399']
492+
result3 = guidLookup['tvdb://121361']
493+
494+
"""
495+
key = '/library/sections/%s/all?includeGuids=1' % self.key
496+
return self.fetchItem(key, Guid__id__iexact=guid)
497+
463498
def all(self, libtype=None, **kwargs):
464499
""" Returns a list of all items from this library section.
465500
See description of :func:`~plexapi.library.LibrarySection.search()` for details about filtering / sorting.
@@ -979,6 +1014,8 @@ def _buildSearchKey(self, title=None, sort=None, libtype=None, limit=None, filte
9791014
"""
9801015
args = {}
9811016
filter_args = []
1017+
1018+
args['includeGuids'] = int(bool(kwargs.pop('includeGuids', True)))
9821019
for field, values in list(kwargs.items()):
9831020
if field.split('__')[-1] not in OPERATORS:
9841021
filter_args.append(self._validateFilterField(field, values, libtype))

plexapi/mixins.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,9 @@ def _parseFilters(self, content):
601601
key += '='
602602
value = value[1:]
603603

604-
if key == 'type':
604+
if key == 'includeGuids':
605+
filters['includeGuids'] = int(value)
606+
elif key == 'type':
605607
filters['libtype'] = utils.reverseSearchType(value)
606608
elif key == 'sort':
607609
filters['sort'] = value.split(',')

tests/test_library.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from urllib.parse import quote_plus
55

66
import pytest
7+
import plexapi.base
78
from plexapi.exceptions import BadRequest, NotFound
89

910
from . import conftest as utils
@@ -54,12 +55,30 @@ def test_library_section_get_movie(movies):
5455
assert movies.get("Sita Sings the Blues")
5556

5657

58+
def test_library_MovieSection_getGuid(movies, movie):
59+
result = movies.getGuid(guid=movie.guids[0].id)
60+
assert result == movie
61+
62+
5763
def test_library_section_movies_all(movies):
5864
# size should always be none unless pagenation is being used.
5965
assert movies.totalSize == 4
6066
assert len(movies.all(container_start=0, container_size=1, maxresults=1)) == 1
6167

6268

69+
def test_library_section_movies_all_guids(movies):
70+
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.add('guids')
71+
try:
72+
results = movies.all(includeGuids=False)
73+
assert results[0].guids == []
74+
results = movies.all()
75+
assert results[0].guids
76+
movie = movies.get("Sita Sings the Blues")
77+
assert movie.guids
78+
finally:
79+
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.remove('guids')
80+
81+
6382
def test_library_section_totalViewSize(tvshows):
6483
assert tvshows.totalViewSize() == 2
6584
assert tvshows.totalViewSize(libtype="show") == 2

0 commit comments

Comments
 (0)