From 79ebe41385260e47674376df7750147bcfdfaf21 Mon Sep 17 00:00:00 2001 From: chasa <79016507+itschasa@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:49:10 +0100 Subject: [PATCH] create helper function and apply to routes --- plexpy/helpers.py | 21 +++++++++++++++++++++ plexpy/webserve.py | 21 ++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/plexpy/helpers.py b/plexpy/helpers.py index 085dfc12..8446e3a6 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -1793,3 +1793,24 @@ def get_ipv6_network_address(ip: str) -> str: if cidr_pattern.match(plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR): cidr = plexpy.CONFIG.NOTIFY_CONCURRENT_IPV6_CIDR return str(ip_network(ip+cidr, strict=False).network_address) + + +def clean_plexapi_sessions(session: dict) -> dict: + "Cleans Plex session data, removing the 10Gbps bandwidth/0.064Mbps quality bug that appears when video transcoding sometimes." + + if isinstance(session, dict): + # Reliable way to identify if a session is bugged. + if session['bandwidth'] == "10000000" and session['quality_profile'] == '0.064 Mbps': + + # Bitrate is unaffected by the bug, use bitrate to estimate the bandwidth reserve. + session['bandwidth'] = str(int(int(session['bitrate']) * 1.5)) + + # Convert bitrate to a human readable quality profile. + if int(session['bitrate']) > 1000000: + session['quality_profile'] = f"{int(session['bitrate']) / 1000000:.1f} Gbps" + elif int(session['bitrate']) > 1000: + session['quality_profile'] = f"{int(session['bitrate']) / 1000:.1f} Mbps" + else: + session['quality_profile'] = f"{int(session['bitrate'])} kbps" + + return session diff --git a/plexpy/webserve.py b/plexpy/webserve.py index b643f84b..2fb3247a 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -352,7 +352,7 @@ class WebInterface(object): if result: session = next((s for s in result['sessions'] if s['session_key'] == session_key), None) - return serve_template(template_name="current_activity_instance.html", session=session) + return serve_template(template_name="current_activity_instance.html", session=helpers.clean_plexapi_sessions(session)) else: return serve_template(template_name="current_activity_instance.html", session=None) @@ -6049,19 +6049,26 @@ class WebInterface(object): 'lan_bandwidth': 0, 'wan_bandwidth': 0} + cleaned_sessions = [] + for s in result['sessions']: - if s['transcode_decision'] == 'transcode': + s_cleaned = helpers.clean_plexapi_sessions(s) + cleaned_sessions.append(s_cleaned) + + if s_cleaned['transcode_decision'] == 'transcode': counts['stream_count_transcode'] += 1 - elif s['transcode_decision'] == 'copy': + elif s_cleaned['transcode_decision'] == 'copy': counts['stream_count_direct_stream'] += 1 else: counts['stream_count_direct_play'] += 1 - counts['total_bandwidth'] += helpers.cast_to_int(s['bandwidth']) - if s['location'] == 'lan': - counts['lan_bandwidth'] += helpers.cast_to_int(s['bandwidth']) + counts['total_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth']) + if s_cleaned['location'] == 'lan': + counts['lan_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth']) else: - counts['wan_bandwidth'] += helpers.cast_to_int(s['bandwidth']) + counts['wan_bandwidth'] += helpers.cast_to_int(s_cleaned['bandwidth']) + + result['sessions'] = cleaned_sessions result.update(counts)