From 067981620104342b279fa3bf7edd717c0b7e5530 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 27 Aug 2023 12:41:37 -0700 Subject: [PATCH] Rename "setDefault" to "setSelected" for media streams (#1206) * Rename "setDefault" to "setSelected" * Update tests for setSelected subtitle stream --- plexapi/media.py | 73 +++++++++++++++++++++++++++++++++------------ tests/test_video.py | 2 +- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/plexapi/media.py b/plexapi/media.py index 8793463f..0ea4e345 100644 --- a/plexapi/media.py +++ b/plexapi/media.py @@ -121,6 +121,7 @@ class MediaPart(PlexObject): optimizedForStreaming (bool): True if the file is optimized for streaming. packetLength (int): The packet length of the file. requiredBandwidths (str): The required bandwidths to stream the file. + selected (bool): True if this media part is selected. size (int): The size of the file in bytes (ex: 733884416). streams (List<:class:`~plexapi.media.MediaPartStream`>): List of stream objects. syncItemId (int): The unique ID for this media part if it is synced. @@ -184,38 +185,60 @@ class MediaPart(PlexObject): """ Returns a list of :class:`~plexapi.media.LyricStream` objects in this MediaPart. """ return [stream for stream in self.streams if isinstance(stream, LyricStream)] - def setDefaultAudioStream(self, stream): - """ Set the default :class:`~plexapi.media.AudioStream` for this MediaPart. + def setSelectedAudioStream(self, stream): + """ Set the selected :class:`~plexapi.media.AudioStream` for this MediaPart. Parameters: - stream (:class:`~plexapi.media.AudioStream`): AudioStream to set as default + stream (:class:`~plexapi.media.AudioStream`): Audio stream to set as selected """ + key = f'/library/parts/{self.id}' + params = {'allParts': 1} + if isinstance(stream, AudioStream): - key = f"/library/parts/{self.id}?audioStreamID={stream.id}&allParts=1" + params['audioStreamID'] = stream.id else: - key = f"/library/parts/{self.id}?audioStreamID={stream}&allParts=1" - self._server.query(key, method=self._server._session.put) + params['audioStreamID'] = stream + + self._server.query(key, method=self._server._session.put, params=params) return self - def setDefaultSubtitleStream(self, stream): - """ Set the default :class:`~plexapi.media.SubtitleStream` for this MediaPart. + def setSelectedSubtitleStream(self, stream): + """ Set the selected :class:`~plexapi.media.SubtitleStream` for this MediaPart. Parameters: - stream (:class:`~plexapi.media.SubtitleStream`): SubtitleStream to set as default. + stream (:class:`~plexapi.media.SubtitleStream`): Subtitle stream to set as selected. """ + key = f'/library/parts/{self.id}' + params = {'allParts': 1} + if isinstance(stream, SubtitleStream): - key = f"/library/parts/{self.id}?subtitleStreamID={stream.id}&allParts=1" + params['subtitleStreamID'] = stream.id else: - key = f"/library/parts/{self.id}?subtitleStreamID={stream}&allParts=1" + params['subtitleStreamID'] = stream + self._server.query(key, method=self._server._session.put) return self - def resetDefaultSubtitleStream(self): - """ Set default subtitle of this MediaPart to 'none'. """ - key = f"/library/parts/{self.id}?subtitleStreamID=0&allParts=1" - self._server.query(key, method=self._server._session.put) + def resetSelectedSubtitleStream(self): + """ Set the selected subtitle of this MediaPart to 'None'. """ + key = f'/library/parts/{self.id}' + params = {'subtitleStreamID': 0, 'allParts': 1} + + self._server.query(key, method=self._server._session.put, params=params) return self + @deprecated('Use "setSelectedAudioStream" instead.') + def setDefaultAudioStream(self, stream): + return self.setSelectedAudioStream(stream) + + @deprecated('Use "setSelectedSubtitleStream" instead.') + def setDefaultSubtitleStream(self, stream): + return self.setSelectedSubtitleStream(stream) + + @deprecated('Use "resetSelectedSubtitleStream" instead.') + def resetDefaultSubtitleStream(self): + return self.resetSelectedSubtitleStream() + class MediaPartStream(PlexObject): """ Base class for media streams. These consist of video, audio, subtitles, and lyrics. @@ -399,9 +422,15 @@ class AudioStream(MediaPartStream): self.peak = utils.cast(float, data.attrib.get('peak')) self.startRamp = data.attrib.get('startRamp') + def setSelected(self): + """ Sets this audio stream as the selected audio stream. + Alias for :func:`~plexapi.media.MediaPart.setSelectedAudioStream`. + """ + return self._parent().setSelectedAudioStream(self) + + @deprecated('Use "setSelected" instead.') def setDefault(self): - """ Sets this audio stream as the default audio stream. """ - return self._parent().setDefaultAudioStream(self) + return self.setSelected() @utils.registerPlexObject @@ -437,9 +466,15 @@ class SubtitleStream(MediaPartStream): self.transient = data.attrib.get('transient') self.userID = utils.cast(int, data.attrib.get('userID')) + def setSelected(self): + """ Sets this subtitle stream as the selected subtitle stream. + Alias for :func:`~plexapi.media.MediaPart.setSelectedSubtitleStream`. + """ + return self._parent().setSelectedSubtitleStream(self) + + @deprecated('Use "setSelected" instead.') def setDefault(self): - """ Sets this subtitle stream as the default subtitle stream. """ - return self._parent().setDefaultSubtitleStream(self) + return self.setSelected() class LyricStream(MediaPartStream): diff --git a/tests/test_video.py b/tests/test_video.py index fd9f962a..a760dd4f 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -405,7 +405,7 @@ def test_video_Movie_upload_select_remove_subtitle(movie, subtitle): subname = subtitle.name.rsplit(".", 1)[0] assert subname in subtitles - movie.subtitleStreams()[0].setDefault() + movie.subtitleStreams()[0].setSelected() movie.reload() subtitleSelection = movie.subtitleStreams()[0]