Final_Assignment_Template / tools /youtube_tools.py
Giustino Esposito
Refactored code
d5ccf60
from langchain_core.tools import tool
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled
@tool("youtube_transcript_extractor", parse_docstring=True)
def extract_youtube_transcript(youtube_url: str) -> str:
"""Extracts the transcript from a given YouTube video URL.
Args:
youtube_url (str): The URL of the YouTube video.
Returns:
str: The transcript as a single string, or an error message if the transcript
cannot be found or an error occurs.
"""
try:
video_id = youtube_url.split("v=")[1].split("&")[0]
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
transcript = " ".join([item['text'] for item in transcript_list])
return transcript
except NoTranscriptFound:
return "Error: No transcript found for this video. It might be disabled or not available in English."
except TranscriptsDisabled:
return "Error: Transcripts are disabled for this video."
except Exception as e:
return f"Error extracting transcript: {str(e)}"