|
3 | 3 | from plexapi.exceptions import BadRequest, NotFound |
4 | 4 | from plexapi.base import Playable, PlexPartialObject |
5 | 5 | from plexapi.compat import quote_plus |
| 6 | +import os |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class Video(PlexPartialObject): |
@@ -89,6 +90,37 @@ def _defaultSyncTitle(self): |
89 | 90 | """ Returns str, default title for a new syncItem. """ |
90 | 91 | return self.title |
91 | 92 |
|
| 93 | + def subtitleStreams(self): |
| 94 | + """ Returns a list of :class:`~plexapi.media.SubtitleStream` objects for all MediaParts. """ |
| 95 | + streams = [] |
| 96 | + |
| 97 | + parts = self.iterParts() |
| 98 | + for part in parts: |
| 99 | + streams += part.subtitleStreams() |
| 100 | + return streams |
| 101 | + |
| 102 | + def uploadSubtitles(self, filepath): |
| 103 | + """ Upload Subtitle file for video. """ |
| 104 | + url = '%s/subtitles' % self.key |
| 105 | + filename = os.path.basename(filepath) |
| 106 | + subFormat = os.path.splitext(filepath)[1][1:] |
| 107 | + with open(filepath, 'rb') as subfile: |
| 108 | + params = {'title': filename, |
| 109 | + 'format': subFormat |
| 110 | + } |
| 111 | + headers = {'Accept': 'text/plain, */*'} |
| 112 | + self._server.query(url, self._server._session.post, data=subfile, params=params, headers=headers) |
| 113 | + |
| 114 | + def removeSubtitles(self, streamID=None, streamTitle=None): |
| 115 | + """ Remove Subtitle from movie's subtitles listing. |
| 116 | +
|
| 117 | + Note: If subtitle file is located inside video directory it will bbe deleted. |
| 118 | + Files outside of video directory are not effected. |
| 119 | + """ |
| 120 | + for stream in self.subtitleStreams(): |
| 121 | + if streamID == stream.id or streamTitle == stream.title: |
| 122 | + self._server.query(stream.key, self._server._session.delete) |
| 123 | + |
92 | 124 | def posters(self): |
93 | 125 | """ Returns list of available poster objects. :class:`~plexapi.media.Poster`:""" |
94 | 126 |
|
@@ -224,14 +256,6 @@ def locations(self): |
224 | 256 | """ |
225 | 257 | return [part.file for part in self.iterParts() if part] |
226 | 258 |
|
227 | | - def subtitleStreams(self): |
228 | | - """ Returns a list of :class:`~plexapi.media.SubtitleStream` objects for all MediaParts. """ |
229 | | - streams = [] |
230 | | - for elem in self.media: |
231 | | - for part in elem.parts: |
232 | | - streams += part.subtitleStreams() |
233 | | - return streams |
234 | | - |
235 | 259 | def _prettyfilename(self): |
236 | 260 | # This is just for compat. |
237 | 261 | return self.title |
|
0 commit comments