Skip to content

Commit

Permalink
Update api_helpers.py to fix segment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bertybuttface committed Jan 28, 2024
1 parent a75dd83 commit 6ab0d2e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
58 changes: 31 additions & 27 deletions src/iSponsorBlockTV/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,38 @@ async def get_segments(self, vid_id):

@staticmethod
def process_segments(response):
segments = []
if "segments" not in response:
return [], True

for seg in response["segments"]:
if not isinstance(seg, dict) or "segment" not in seg or not isinstance(seg["segment"], list):
raise TypeError("Invalid segment structure")

# Sort segments by their start time
sorted_segments = sorted(response["segments"], key=lambda x: x["segment"][0])

merged_segments = []
ignore_ttl = True
try:
for i in response["segments"]:
ignore_ttl = (
ignore_ttl and i["locked"] == 1
) # If all segments are locked, ignore ttl
segment = i["segment"]
UUID = i["UUID"]
segment_dict = {"start": segment[0], "end": segment[1], "UUID": [UUID]}
try:
# Get segment before to check if they are too close to each other
segment_before_end = segments[-1]["end"]
segment_before_start = segments[-1]["start"]
segment_before_UUID = segments[-1]["UUID"]

except Exception:
segment_before_end = -10
if (
segment_dict["start"] - segment_before_end < 1
): # Less than 1 second apart, combine them and skip them together
segment_dict["start"] = segment_before_start
segment_dict["UUID"].extend(segment_before_UUID)
segments.pop()
segments.append(segment_dict)
except Exception:
pass
return segments, ignore_ttl
for segment in sorted_segments:
ignore_ttl = ignore_ttl and segment.get("locked") == 1
segment["UUID"] = [segment["UUID"]]
if not merged_segments:
# First segment, just add it
merged_segments.append(segment)
elif segment["segment"][0] - merged_segments[-1]["segment"][1] < constants.min_gap_to_merge:
# Merge if segments are less than constant.min_gap_to_merge seconds apart
merged_segments[-1]["segment"][1] = segment["segment"][1]
merged_segments[-1]["UUID"].extend(segment["UUID"])
elif segment["segment"][0] <= merged_segments[-1]["segment"][1]:
# Merge if segments are overlapping
merged_segments[-1]["segment"][1] = max(merged_segments[-1]["segment"][1], segment["segment"][1])
merged_segments[-1]["UUID"].extend(segment["UUID"])
else:
# Non-overlapping segment, make UUIDs append
merged_segments.append(segment)

output_segments = [{"start": seg["segment"][0], "end": seg["segment"][1], "UUID": seg["UUID"]} for seg in merged_segments]
return output_segments, ignore_ttl

async def mark_viewed_segments(self, uuids):
"""Marks the segments as viewed in the SponsorBlock API, if skip_count_tracking is enabled.
Expand Down
2 changes: 2 additions & 0 deletions src/iSponsorBlockTV/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@


config_file_blacklist_keys = ["config_file", "data_dir"]

min_gap_to_merge = 1 # seconds

0 comments on commit 6ab0d2e

Please sign in to comment.