File size: 1,335 Bytes
44bafb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Native python imports
from datetime import timedelta
from typing import List


class KeyMomentsThumbnail:
    """Container for key moments thumbnails."""

    def __init__(self, width: int, height: int, url: str):
        self.width = width
        self.height = height
        self.url = url

    def __repr__(self):
        return f"<pytubefix.keymoments.KeyMomentThumbnail: width={self.width}, height={self.height}, url={self.url}>"


class KeyMoment:
    """Container for key moments tracks."""

    title: str
    start_seconds: int
    duration: int  # in seconds
    thumbnails: List[KeyMomentsThumbnail]

    def __init__(self, keymoment_data: dict, duration: int):
        data = keymoment_data

        self.title = data["title"]["simpleText"]
        self.start_seconds = int(data["startMillis"]) // 1000
        self.duration = duration

        thumbnails_data = data.get("thumbnailDetails", {}).get("thumbnails", [])
        self.thumbnails = [
            KeyMomentsThumbnail(
                width=thumb["width"], height=thumb["height"], url=thumb["url"]
            )
            for thumb in thumbnails_data
        ]

    @property
    def start_label(self) -> str:
        return str(timedelta(seconds=self.start_seconds))

    def __repr__(self):
        return f"<KeyMoment: {self.title} | {self.start_label}>"