Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Last active January 1, 2024 11:18
Show Gist options
  • Save jefftriplett/4bf333453d7cab320aa45767b1949be6 to your computer and use it in GitHub Desktop.
Save jefftriplett/4bf333453d7cab320aa45767b1949be6 to your computer and use it in GitHub Desktop.
A YouTube Playlist to Markdown automation
"""
----------------------------------------------------------------------------
Author: Jeff Triplett <https://github.com/jefftriplett>
Copyright (c) 2023 - Jeff Triplett
License: PolyForm Noncommercial License 1.0.0 - https://polyformproject.org/licenses/noncommercial/1.0.0/
----------------------------------------------------------------------------
1. To extract video URLs, titles, and descriptions from a YouTube playlist using Python, you can use the google-api-python-client library. Here's a step-by-step guide on how to do this:
Install the google-api-python-client library:
```shell
pip install google-api-python-client environs rich typer
```
2. Get an API key for the YouTube Data API:
- Go to the Google Cloud Console: https://console.cloud.google.com/
- Create a new project or select an existing one.
- Click on "Enable APIs and Services" and search for "YouTube Data API v3".
- Click "Enable" to enable the API for your project.
- Click "Create credentials" and follow the steps to get an API key.
3. To use it:
```shell
export YOUTUBE_API_KEY=YOUR-API-KEY-HERE
python demo-youtube-playlist-to-markdown.py --playlist=PL2NFhrDSOxgUoF-4F2MdAFvOK1wOrNdqB
```
"""
import typer
import googleapiclient.discovery
from environs import Env
from rich import print
env = Env()
YOUTUBE_API_KEY = env.str("YOUTUBE_API_KEY")
def get_playlist_videos(
*,
playlist_id: str,
playlist_url: str,
max_results: int = 50,
):
youtube = googleapiclient.discovery.build(
"youtube", "v3", developerKey=YOUTUBE_API_KEY
)
request = youtube.playlistItems().list(
part="snippet", maxResults=max_results, playlistId=playlist_id
)
videos = []
while request:
response = request.execute()
for item in response["items"]:
video = {
"url": f"https://www.youtube.com/watch?v={item['snippet']['resourceId']['videoId']}",
"title": item["snippet"]["title"],
"description": item["snippet"]["description"],
}
videos.append(video)
request = youtube.playlistItems().list_next(request, response)
return videos
def main(
playlist_id: str = typer.Option("PLcNrB7gPa-NedACvFYa9iVUIhnku_EBSz", "--playlist"),
max_results: int = 50
):
playlist_url = f"https://www.youtube.com/playlist?list={playlist_id}"
playlist_videos = get_playlist_videos(
playlist_id=playlist_id,
playlist_url=playlist_url,
max_results=max_results,
)
for video in playlist_videos:
print(
f"## {video['title']}\n\n"
f"{video['description']}\n\n"
f"{video['url']}\n"
"----"
)
if __name__ == "__main__":
typer.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment