From f138e27018571870287edd5c0b07417bbfe50b3b Mon Sep 17 00:00:00 2001 From: Paco8 <5084042+Paco8@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:36:39 +0200 Subject: [PATCH] Version 0.5.8 --- addon.xml | 2 +- resources/lib/mpd.py | 85 +++++++++++++++++++++++++++++++++++++++++ resources/lib/plugin.py | 9 +---- resources/lib/proxy.py | 5 +++ resources/settings.xml | 2 +- 5 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 resources/lib/mpd.py diff --git a/addon.xml b/addon.xml index 436d618..38ad3b0 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/resources/lib/mpd.py b/resources/lib/mpd.py new file mode 100644 index 0000000..dedecbb --- /dev/null +++ b/resources/lib/mpd.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# encoding: utf-8 +# +# SPDX-License-Identifier: LGPL-2.1-or-later + +from __future__ import unicode_literals, absolute_import, division + +import xml.dom.minidom as minidom +import datetime +import re + +def parse_duration(duration_str): + # Regular expression to parse ISO 8601 duration format (e.g., PT1H2M3S) + pattern = re.compile(r'PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?') + match = pattern.match(duration_str) + if not match: + return 0 + + hours, minutes, seconds = match.groups() + hours = int(hours) if hours else 0 + minutes = int(minutes) if minutes else 0 + seconds = float(seconds) if seconds else 0 + + return datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds) + +def find_unprotected_periods(doc): + unprotected_periods = [] + protected_periods = [] + + periods = doc.getElementsByTagNameNS("urn:mpeg:dash:schema:mpd:2011", "Period") + for period in periods: + content_protection = period.getElementsByTagNameNS("urn:mpeg:dash:schema:mpd:2011", "ContentProtection") + if not content_protection: + unprotected_periods.append(period) + else: + protected_periods.append(period) + + return unprotected_periods, protected_periods + +def format_start_time(timedelta_obj): + total_seconds = timedelta_obj.total_seconds() + minutes = int(total_seconds // 60) + seconds = total_seconds % 60 + # Format seconds to 6 decimal places + return "PT{}M{:.6f}S".format(minutes, seconds) + +def set_period_start_times(periods): + current_time = datetime.timedelta() + for period in periods: + start_time = format_start_time(current_time) + period.setAttribute("start", start_time) + + duration_str = period.getAttribute("duration") + if duration_str: + duration = parse_duration(duration_str) + current_time += duration + else: + # If no duration is provided, assume a default duration (e.g., 0 seconds) + duration = datetime.timedelta(seconds=0) + current_time += duration + +def move_unprotected_periods(content): + doc = minidom.parseString(content) + + # Find unprotected and protected periods + unprotected_periods, protected_periods = find_unprotected_periods(doc) + + if not unprotected_periods: + return content + + # Remove all periods + mpd = doc.getElementsByTagNameNS("urn:mpeg:dash:schema:mpd:2011", "MPD")[0] + periods = doc.getElementsByTagNameNS("urn:mpeg:dash:schema:mpd:2011", "Period") + for period in periods: + mpd.removeChild(period) + + # Insert unprotected periods at the beginning + all_periods = unprotected_periods + protected_periods + for period in all_periods: + mpd.appendChild(period) + + # Set the start times for all periods + set_period_start_times(all_periods) + + return doc.toprettyxml() diff --git a/resources/lib/plugin.py b/resources/lib/plugin.py index 59e33d9..9cee1d8 100644 --- a/resources/lib/plugin.py +++ b/resources/lib/plugin.py @@ -46,11 +46,6 @@ def play(params): LOG('play - slug: {} service_key: {}'.format(slug, service_key)) - #if sky.platform['name'] == 'SkyShowtime': - # if 'NO_ADS' not in sky.account['account_type']: - # show_notification(addon.getLocalizedString(30209)) - # return - if slug: info = sky.get_video_info(slug) LOG('video info: {}'.format(info)) @@ -91,8 +86,8 @@ def play(params): return url = data['manifest_url'] - #if 'NO_ADS' not in sky.account['account_type'] and slug: - if addon.getSettingBool('ads') and slug: + if 'NO_ADS' not in sky.account['account_type'] and slug: + #if addon.getSettingBool('ads') and slug: try: new_url = sky.get_manifest_with_ads(data) if new_url: diff --git a/resources/lib/proxy.py b/resources/lib/proxy.py index 998e906..c82abb7 100644 --- a/resources/lib/proxy.py +++ b/resources/lib/proxy.py @@ -114,6 +114,11 @@ def do_GET(self): pattern = r'(mimeType="text/vtt".*?)presentationTimeOffset="\d+"' content = re.sub(pattern, r'\1', content, flags=re.DOTALL) + if addon.getSetting('platform_id') == 'PeacockTV': # and addon.getSettingBool('ads'): + # Due to Kodi bugs, ads are moved to the beginning of the video + from .mpd import move_unprotected_periods + content = move_unprotected_periods(content) + #LOG('content: {}'.format(content)) self.send_response(200) self.send_header('Content-type', 'application/xml') diff --git a/resources/settings.xml b/resources/settings.xml index 4be4383..7e6a19e 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -9,7 +9,7 @@ - +