Skip to content

Commit

Permalink
Update api_helpers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bertybuttface committed Jan 28, 2024
1 parent a75dd83 commit 28619b1
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/iSponsorBlockTV/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,24 @@ async def get_segments(self, vid_id):

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

# 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 28619b1

Please sign in to comment.