File size: 1,358 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
48
# Native python imports
from datetime import timedelta
from typing import List


class ChapterThumbnail:
    """Container for chapter 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.chapters.ChapterThumbnail: width={self.width}, height={self.height}, url={self.url}>'


class Chapter:
    """Container for chapters tracks."""
    title: str
    start_seconds: int
    duration: int  # in seconds
    thumbnails: List[ChapterThumbnail]

    def __init__(self, chapter_data: dict, duration: int):
        data = chapter_data['chapterRenderer']

        self.title = data['title']['simpleText']
        self.start_seconds = int(data['timeRangeStartMillis'] / 1000)
        self.duration = duration

        thumbnails_data = data.get('thumbnail', {}).get('thumbnails', [])
        self.thumbnails = [
            ChapterThumbnail(
                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'<Chapter: {self.title} | {self.start_label}>'