Skip to content

youtube

Youtube module for downloading and searching songs.

YouTube ¤

Bases: AudioProvider

YouTube audio provider class

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
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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