Spaces:
Sleeping
Sleeping
File size: 764 Bytes
1fcafa8 |
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 |
import os
import requests
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
def search_youtube(query: str) -> str:
"""
Searches YouTube for a relevant video tutorial and returns the URL of the top result.
"""
search_url = (
"https://www.googleapis.com/youtube/v3/search"
f"?part=snippet&type=video&videoEmbeddable=true&maxResults=1"
f"&q={requests.utils.quote(query)}&key={YOUTUBE_API_KEY}"
)
response = requests.get(search_url)
if response.status_code != 200:
return "YouTube search failed."
items = response.json().get("items", [])
if not items:
return "No video found for this query."
video_id = items[0]["id"]["videoId"]
return f"https://www.youtube.com/watch?v={video_id}" |