Update download_transcript.py

This commit is contained in:
Saifeddine ALOUI 2024-08-29 00:16:24 +02:00 committed by GitHub
parent 62df82e8e3
commit 1ee216e2f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ from functools import partial
from typing import List from typing import List
from lollms.utilities import PackageManager from lollms.utilities import PackageManager
from ascii_colors import trace_exception from ascii_colors import trace_exception
from typing import Any
# Installing necessary packages # Installing necessary packages
if not PackageManager.check_package_installed("youtube_transcript_api"): if not PackageManager.check_package_installed("youtube_transcript_api"):
@ -16,19 +17,20 @@ if not PackageManager.check_package_installed("youtube_transcript_api"):
# Importing the package after installation # Importing the package after installation
from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api import YouTubeTranscriptApi
def download_youtube_transcript(video_id: str) -> str: def download_youtube_transcript(video_id: str, language_code: str = 'en') -> str:
""" """
This function downloads the transcript of a YouTube video given its video ID. This function downloads the transcript of a YouTube video given its video ID.
Parameters: Parameters:
video_id (str): The ID of the YouTube video. video_id (str): The ID of the YouTube video.
languages (tuple): A list of languages to extract.
Returns: Returns:
str: The transcript of the video. str: The transcript of the video.
""" """
try: try:
# Fetching the transcript # Fetching the transcript
transcript = YouTubeTranscriptApi.get_transcript(video_id) transcript = YouTubeTranscriptApi.get_transcript(video_id, (language_code,))
# Combining the transcript into a single string # Combining the transcript into a single string
transcript_text = " ".join([entry['text'] for entry in transcript]) transcript_text = " ".join([entry['text'] for entry in transcript])
@ -42,5 +44,5 @@ def download_youtube_transcript_function():
"function_name": "download_youtube_transcript", "function_name": "download_youtube_transcript",
"function": download_youtube_transcript, "function": download_youtube_transcript,
"function_description": "This function goes online to YouTube and downloads the transcript from any video.", "function_description": "This function goes online to YouTube and downloads the transcript from any video.",
"function_parameters": [{"name": "video_id", "type": "str"}] "function_parameters": [{"name": "video_id", "type": "str"},{"name": "language_code", "type": "str"}]
} }