Set view offset equal to duration if stopped within the last 10 sec

* Plex reports the view offset every 10 seconds, so the view offset at the end of stream can be short by up to 10 seconds.
This commit is contained in:
JonnyWong16 2023-04-17 12:50:52 -07:00
parent e9b1db139e
commit 3b3c59c4bb
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 22 additions and 11 deletions

View file

@ -108,7 +108,6 @@ class ActivityHandler(object):
self.ap.write_session(session=self.session, notify=notify)
self.set_session_state()
self.get_db_session()
def set_session_state(self, view_offset=None):
self.ap.set_session_state(
@ -117,6 +116,7 @@ class ActivityHandler(object):
view_offset=view_offset or self.view_offset,
stopped=helpers.timestamp()
)
self.get_db_session()
def put_notification(self, notify_action, **kwargs):
notification = {'stream_data': self.db_session.copy(), 'notify_action': notify_action}
@ -162,7 +162,12 @@ class ActivityHandler(object):
# Update the session state and viewOffset
# Set force_stop to true to disable the state set
if not force_stop:
self.set_session_state()
# Set the view offset equal to the duration if it is within the last 10 seconds
if self.db_session['duration'] - self.view_offset <= 10000:
view_offset = self.db_session['duration']
else:
view_offset = self.view_offset
self.set_session_state(view_offset=view_offset)
# Write it to the history table
row_id = self.ap.write_session_history(session=self.db_session)

View file

@ -550,7 +550,13 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m
if session:
# Reload json from raw stream info
if session.get('raw_stream_info'):
session.update(json.loads(session['raw_stream_info']))
raw_stream_info = json.loads(session['raw_stream_info'])
# Don't overwrite id, session_key, stopped, view_offset
raw_stream_info.pop('id', None)
raw_stream_info.pop('session_key', None)
raw_stream_info.pop('stopped', None)
raw_stream_info.pop('view_offset', None)
session.update(raw_stream_info)
notify_params.update(session)
if timeline:
@ -638,13 +644,13 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m
stream_duration_sec = 0
stream_duration = 0
view_offset_sec = helpers.convert_milliseconds_to_seconds(session.get('view_offset', 0))
progress_duration_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
remaining_duration_sec = duration_sec - progress_duration_sec
view_offset = helpers.seconds_to_minutes(view_offset_sec)
progress_duration = helpers.seconds_to_minutes(progress_duration_sec)
duration = helpers.seconds_to_minutes(duration_sec)
remaining_duration = duration - view_offset
remaining_duration = duration - progress_duration
# Build Plex URL
if notify_params['media_type'] == 'track':
@ -1005,10 +1011,10 @@ def build_media_notify_params(notify_action=None, session=None, timeline=None, m
'remaining_duration': remaining_duration,
'remaining_duration_sec': remaining_duration_sec,
'remaining_time': arrow.get(remaining_duration_sec).format(duration_format),
'progress_duration': view_offset,
'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),
'progress_duration': progress_duration,
'progress_duration_sec': progress_duration_sec,
'progress_time': arrow.get(progress_duration_sec).format(duration_format),
'progress_percent': helpers.get_percent(progress_duration_sec, duration_sec),
'view_offset': session.get('view_offset', 0),
'initial_stream': notify_params['initial_stream'],
'transcode_decision': transcode_decision,