From e567134ee1bfd7a2c389f1d12280b150e2861674 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Fri, 6 Jul 2018 19:41:03 -0700 Subject: [PATCH] Use default selected stream for media info in notifications --- plexpy/libraries.py | 3 ++- plexpy/notification_handler.py | 28 ++++++++++++++++------------ plexpy/pmsconnect.py | 12 ++++++++---- plexpy/webserve.py | 9 ++++++--- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/plexpy/libraries.py b/plexpy/libraries.py index cee7b368..cdf48910 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -633,7 +633,8 @@ class Libraries(object): if 'media_info' in child_metadata and len(child_metadata['media_info']) > 0: media_info = child_metadata['media_info'][0] if 'parts' in media_info and len (media_info['parts']) > 0: - media_part_info = media_info['parts'][0] + media_part_info = next((p for p in media_info['parts'] if p['selected']), + media_info['parts'][0]) file_size += helpers.cast_to_int(media_part_info.get('file_size', 0)) diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 16ff6054..8c778556 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -486,20 +486,24 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m if 'media_info' in notify_params and len(notify_params['media_info']) > 0: media_info = notify_params['media_info'][0] if 'parts' in media_info and len(media_info['parts']) > 0: - media_part_info = media_info.pop('parts')[0] + parts = media_info.pop('parts') + media_part_info = next((p for p in parts if p['selected']), parts[0]) - stream_video = stream_audio = stream_subtitle = False if 'streams' in media_part_info: - for stream in media_part_info.pop('streams'): - if not stream_video and stream['type'] == '1': - media_part_info.update(stream) - stream_video = True - if not stream_audio and stream['type'] == '2': - media_part_info.update(stream) - stream_audio = True - if not stream_subtitle and stream['type'] == '3': - media_part_info.update(stream) - stream_subtitle = True + streams = media_part_info.pop('streams') + video_streams = [s for s in streams if s['type'] == '1'] + audio_streams = [s for s in streams if s['type'] == '2'] + subtitle_streams = [s for s in streams if s['type'] == '3'] + + if video_streams: + video_stream = next((s for s in video_streams if s['selected']), video_streams[0]) + media_part_info.update(video_stream) + if audio_streams: + audio_stream = next((s for s in audio_streams if s['selected']), audio_streams[0]) + media_part_info.update(audio_stream) + if subtitle_streams: + subtitle_stream = next((s for s in subtitle_streams if s['selected']), subtitle_streams[0]) + media_part_info.update(subtitle_stream) notify_params.update(media_info) notify_params.update(media_part_info) diff --git a/plexpy/pmsconnect.py b/plexpy/pmsconnect.py index 6674bf40..3c589e27 100644 --- a/plexpy/pmsconnect.py +++ b/plexpy/pmsconnect.py @@ -1213,7 +1213,8 @@ class PmsConnect(object): 'video_width': helpers.get_xml_attr(stream, 'width'), 'video_language': helpers.get_xml_attr(stream, 'language'), 'video_language_code': helpers.get_xml_attr(stream, 'languageCode'), - 'video_profile': helpers.get_xml_attr(stream, 'profile') + 'video_profile': helpers.get_xml_attr(stream, 'profile'), + 'selected': int(helpers.get_xml_attr(stream, 'selected') == '1') }) elif helpers.get_xml_attr(stream, 'streamType') == '2': @@ -1227,7 +1228,8 @@ class PmsConnect(object): 'audio_sample_rate': helpers.get_xml_attr(stream, 'samplingRate'), 'audio_language': helpers.get_xml_attr(stream, 'language'), 'audio_language_code': helpers.get_xml_attr(stream, 'languageCode'), - 'audio_profile': helpers.get_xml_attr(stream, 'profile') + 'audio_profile': helpers.get_xml_attr(stream, 'profile'), + 'selected': int(helpers.get_xml_attr(stream, 'selected') == '1') }) elif helpers.get_xml_attr(stream, 'streamType') == '3': @@ -1239,14 +1241,16 @@ class PmsConnect(object): 'subtitle_forced': int(helpers.get_xml_attr(stream, 'forced') == '1'), 'subtitle_location': 'external' if helpers.get_xml_attr(stream, 'key') else 'embedded', 'subtitle_language': helpers.get_xml_attr(stream, 'language'), - 'subtitle_language_code': helpers.get_xml_attr(stream, 'languageCode') + 'subtitle_language_code': helpers.get_xml_attr(stream, 'languageCode'), + 'selected': int(helpers.get_xml_attr(stream, 'selected') == '1') }) parts.append({'id': helpers.get_xml_attr(part, 'id'), 'file': helpers.get_xml_attr(part, 'file'), 'file_size': helpers.get_xml_attr(part, 'size'), 'indexes': int(helpers.get_xml_attr(part, 'indexes') == 'sd'), - 'streams': streams + 'streams': streams, + 'selected': int(helpers.get_xml_attr(part, 'selected') == '1') }) audio_channels = helpers.get_xml_attr(media, 'audioChannels') diff --git a/plexpy/webserve.py b/plexpy/webserve.py index bca79e90..d893201e 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -4614,7 +4614,8 @@ class WebInterface(object): "video_language_code": "", "video_profile": "high", "video_ref_frames": "4", - "video_width": "1920" + "video_width": "1920", + "selected": 0 }, { "audio_bitrate": "384", @@ -4627,7 +4628,8 @@ class WebInterface(object): "audio_profile": "", "audio_sample_rate": "48000", "id": "511664", - "type": "2" + "type": "2", + "selected": 1 }, { "id": "511953", @@ -4638,7 +4640,8 @@ class WebInterface(object): "subtitle_language": "English", "subtitle_language_code": "eng", "subtitle_location": "external", - "type": "3" + "type": "3", + "selected": 1 } ] }