From 717b274d8296125042e0dc114411d3b7dc76a290 Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 10 Oct 2020 14:55:16 -0400 Subject: [PATCH 1/3] add iterParts() method to Photo class --- plexapi/photo.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plexapi/photo.py b/plexapi/photo.py index a8fc1c703..0234b0319 100644 --- a/plexapi/photo.py +++ b/plexapi/photo.py @@ -137,6 +137,12 @@ def section(self): else: raise BadRequest('Unable to get section for photo, can`t find librarySectionID') + def iterParts(self): + """ Iterates over the parts of this media item. """ + for item in self.media: + for part in item.parts: + yield part + def sync(self, resolution, client=None, clientId=None, limit=None, title=None): """ Add current photo as sync item for specified device. See :func:`plexapi.myplex.MyPlexAccount.sync()` for possible exceptions. From 0e8b2e88db2bd8e1626fb382fd7d4d93bdbffd75 Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 10 Oct 2020 14:55:49 -0400 Subject: [PATCH 2/3] add download() method to Photo class --- plexapi/photo.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plexapi/photo.py b/plexapi/photo.py index 0234b0319..ff7e848a3 100644 --- a/plexapi/photo.py +++ b/plexapi/photo.py @@ -178,3 +178,26 @@ def sync(self, resolution, client=None, clientId=None, limit=None, title=None): sync_item.mediaSettings = MediaSettings.createPhoto(resolution) return myplex.sync(sync_item, client=client, clientId=clientId) + + def download(self, savepath=None, keep_original_name=False, showstatus=False): + """ Download photo files to specified directory. + + Parameters: + savepath (str): Defaults to current working dir. + keep_original_name (bool): True to keep the original file name otherwise + a friendlier is generated. + showstatus(bool): Display a progressbar. + """ + filepaths = [] + locations = [i for i in self.iterParts() if i] + for location in locations: + name = location.file + if not keep_original_name: + title = self.title.replace(' ', '.') + name = '%s.%s' % (title, location.container) + url = self._server.url('%s?download=1' % location.key) + filepath = utils.download(url, self._server._token, filename=name, showstatus=showstatus, + savepath=savepath, session=self._server._session) + if filepath: + filepaths.append(filepath) + return filepaths From 411018dd37fe55211eab03188aebbef55f336c06 Mon Sep 17 00:00:00 2001 From: blacktwin Date: Sat, 10 Oct 2020 15:00:19 -0400 Subject: [PATCH 3/3] add iterParts() and download() methods to Photoalbum class --- plexapi/photo.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/plexapi/photo.py b/plexapi/photo.py index ff7e848a3..82bca65ba 100644 --- a/plexapi/photo.py +++ b/plexapi/photo.py @@ -74,6 +74,36 @@ def photo(self, title): return photo raise NotFound('Unable to find photo: %s' % title) + def iterParts(self): + """ Iterates over the parts of this media item. """ + for album in self.albums(): + for photo in album.photos(): + for part in photo.iterParts(): + yield part + + def download(self, savepath=None, keep_original_name=False, showstatus=False): + """ Download photo files to specified directory. + + Parameters: + savepath (str): Defaults to current working dir. + keep_original_name (bool): True to keep the original file name otherwise + a friendlier is generated. + showstatus(bool): Display a progressbar. + """ + filepaths = [] + locations = [i for i in self.iterParts() if i] + for location in locations: + name = location.file + if not keep_original_name: + title = self.title.replace(' ', '.') + name = '%s.%s' % (title, location.container) + url = self._server.url('%s?download=1' % location.key) + filepath = utils.download(url, self._server._token, filename=name, showstatus=showstatus, + savepath=savepath, session=self._server._session) + if filepath: + filepaths.append(filepath) + return filepaths + @utils.registerPlexObject class Photo(PlexPartialObject):