Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deeptutor/video_learning/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class ProviderResolution:

def parse_timestamp(value: Any) -> int:
raw = str(value or "").strip().lower()
if raw.isdigit():
# isdecimal, not isdigit: "²" is a digit to Python but int() rejects it.
if raw.isdecimal():
return max(0, int(raw))
match = re.fullmatch(r"(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?", raw)
if not match or not any(match.groups()):
Expand Down
12 changes: 12 additions & 0 deletions tests/video_learning/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def isolated(monkeypatch, tmp_path: Path) -> Path:
("https://youtube.com/shorts/dQw4w9WgXcQ", "dQw4w9WgXcQ", 0),
("https://youtube.com/live/dQw4w9WgXcQ?start=12", "dQw4w9WgXcQ", 12),
("https://youtube.com/embed/dQw4w9WgXcQ", "dQw4w9WgXcQ", 0),
# A superscript digit passes str.isdigit() but not int(); ignore it.
("https://youtu.be/dQw4w9WgXcQ?t=²", "dQw4w9WgXcQ", 0),
],
)
def test_parse_supported_youtube_urls(url: str, video_id: str, start: int) -> None:
Expand All @@ -45,6 +47,16 @@ def test_parse_supported_youtube_urls(url: str, video_id: str, start: int) -> No
assert "si=" not in parsed.canonical_url


@pytest.mark.parametrize(
("value", "seconds"),
[("82", 82), ("1m2s", 62), ("²", 0), ("³⁴", 0), ("١٢", 12), ("1.5", 0)],
)
def test_parse_timestamp_ignores_digit_like_characters_int_rejects(
value: str, seconds: int
) -> None:
assert service.parse_timestamp(value) == seconds


@pytest.mark.parametrize(
"url",
[
Expand Down
Loading