Skip to content

youtube

Youtube module for downloading and searching songs.

YouTube(*args, **kwargs) ¤

Bases: AudioProvider

YouTube audio provider class

Source code in spotdl/providers/audio/youtube.py
25
26
27
28
29
30
31
32
33
def __init__(self, *args, **kwargs) -> None:
    """
    Initialize the YouTube audio provider
    """
    super().__init__(*args, **kwargs)

    # Set the client version to a specific version to avoid issues with pytube
    # See #2323 or https://github.com/pytube/pytube/issues/296
    innertube._default_clients['WEB']["context"]["client"]["clientVersion"] = "2.20230427.04.00"

get_results(search_term, *_args, **_kwargs) ¤

Get results from YouTube

Arguments¤
  • search_term: The search term to search for.
  • args: Unused.
  • kwargs: Unused.
Returns¤
  • A list of YouTube results if found, None otherwise.
Source code in spotdl/providers/audio/youtube.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def get_results(
    self, search_term: str, *_args, **_kwargs
) -> List[Result]:  # pylint: disable=W0221
    """
    Get results from YouTube

    ### Arguments
    - search_term: The search term to search for.
    - args: Unused.
    - kwargs: Unused.

    ### Returns
    - A list of YouTube results if found, None otherwise.
    """

    search_results: Optional[List[PyTube]] = Search(search_term).results

    if not search_results:
        return []

    results = []
    for result in search_results:
        if result.watch_url:
            try:
                duration = result.length
            except Exception:
                duration = 0

            try:
                views = result.views
            except Exception:
                views = 0

            results.append(
                Result(
                    source=self.name,
                    url=result.watch_url,
                    verified=False,
                    name=result.title,
                    duration=duration,
                    author=result.author,
                    search_query=search_term,
                    views=views,
                    result_id=result.video_id,
                )
            )

    return results