diff --git a/config.json.template b/config.json.template index d3268ff..d4a163b 100644 --- a/config.json.template +++ b/config.json.template @@ -12,6 +12,7 @@ "skip_count_tracking": true, "mute_ads": true, "skip_ads": true, + "minimum_skip_length": 1, "auto_play": true, "join_name": "iSponsorBlockTV", "apikey": "", diff --git a/src/iSponsorBlockTV/api_helpers.py b/src/iSponsorBlockTV/api_helpers.py index 39d3ce0..33db3e9 100644 --- a/src/iSponsorBlockTV/api_helpers.py +++ b/src/iSponsorBlockTV/api_helpers.py @@ -27,6 +27,7 @@ class ApiHelper: self.skip_count_tracking = config.skip_count_tracking self.web_session = web_session self.num_devices = len(config.devices) + self.minimum_skip_length = config.minimum_skip_length # Not used anymore, maybe it can stay here a little longer @AsyncLRU(maxsize=10) @@ -139,10 +140,10 @@ class ApiHelper: if str(i["videoID"]) == str(vid_id): response_json = i break - return self.process_segments(response_json) + return self.process_segments(response_json, self.minimum_skip_length) @staticmethod - def process_segments(response): + def process_segments(response, minimum_skip_length): segments = [] ignore_ttl = True try: @@ -184,7 +185,9 @@ class ApiHelper: segment_dict["start"] = segment_before_start segment_dict["UUID"].extend(segment_before_UUID) segments.pop() - segments.append(segment_dict) + # Only add segments greater than minimum skip length + if segment_dict["end"] - segment_dict["start"] > minimum_skip_length: + segments.append(segment_dict) except BaseException: pass return segments, ignore_ttl diff --git a/src/iSponsorBlockTV/config_setup.py b/src/iSponsorBlockTV/config_setup.py index 6efb8e9..86b392c 100644 --- a/src/iSponsorBlockTV/config_setup.py +++ b/src/iSponsorBlockTV/config_setup.py @@ -24,6 +24,10 @@ SEARCH_CHANNEL_PROMPT = 'Enter a channel name or "/exit" to exit: ' SELECT_CHANNEL_PROMPT = "Select one option of the above [0-6]: " ENTER_CHANNEL_ID_PROMPT = "Enter a channel ID: " ENTER_CUSTOM_CHANNEL_NAME_PROMPT = "Enter the channel name: " +MINIMUM_SKIP_PROMPT = "Do you want to specify a minimum length of segment to skip? (y/N)" +MINIMUM_SKIP_SPECIFICATION_PROMPT = ( + "Enter minimum length of segment to skip in seconds (enter 0 to disable):" +) REPORT_SKIPPED_SEGMENTS_PROMPT = ( "Do you want to report skipped segments to sponsorblock. Only the segment" " UUID will be sent? (Y/n) " @@ -171,6 +175,21 @@ def main(config, debug: bool) -> None: config.channel_whitelist = channel_whitelist + # Ask for minimum skip length. Confirm input is an integer + minimum_skip_length = config.minimum_skip_length + + choice = get_yn_input(MINIMUM_SKIP_PROMPT) + if choice == "y": + while True: + try: + minimum_skip_length = int(input(MINIMUM_SKIP_SPECIFICATION_PROMPT)) + break + except ValueError: + print("You entered a non integer value, try again.") + continue + + config.minimum_skip_length = minimum_skip_length + choice = get_yn_input(REPORT_SKIPPED_SEGMENTS_PROMPT) config.skip_count_tracking = choice != "n" diff --git a/src/iSponsorBlockTV/helpers.py b/src/iSponsorBlockTV/helpers.py index dbca37d..6d56925 100644 --- a/src/iSponsorBlockTV/helpers.py +++ b/src/iSponsorBlockTV/helpers.py @@ -41,6 +41,7 @@ class Config: self.skip_count_tracking = True self.mute_ads = False self.skip_ads = False + self.minimum_skip_length = 1 self.auto_play = True self.join_name = "iSponsorBlockTV" self.__load() diff --git a/src/iSponsorBlockTV/setup_wizard.py b/src/iSponsorBlockTV/setup_wizard.py index 653a0b7..7e7830a 100644 --- a/src/iSponsorBlockTV/setup_wizard.py +++ b/src/iSponsorBlockTV/setup_wizard.py @@ -692,6 +692,43 @@ class SkipCategoriesManager(Vertical): self.config.skip_categories = event.selection_list.selected +class MinimumSkipLengthManager(Vertical): + """Manager for minimum skip length setting.""" + + def __init__(self, config, **kwargs) -> None: + super().__init__(**kwargs) + self.config = config + + def compose(self) -> ComposeResult: + yield Label("Minimum Skip Length", classes="title") + yield Label( + ( + "Specify the minimum length a segment must meet in order to skip " + "it (in seconds). Default is 1 second; entering 0 will skip all " + "segments." + ), + classes="subtitle", + ) + yield Input( + placeholder="Minimum skip length (0 to skip all)", + id="minimum-skip-length-input", + value=str(getattr(self.config, "minimum_skip_length", 1)), + validators=[ + Function( + lambda user_input: user_input.isdigit(), + "Please enter a valid non-negative number", + ) + ], + ) + + @on(Input.Changed, "#minimum-skip-length-input") + def changed_minimum_skip_length(self, event: Input.Changed): + try: + self.config.minimum_skip_length = int(event.input.value) + except ValueError: + self.config.minimum_skip_length = 1 + + class SkipCountTrackingManager(Vertical): """Manager for skip count tracking, allows to enable/disable skip count tracking.""" @@ -873,6 +910,11 @@ class ISponsorBlockTVSetupMainScreen(Screen): yield SkipCategoriesManager( config=self.config, id="skip-categories-manager", classes="container" ) + yield MinimumSkipLengthManager( + config=self.config, + id="minimum-skip-length-manager", + classes="container", + ) yield SkipCountTrackingManager( config=self.config, id="count-segments-manager", classes="container" )