File tree 2 files changed +23
-4
lines changed
src/mcp_youtube_transcript
2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -39,11 +39,14 @@ def get_transcript(
39
39
) -> str :
40
40
"""Retrieves the transcript of a YouTube video."""
41
41
parsed_url = urlparse (url )
42
- query_params = parse_qs (parsed_url .query )
43
42
44
- video_id = query_params .get ("v" , [None ])[0 ]
45
- if video_id is None :
46
- raise ValueError (f"couldn't find a video ID from the provided URL: { url } ." )
43
+ if parsed_url .hostname == "youtu.be" :
44
+ video_id = parsed_url .path .lstrip ("/" )
45
+ else :
46
+ q = parse_qs (parsed_url .query ).get ("v" )
47
+ if q is None :
48
+ raise ValueError (f"couldn't find a video ID from the provided URL: { url } ." )
49
+ video_id = q [0 ]
47
50
48
51
if lang == "en" :
49
52
languages = ["en" ]
Original file line number Diff line number Diff line change @@ -101,3 +101,19 @@ async def test_get_transcript_invalid_url(mcp_client_session: ClientSession) ->
101
101
async def test_get_transcript_not_found (mcp_client_session : ClientSession ) -> None :
102
102
res = await mcp_client_session .call_tool ("get_transcript" , arguments = {"url" : "https//www.youtube.com/watch?v=a" })
103
103
assert res .isError
104
+
105
+
106
+ @pytest .mark .skipif (os .getenv ("CI" ) == "true" , reason = "Skipping this test on CI" )
107
+ @pytest .mark .anyio
108
+ async def test_get_transcript_with_short_url (mcp_client_session : ClientSession ) -> None :
109
+ video_id = "LPZh9BOjkQs"
110
+
111
+ expect = "\n " .join ((item .text for item in YouTubeTranscriptApi ().fetch (video_id )))
112
+
113
+ res = await mcp_client_session .call_tool (
114
+ "get_transcript" ,
115
+ arguments = {"url" : f"https://youtu.be/{ video_id } " },
116
+ )
117
+ assert isinstance (res .content [0 ], TextContent )
118
+ assert res .content [0 ].text == expect
119
+ assert not res .isError
You can’t perform that action at this time.
0 commit comments