File size: 1,202 Bytes
2dfc476
 
 
7c21d30
2dfc476
7c21d30
 
2dfc476
7c21d30
 
 
 
 
2dfc476
7c21d30
2dfc476
ca3ab6d
 
 
 
 
 
 
2dfc476
7c21d30
2dfc476
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.proxies import GenericProxyConfig
from langchain_core.tools import tool

@tool
def youtube_transcript(video_url: str) -> str:
    """
    Extracts the transcript from a YouTube video url
    Args:
        video_url: The url of the YouTube video
    Returns:
        The transcript of the YouTube video
    """
    print(f"Extracting transcript from: {video_url}")
    try:
        ytt_api = YouTubeTranscriptApi()
        if os.getenv("PROXY_URL"):
            ytt_api = YouTubeTranscriptApi(
                proxy_config=GenericProxyConfig(
                    http_url=os.getenv("PROXY_URL"),
                    https_url=os.getenv("PROXY_URL"),
                )
            )
        video_id = get_video_id(video_url)
        transcript = ytt_api.fetch(video_id)
        print(f"Transcript: {transcript}")
        return transcript
    except Exception as e:
        print(f"Error extracting transcript: {e}")
        return ""

def get_video_id(url: str) -> str:
    """
    Extracts the video id from a YouTube url
    """
    print(f"Extracting video id from: {url}")
    return url.split("v=")[1]