Skip to content

Instantly share code, notes, and snippets.

@twyle
Created December 20, 2023 08:04
Show Gist options
  • Save twyle/e1092440deb8dd4495bc1dd55e20be72 to your computer and use it in GitHub Desktop.
Save twyle/e1092440deb8dd4495bc1dd55e20be72 to your computer and use it in GitHub Desktop.
# First install the library
# pip install youtube
# You need youtube credentials. Follow this tutorila to get them https://medium.com/@lyle-okoth/how-to-get-a-google-api-key-d3c38649eaae
# This simple example shows you how to search through youtube for python ptogramming videos
from youtube import YouTube
from youtube.schemas import (
SearchFilter, SearchOptionalParameters, SearchPart, YouTubeResponse, YouTubeRequest
)
from typing import Iterator
from youtube.models import Video
client_secrets_file = '/home/lyle/Downloads/secrets.json'
youtube = YouTube(client_secrets_file)
youtube.authenticate()
query: str = 'Python programming videos'
max_results: int = 10
part: SearchPart = SearchPart()
optional_parameters: SearchOptionalParameters = SearchOptionalParameters(
q=query,
maxResults=max_results,
type=['video', 'playlist', 'channel']
)
search_request: YouTubeRequest = YouTubeRequest(
part=part,
optional_parameters=optional_parameters
)
video_iterator: Iterator = youtube.get_search_iterator(search_request)
videos: list[Video] = next(video_iterator)
print(videos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment