Skip to content

Commit 5f4b7e1

Browse files
authored
Merge pull request #384 from blacktwin/patch-8
upload, select, remove subtitles
2 parents 97903cf + b59aab1 commit 5f4b7e1

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

plexapi/video.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from plexapi.exceptions import BadRequest, NotFound
44
from plexapi.base import Playable, PlexPartialObject
55
from plexapi.compat import quote_plus
6+
import os
67

78

89
class Video(PlexPartialObject):
@@ -89,6 +90,37 @@ def _defaultSyncTitle(self):
8990
""" Returns str, default title for a new syncItem. """
9091
return self.title
9192

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+
92124
def posters(self):
93125
""" Returns list of available poster objects. :class:`~plexapi.media.Poster`:"""
94126

@@ -224,14 +256,6 @@ def locations(self):
224256
"""
225257
return [part.file for part in self.iterParts() if part]
226258

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-
235259
def _prettyfilename(self):
236260
# This is just for compat.
237261
return self.title

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
from functools import partial
88
from os import environ
99
from plexapi.myplex import MyPlexAccount
10+
11+
try:
12+
from unittest.mock import patch, MagicMock, mock_open
13+
except ImportError:
14+
from mock import patch, MagicMock, mock_open
15+
1016
from plexapi import compat
1117
from plexapi.compat import patch, MagicMock
1218
from plexapi.client import PlexClient
@@ -219,6 +225,14 @@ def photoalbum(photos):
219225
return photos.get('Cats')
220226
except Exception:
221227
return photos.get('photo_album1')
228+
229+
@pytest.fixture()
230+
def subtitle():
231+
mopen = mock_open()
232+
with patch('__main__.open', mopen):
233+
with open('subtitle.srt', 'w') as handler:
234+
handler.write('test')
235+
return handler
222236

223237

224238
@pytest.fixture()

tests/test_video.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ def test_video_Movie_download(monkeydownload, tmpdir, movie):
8383
def test_video_Movie_subtitlestreams(movie):
8484
assert not movie.subtitleStreams()
8585

86+
def test_video_Episode_subtitlestreams(episode):
87+
assert not episode.subtitleStreams()
88+
89+
def test_video_Movie_upload_select_remove_subtitle(movie, subtitle):
90+
import os
91+
filepath = os.path.realpath(subtitle.name)
92+
movie.uploadSubtitles(filepath)
93+
subtitles = [sub.title for sub in movie.subtitleStreams()]
94+
subname = subtitle.name.rsplit('.', 1)[0]
95+
assert subname in subtitles
96+
97+
subtitleSelection = movie.subtitleStreams()[0]
98+
parts = [part for part in movie.iterParts()]
99+
parts[0].setDefaultSubtitleStream(subtitleSelection)
100+
101+
subtitleSelection = movie.subtitleStreams()[0]
102+
assert subtitleSelection.selected
103+
104+
movie.removeSubtitles(streamTitle=subname)
105+
subtitles = [sub.title for sub in movie.subtitleStreams()]
106+
assert subname not in subtitles
107+
108+
if subtitle:
109+
os.remove(filepath)
86110

87111
def test_video_Movie_attrs(movies):
88112
movie = movies.get('Sita Sings the Blues')

0 commit comments

Comments
 (0)