From e5da3423e9d80660b2208f0007e7b7a1e87a0668 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Fri, 14 May 2021 16:23:13 -0700 Subject: [PATCH] Add duration in seconds to notification parameters Fixes #1434 --- plexpy/common.py | 4 ++++ plexpy/helpers.py | 15 +++++++++++++-- plexpy/notification_handler.py | 32 +++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/plexpy/common.py b/plexpy/common.py index 33d40f37..7572c7c2 100644 --- a/plexpy/common.py +++ b/plexpy/common.py @@ -402,10 +402,13 @@ NOTIFICATION_PARAMETERS = [ {'name': 'Initial Stream', 'type': 'int', 'value': 'initial_stream', 'description': 'If the stream is the initial stream of a continuous streaming session.', 'example': '0 or 1'}, {'name': 'IP Address', 'type': 'str', 'value': 'ip_address', 'description': 'The IP address of the device being used for playback.'}, {'name': 'Stream Duration', 'type': 'int', 'value': 'stream_duration', 'description': 'The duration (in minutes) for the stream.'}, + {'name': 'Stream Duration (sec)', 'type': 'int', 'value': 'stream_duration_sec', 'description': 'The duration (in seconds) for the stream.'}, {'name': 'Stream Time', 'type': 'str', 'value': 'stream_time', 'description': 'The duration (in time format) of the stream.'}, {'name': 'Remaining Duration', 'type': 'int', 'value': 'remaining_duration', 'description': 'The remaining duration (in minutes) of the stream.'}, + {'name': 'Remaining Duration (sec)', 'type': 'int', 'value': 'remaining_duration_sec', 'description': 'The remaining duration (in seconds) of the stream.'}, {'name': 'Remaining Time', 'type': 'str', 'value': 'remaining_time', 'description': 'The remaining duration (in time format) of the stream.'}, {'name': 'Progress Duration', 'type': 'int', 'value': 'progress_duration', 'description': 'The last reported offset (in minutes) of the stream.'}, + {'name': 'Progress Duration (sec)', 'type': 'int', 'value': 'progress_duration_sec', 'description': 'The last reported offset (in seconds) of the stream.'}, {'name': 'Progress Time', 'type': 'str', 'value': 'progress_time', 'description': 'The last reported offset (in time format) of the stream.'}, {'name': 'Progress Percent', 'type': 'int', 'value': 'progress_percent', 'description': 'The last reported progress percent of the stream.'}, {'name': 'Transcode Decision', 'type': 'str', 'value': 'transcode_decision', 'description': 'The transcode decision of the stream.'}, @@ -527,6 +530,7 @@ NOTIFICATION_PARAMETERS = [ {'name': 'Audience Rating', 'type': 'float', 'value': 'audience_rating', 'description': 'The audience rating for the item.', 'help_text': 'Rating out of 10 for IMDB, percentage (%) for Rotten Tomatoes and TMDB.'}, {'name': 'User Rating', 'type': 'float', 'value': 'user_rating', 'description': 'The user (star) rating (out of 10) for the item.'}, {'name': 'Duration', 'type': 'int', 'value': 'duration', 'description': 'The duration (in minutes) for the item.'}, + {'name': 'Duration (sec)', 'type': 'int', 'value': 'duration_sec', 'description': 'The duration (in seconds) for the item.'}, {'name': 'Poster URL', 'type': 'str', 'value': 'poster_url', 'description': 'A URL for the movie, TV show, or album poster.'}, {'name': 'Plex ID', 'type': 'str', 'value': 'plex_id', 'description': 'The Plex ID for the item.', 'example': 'e.g. 5d7769a9594b2b001e6a6b7e'}, {'name': 'Plex URL', 'type': 'str', 'value': 'plex_url', 'description': 'The Plex URL to your server for the item.'}, diff --git a/plexpy/helpers.py b/plexpy/helpers.py index eada9343..9e844aa9 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -172,14 +172,25 @@ def convert_milliseconds(ms): return minutes -def convert_milliseconds_to_minutes(ms): +def convert_milliseconds_to_seconds(ms): + if str(ms).isdigit(): + seconds = float(ms) / 1000 + return math.trunc(seconds) + return 0 + +def convert_milliseconds_to_minutes(ms): if str(ms).isdigit(): seconds = float(ms) / 1000 minutes = round(seconds / 60, 0) - return math.trunc(minutes) + return 0 + +def seconds_to_minutes(s): + if str(s).isdigit(): + minutes = round(s / 60, 0) + return math.trunc(minutes) return 0 diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 4cd3dd25..efb8bdec 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -581,14 +581,24 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m user_transcode_decision_count = Counter(s['transcode_decision'] for s in user_sessions) if notify_action != 'on_play': - stream_duration = int((time.time() - - helpers.cast_to_int(session.get('started', 0)) - - helpers.cast_to_int(session.get('paused_counter', 0))) / 60) + stream_duration_sec = int( + ( + helpers.timestamp() + - helpers.cast_to_int(session.get('started', 0)) + - helpers.cast_to_int(session.get('paused_counter', 0)) + ) + ) + stream_duration = helpers.seconds_to_minutes(stream_duration_sec) else: + stream_duration_sec = 0 stream_duration = 0 - view_offset = helpers.convert_milliseconds_to_minutes(session.get('view_offset', 0)) - duration = helpers.convert_milliseconds_to_minutes(notify_params['duration']) + view_offset_sec = helpers.convert_milliseconds_to_seconds(session.get('view_offset', 0)) + duration_sec = helpers.convert_milliseconds_to_seconds(notify_params['duration']) + remaining_duration_sec = duration_sec - view_offset_sec + + view_offset = helpers.seconds_to_minutes(view_offset_sec) + duration = helpers.seconds_to_minutes(duration_sec) remaining_duration = duration - view_offset # Build Plex URL @@ -899,12 +909,15 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m 'player': notify_params['player'], 'ip_address': notify_params.get('ip_address', 'N/A'), 'stream_duration': stream_duration, - 'stream_time': arrow.get(stream_duration * 60).format(duration_format), + 'stream_duration_sec': stream_duration_sec, + 'stream_time': arrow.get(stream_duration_sec).format(duration_format), 'remaining_duration': remaining_duration, - 'remaining_time': arrow.get(remaining_duration * 60).format(duration_format), + 'remaining_duration_sec': remaining_duration_sec, + 'remaining_time': arrow.get(remaining_duration_sec).format(duration_format), 'progress_duration': view_offset, - 'progress_time': arrow.get(view_offset * 60).format(duration_format), - 'progress_percent': helpers.get_percent(view_offset, duration), + 'progress_duration_sec': view_offset_sec, + 'progress_time': arrow.get(view_offset_sec).format(duration_format), + 'progress_percent': helpers.get_percent(view_offset_sec, duration_sec), 'initial_stream': notify_params['initial_stream'], 'transcode_decision': transcode_decision, 'container_decision': notify_params['container_decision'], @@ -1027,6 +1040,7 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m 'audience_rating': audience_rating, 'user_rating': notify_params['user_rating'], 'duration': duration, + 'duration_sec': duration_sec, 'poster_title': notify_params['poster_title'], 'poster_url': notify_params['poster_url'], 'plex_id': notify_params['plex_id'],