diff --git a/capcut_agent/comments.py b/capcut_agent/comments.py index 569932c..5a382f9 100644 --- a/capcut_agent/comments.py +++ b/capcut_agent/comments.py @@ -9,7 +9,7 @@ from __future__ import annotations import json import re import urllib.request -from typing import Dict, List +from typing import Dict, List, Optional H_LAB = "https://h-lab.tolag.shop" @@ -97,3 +97,32 @@ def top_liked(comments: List[Dict], exclude: set, n: int = 20) -> List[int]: rest = [c for c in comments if c["idx"] not in exclude] rest.sort(key=lambda c: -c["likeCount"]) return [c["idx"] for c in rest[:n]] + + +def match_slots(comments: List[Dict], start: float, total: float, n: int, + exclude=None) -> List[Optional[int]]: + """구간을 n개 슬롯으로 잘라 슬롯마다 댓글 1개씩 배정 — 통짜 모드용. + + 슬롯 k = 원본 [start + k·total/n, start + (k+1)·total/n). + 통짜 모드는 구간을 그대로 이어붙여 **타임라인 시각 = 원본 시각**이라 이 계산이 성립한다 + (컷 있는 모드는 컷 순서를 섞어 재배치하므로 쓰면 안 된다 — 내용 기반 추천을 쓸 것). + + 각 슬롯은 그 시간대를 언급한 댓글 중 좋아요 최다 1개. 없으면 None. + 한 댓글이 두 슬롯에 겹치지 않는다. exclude(idx 집합)는 처음부터 제외. + Returns: 길이 n 리스트 (n<=0 이거나 total<=0 이면 빈 리스트) + """ + if n <= 0 or total <= 0: + return [] + used = set(exclude or ()) + step = total / n + out: List[Optional[int]] = [] + for k in range(n): + s = start + k * step + pick = None + for idx in match_ranges(comments, [(s, s + step)]): + if idx not in used: + pick = idx + used.add(idx) + break + out.append(pick) + return out