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 83f83dd
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions src/iSponsorBlockTV/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,28 @@ 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
if not merged_segments or segment["segment"][0] > merged_segments[-1]["segment"][1]:
merged_segments.append(segment)
else:
merged_segments[-1]["segment"][1] = max(merged_segments[-1]["segment"][1], segment["segment"][1])
merged_segments[-1]["UUID"].extend([segment["UUID"]])

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

0 comments on commit 83f83dd

Please sign in to comment.