|
|
@ -116,8 +116,9 @@ def seconds_to_time(seconds) -> Optional[object]: |
|
|
|
|
|
|
|
|
def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
""" |
|
|
""" |
|
|
Extract metadata (duration, thumbnail URL, title, etc.) from a YouTube video URL using yt-dlp. |
|
|
|
|
|
Falls back to regex-based video ID and thumbnail URL if yt-dlp extraction encounters network/restriction issues. |
|
|
|
|
|
|
|
|
Extract metadata (duration, thumbnail URL, title, description) from a YouTube video URL. |
|
|
|
|
|
Uses fast ytInitialPlayerResponse parsing first, then falls back to yt-dlp and regex-based ID. |
|
|
|
|
|
Caches extracted metadata for 1 hour to avoid duplicate network calls on preview + save. |
|
|
""" |
|
|
""" |
|
|
if not youtube_url or not isinstance(youtube_url, str): |
|
|
if not youtube_url or not isinstance(youtube_url, str): |
|
|
return None |
|
|
return None |
|
|
@ -126,8 +127,55 @@ def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
match = re.search(r'(?:v=|\/|embed\/|v\/|^)([0-9A-Za-z_-]{11})(?:[\?&/]|$)', clean_url) |
|
|
match = re.search(r'(?:v=|\/|embed\/|v\/|^)([0-9A-Za-z_-]{11})(?:[\?&/]|$)', clean_url) |
|
|
video_id = match.group(1) if match else None |
|
|
video_id = match.group(1) if match else None |
|
|
|
|
|
|
|
|
|
|
|
if video_id: |
|
|
|
|
|
cache_key = f"yt_metadata_v2_{video_id}" |
|
|
|
|
|
cached_meta = cache.get(cache_key) |
|
|
|
|
|
if cached_meta: |
|
|
|
|
|
return cached_meta |
|
|
|
|
|
|
|
|
fallback_thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg" if video_id else None |
|
|
fallback_thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg" if video_id else None |
|
|
|
|
|
|
|
|
|
|
|
# 1. Fast path: parse ytInitialPlayerResponse directly from YouTube watch page (~1.5s) |
|
|
|
|
|
if video_id: |
|
|
|
|
|
try: |
|
|
|
|
|
import json |
|
|
|
|
|
import urllib.request |
|
|
|
|
|
|
|
|
|
|
|
req = urllib.request.Request( |
|
|
|
|
|
f"https://www.youtube.com/watch?v={video_id}", |
|
|
|
|
|
headers={ |
|
|
|
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36", |
|
|
|
|
|
"Accept-Language": "en-US,en;q=0.9", |
|
|
|
|
|
}, |
|
|
|
|
|
) |
|
|
|
|
|
with urllib.request.urlopen(req, timeout=8) as resp: |
|
|
|
|
|
if resp.status == 200: |
|
|
|
|
|
html = resp.read().decode("utf-8", errors="ignore") |
|
|
|
|
|
m = re.search(r"var ytInitialPlayerResponse\s*=\s*(\{.+?\});", html) |
|
|
|
|
|
if m: |
|
|
|
|
|
player_data = json.loads(m.group(1)) |
|
|
|
|
|
vd = player_data.get("videoDetails") or {} |
|
|
|
|
|
title = vd.get("title") |
|
|
|
|
|
description = vd.get("shortDescription") |
|
|
|
|
|
length_str = vd.get("lengthSeconds") |
|
|
|
|
|
duration_secs = int(length_str) if length_str and str(length_str).isdigit() else None |
|
|
|
|
|
duration_time = seconds_to_time(duration_secs) if duration_secs is not None else None |
|
|
|
|
|
|
|
|
|
|
|
if title or duration_secs: |
|
|
|
|
|
result = { |
|
|
|
|
|
'video_id': video_id, |
|
|
|
|
|
'duration_seconds': duration_secs, |
|
|
|
|
|
'duration_time': duration_time, |
|
|
|
|
|
'thumbnail_url': fallback_thumbnail, |
|
|
|
|
|
'title': title, |
|
|
|
|
|
'description': description, |
|
|
|
|
|
} |
|
|
|
|
|
cache.set(f"yt_metadata_v2_{video_id}", result, 3600) |
|
|
|
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
logger.debug(f"Fast YouTube metadata fetch failed for '{youtube_url}', falling back to yt-dlp: {e}") |
|
|
|
|
|
|
|
|
|
|
|
# 2. Fallback path: yt-dlp |
|
|
try: |
|
|
try: |
|
|
import yt_dlp |
|
|
import yt_dlp |
|
|
|
|
|
|
|
|
@ -138,7 +186,7 @@ def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
'nocheckcertificate': True, |
|
|
'nocheckcertificate': True, |
|
|
'extractor_args': { |
|
|
'extractor_args': { |
|
|
'youtube': { |
|
|
'youtube': { |
|
|
'player_client': ['android', 'ios', 'web', 'mweb'], |
|
|
|
|
|
|
|
|
'player_client': ['android', 'web'], |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -150,18 +198,23 @@ def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
info = info['entries'][0] |
|
|
info = info['entries'][0] |
|
|
|
|
|
|
|
|
duration_secs = info.get('duration') |
|
|
duration_secs = info.get('duration') |
|
|
thumbnail = info.get('thumbnail') or fallback_thumbnail |
|
|
|
|
|
|
|
|
thumbnail = fallback_thumbnail or info.get('thumbnail') |
|
|
title = info.get('title') |
|
|
title = info.get('title') |
|
|
|
|
|
description = info.get('description') |
|
|
|
|
|
|
|
|
duration_time = seconds_to_time(duration_secs) if duration_secs is not None else None |
|
|
duration_time = seconds_to_time(duration_secs) if duration_secs is not None else None |
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
|
result = { |
|
|
'video_id': video_id, |
|
|
'video_id': video_id, |
|
|
'duration_seconds': duration_secs, |
|
|
'duration_seconds': duration_secs, |
|
|
'duration_time': duration_time, |
|
|
'duration_time': duration_time, |
|
|
'thumbnail_url': thumbnail, |
|
|
'thumbnail_url': thumbnail, |
|
|
'title': title, |
|
|
'title': title, |
|
|
|
|
|
'description': description, |
|
|
} |
|
|
} |
|
|
|
|
|
if video_id: |
|
|
|
|
|
cache.set(f"yt_metadata_v2_{video_id}", result, 3600) |
|
|
|
|
|
return result |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
logger.error(f"Failed to extract YouTube metadata for '{youtube_url}': {e}") |
|
|
logger.error(f"Failed to extract YouTube metadata for '{youtube_url}': {e}") |
|
|
|
|
|
|
|
|
@ -172,6 +225,7 @@ def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]: |
|
|
'duration_time': None, |
|
|
'duration_time': None, |
|
|
'thumbnail_url': fallback_thumbnail, |
|
|
'thumbnail_url': fallback_thumbnail, |
|
|
'title': None, |
|
|
'title': None, |
|
|
|
|
|
'description': None, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return None |
|
|
return None |
|
|
|