diff --git a/backend/open_webui/retrieval/loaders/youtube.py b/backend/open_webui/retrieval/loaders/youtube.py index 8eb48488b..6fb4d2b43 100644 --- a/backend/open_webui/retrieval/loaders/youtube.py +++ b/backend/open_webui/retrieval/loaders/youtube.py @@ -69,6 +69,39 @@ class YoutubeLoader: else: self.language = language + def _get_video_title(self) -> Optional[str]: + """Get the video title using YouTube API or page scraping.""" + try: + import requests + import json + + # First try using YouTube Data API v3 if available + try: + from open_webui.config import YOUTUBE_API_KEY + if YOUTUBE_API_KEY: + url = f"https://www.googleapis.com/youtube/v3/videos?id={self.video_id}&key={YOUTUBE_API_KEY}&part=snippet" + response = requests.get(url) + if response.status_code == 200: + data = response.json() + if data.get("items"): + return data["items"][0]["snippet"]["title"] + except ImportError: + pass + + # Fallback to scraping the title from YouTube page + url = f"https://www.youtube.com/watch?v={self.video_id}" + response = requests.get(url) + if response.status_code == 200: + import re + title_match = re.search(r'