Skip to content

Commit 380fd78

Browse files
authored
Merge pull request #582 from pkkid/photo_download
Add download feature to Photo library
2 parents da11812 + 11dcf0b commit 380fd78

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

plexapi/photo.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ def photo(self, title):
7474
return photo
7575
raise NotFound('Unable to find photo: %s' % title)
7676

77+
def iterParts(self):
78+
""" Iterates over the parts of this media item. """
79+
for album in self.albums():
80+
for photo in album.photos():
81+
for part in photo.iterParts():
82+
yield part
83+
84+
def download(self, savepath=None, keep_original_name=False, showstatus=False):
85+
""" Download photo files to specified directory.
86+
87+
Parameters:
88+
savepath (str): Defaults to current working dir.
89+
keep_original_name (bool): True to keep the original file name otherwise
90+
a friendlier is generated.
91+
showstatus(bool): Display a progressbar.
92+
"""
93+
filepaths = []
94+
locations = [i for i in self.iterParts() if i]
95+
for location in locations:
96+
name = location.file
97+
if not keep_original_name:
98+
title = self.title.replace(' ', '.')
99+
name = '%s.%s' % (title, location.container)
100+
url = self._server.url('%s?download=1' % location.key)
101+
filepath = utils.download(url, self._server._token, filename=name, showstatus=showstatus,
102+
savepath=savepath, session=self._server._session)
103+
if filepath:
104+
filepaths.append(filepath)
105+
return filepaths
106+
77107

78108
@utils.registerPlexObject
79109
class Photo(PlexPartialObject):
@@ -137,6 +167,12 @@ def section(self):
137167
else:
138168
raise BadRequest('Unable to get section for photo, can`t find librarySectionID')
139169

170+
def iterParts(self):
171+
""" Iterates over the parts of this media item. """
172+
for item in self.media:
173+
for part in item.parts:
174+
yield part
175+
140176
def sync(self, resolution, client=None, clientId=None, limit=None, title=None):
141177
""" Add current photo as sync item for specified device.
142178
See :func:`plexapi.myplex.MyPlexAccount.sync()` for possible exceptions.
@@ -172,3 +208,26 @@ def sync(self, resolution, client=None, clientId=None, limit=None, title=None):
172208
sync_item.mediaSettings = MediaSettings.createPhoto(resolution)
173209

174210
return myplex.sync(sync_item, client=client, clientId=clientId)
211+
212+
def download(self, savepath=None, keep_original_name=False, showstatus=False):
213+
""" Download photo files to specified directory.
214+
215+
Parameters:
216+
savepath (str): Defaults to current working dir.
217+
keep_original_name (bool): True to keep the original file name otherwise
218+
a friendlier is generated.
219+
showstatus(bool): Display a progressbar.
220+
"""
221+
filepaths = []
222+
locations = [i for i in self.iterParts() if i]
223+
for location in locations:
224+
name = location.file
225+
if not keep_original_name:
226+
title = self.title.replace(' ', '.')
227+
name = '%s.%s' % (title, location.container)
228+
url = self._server.url('%s?download=1' % location.key)
229+
filepath = utils.download(url, self._server._token, filename=name, showstatus=showstatus,
230+
savepath=savepath, session=self._server._session)
231+
if filepath:
232+
filepaths.append(filepath)
233+
return filepaths

0 commit comments

Comments
 (0)