youtube-search-python

repository·main·Indexed 21 days ago

https://github.com/alexmercerind/youtube-search-python

A Python library for searching YouTube videos, channels, and playlists without requiring an official API key. It provides classes for synchronous and asynchronous searches (VideosSearch, ChannelsSearch, PlaylistsSearch), custom filtering via CustomSearch, and tools for retrieving video information, transcripts, comments, and direct stream URLs using StreamURLFetcher.

Tokens
9.4K
Snippets
37
Records
38
Agent score
24%

What's inside youtube-search-python

  1. Get next page search results

    main

    To paginate through search results, use the next() method on a VideosSearch instance. Calling next() advances the internal pointer to the next page. Subsequent calls to result() will then return the data for that specific page.

    from youtubesearchpython import VideosSearch
    
    search = VideosSearch('NoCopyrightSounds')
    
    # Get first page
    print(search.result()['result'])
    
    # Get second page
    search.next()
    print(search.result()['result'])
    
    # Get third page
    search.next()
    print(search.result()['result'])
  2. Retrieve all videos from a channel or playlist

    main

    To fetch all videos from a channel or a specific playlist, use the Playlist class. Since YouTube typically returns only 100 videos per request, you must implement a loop to fetch subsequent pages.

    1. Instantiate Playlist with a playlist link or use playlist_from_channel_id(channel_id) to get a link for a specific channel.
    2. Check the hasMoreVideos boolean property to see if more videos are available.
    3. Call getNextVideos() to fetch the next batch of videos.

    This pattern allows you to iterate through the entire video history of a channel or a large playlist.

    from youtubesearchpython import *
    
    # Example: Getting all videos from a channel
    channel_id = "UC_aEa8K-EOJ3D6gOs7HcyNg"
    playlist = Playlist(playlist_from_channel_id(channel_id))
    
    print(f'Videos Retrieved: {len(playlist.videos)}')
    
    while playlist.hasMoreVideos:
        print('Getting more videos...')
        playlist.getNextVideos()
        print(f'Videos Retrieved: {len(playlist.videos)}')
    
    print('Found all the videos.')
  3. Get video comments

    main

    You can retrieve comments from a video using either a video ID or a video URL.

    There are two ways to use the Comments class:

    1. Pagination (All comments): Instantiate Comments(video_id), then loop using while comments.hasMoreComments: and await comments.getNextComments() to fetch all comments in batches.
    2. Direct Fetch (First 20 comments): Use the static method await Comments.get(video_id) to quickly retrieve the first 20 comments.
    from youtubesearchpython.__future__ import *
    
    # Option 1: Get all comments via pagination
    video_id = "_ZdsmLgCVdU"
    comments = Comments(video_id)
    while comments.hasMoreComments:
        await comments.getNextComments()
    
    # Option 2: Get first 20 comments
    comments_batch = await Comments.get(video_id)
    print(comments_batch)
  4. Search for playlists using PlaylistsSearch

    main

    Use PlaylistsSearch from youtubesearchpython.__future__ to perform asynchronous searches specifically for YouTube playlists. Results are retrieved by awaiting the .next() method.

    from youtubesearchpython.__future__ import PlaylistsSearch
    
    playlistsSearch = PlaylistsSearch('NoCopyrightSounds', limit = 1)
    playlistsResult = await playlistsSearch.next()
    print(playlistsResult)
  5. Get playlist information using a link

    main

    You can retrieve information about a YouTube playlist or the videos contained within it using the Playlist class methods.

    • Playlist.get(link, mode=ResultMode.json): Returns both playlist information and video formats.
    • Playlist.getInfo(link, mode=ResultMode.json): Returns only the metadata/information about the playlist.
    • Playlist.getVideos(link): Returns only the list of videos in the playlist.

    Use mode=ResultMode.json to receive the data in a JSON-compatible format.

    playlist = Playlist.get('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
    print(playlist)
    
    playlistInfo = Playlist.getInfo('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK', mode = ResultMode.json)
    print(playlistInfo)
    
    playlistVideos = Playlist.getVideos('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
    print(playlistVideos)
  6. Search for videos synchronously using VideosSearch

    main

    Use the VideosSearch class from youtubesearchpython to perform synchronous searches for videos. You can specify a search query and a limit for the number of results returned. Call .result() to retrieve the data.

    from youtubesearchpython import VideosSearch
    
    videosSearch = VideosSearch('NoCopyrightSounds', limit = 2)
    
    print(videosSearch.result())
  7. Retrieve and paginate channel playlists

    main

    To fetch all playlists from a channel, instantiate a Channel object, call await channel.init(), and then use a loop with channel.has_more_playlists() and await channel.next() to paginate through the results.

    The playlists are stored in channel.result["playlists"].

    from youtubesearchpython.__future__ import Channel
    
    channel = Channel("UC_aEa8K-EOJ3D6gOs7HcyNg")
    await channel.init()
    
    print(len(channel.result["playlists"]))
    
    # Paginate through all playlists
    while channel.has_more_playlists():
        await channel.next()
        print(len(channel.result["playlists"]))
  8. Retrieve channel information using Channel.get()

    main

    Use the Channel class from youtubesearchpython.__future__ to fetch metadata for a specific YouTube channel using its Channel ID.

    Channel Result Schema:

    • id: The channel ID.
    • url: The channel URL.
    • description: The channel description.
    • title: The channel name.
    • subscribers: A dictionary with simpleText and label.
    • thumbnails: A list of available thumbnail URLs and dimensions.
    • banners: A list of available banner URLs and dimensions.
    • views: Total channel views.
    • joinedDate: The date the channel was created.
    • country: The channel's country.
    from youtubesearchpython.__future__ import Channel
    
    # Fetch channel info using the Channel ID
    print(await Channel.get("UC_aEa8K-EOJ3D6gOs7HcyNg"))
  9. Search for YouTube playlists using PlaylistsSearch

    main

    Use the PlaylistsSearch class to perform searches that return only YouTube playlist information. You can specify a limit for the number of results.

    from youtubesearchpython import PlaylistsSearch
    
    playlistsSearch = PlaylistsSearch('NoCopyrightSounds', limit = 1)
    
    print(playlistsSearch.result())