Browse Source

feat(video): auto-fetch YouTube title & description and add metadata preview endpoint

master
Mohsen Taba 3 weeks ago
parent
commit
f44f1b4bc7
  1. 21
      apps/video/models.py
  2. 15
      apps/video/serializers_admin.py
  3. 2
      apps/video/serializers_dovodi.py
  4. 1
      apps/video/urls.py
  5. 32
      apps/video/views.py
  6. 64
      utils/youtube.py

21
apps/video/models.py

@ -77,10 +77,10 @@ class Video(LowercaseSlugMixin, models.Model):
YOUTUBE_LINK = 'youtube_link', 'Youtube Link'
VIDEO_FILE = 'video_file', 'Video File'
title = models.CharField(max_length=255, null=True)
title = models.CharField(max_length=255, null=True, blank=True)
slug = models.SlugField(max_length=255, allow_unicode=True, unique=True, blank=True, null=True)
thumbnail = models.ImageField(upload_to='video/thumbnails/', null=True, blank=True, help_text=_('image allowed'))
description = models.TextField(null=True)
description = models.TextField(null=True, blank=True)
video_type = models.CharField(max_length=255, choices=VedioTypeChoices.choices)
video_file = models.FileField(upload_to='video/videos/', null=True, blank=True ,validators=[
@ -162,18 +162,31 @@ class Video(LowercaseSlugMixin, models.Model):
file_changed = True
url_changed = True
# Handle YouTube link automatic metadata extraction (duration & thumbnail)
# Handle YouTube link automatic metadata extraction (title, description, duration & thumbnail)
if self.video_type == self.VedioTypeChoices.YOUTUBE_LINK and self.video_url:
needs_thumbnail = not bool(self.thumbnail)
needs_time = not self.video_time or self.video_time == datetime.time(0, 0, 0)
needs_title = not bool(self.title and str(self.title).strip())
needs_description = not bool(self.description and str(self.description).strip())
if needs_thumbnail or needs_time or url_changed:
if needs_thumbnail or needs_time or needs_title or needs_description or url_changed:
try:
from utils.youtube import get_youtube_video_metadata, download_youtube_thumbnail
from utils.slug import generate_smart_slug
from django.core.files.base import ContentFile
meta = get_youtube_video_metadata(self.video_url)
if meta:
# Auto set title if missing
if needs_title and meta.get('title'):
self.title = str(meta['title']).strip()[:255]
if not self.slug:
self.slug = generate_smart_slug(self.title, Video, instance=self)
# Auto set description if missing
if needs_description and meta.get('description'):
self.description = str(meta['description']).strip()
# Auto set duration
if (needs_time or url_changed) and meta.get('duration_time'):
self.video_time = meta['duration_time']

15
apps/video/serializers_admin.py

@ -99,6 +99,8 @@ class AdminVideoListSerializer(serializers.ModelSerializer):
class AdminVideoDetailSerializer(serializers.ModelSerializer):
title = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=255)
description = serializers.CharField(required=False, allow_blank=True, allow_null=True)
thumbnail = AbsoluteImageField(required=False, allow_null=True)
video_file = AbsoluteFileField(required=False, allow_null=True)
video_time = serializers.TimeField(required=False, allow_null=True)
@ -152,10 +154,15 @@ class AdminVideoDetailSerializer(serializers.ModelSerializer):
"video_url": "This field is required when video type is link.",
})
if video_type == Video.VedioTypeChoices.VIDEO_FILE and not video_file and not getattr(self.instance, "video_file", None):
raise serializers.ValidationError({
"video_file": "This field is required when video type is file.",
})
if video_type == Video.VedioTypeChoices.VIDEO_FILE:
if not (title and str(title).strip()):
raise serializers.ValidationError({
"title": "This field is required when video type is file.",
})
if not video_file and not getattr(self.instance, "video_file", None):
raise serializers.ValidationError({
"video_file": "This field is required when video type is file.",
})
if video_type == Video.VedioTypeChoices.VIDEO_FILE and remove_video_file and "video_file" not in attrs:
raise serializers.ValidationError({

2
apps/video/serializers_dovodi.py

@ -44,6 +44,8 @@ class DovodiVideoCollectionSerializer(serializers.ModelSerializer):
class DovodiVideoItemSerializer(serializers.ModelSerializer):
title = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=255)
description = serializers.CharField(required=False, allow_blank=True, allow_null=True)
slug = serializers.CharField(required=False, allow_blank=True)
thumbnail = AbsoluteImageField(required=False, allow_null=True)
video_file = AbsoluteFileField(required=False, allow_null=True)

1
apps/video/urls.py

@ -39,5 +39,6 @@ urlpatterns = [
path('list/', conditional_cached_view(60 * 30, 'video_list')(VideoListAPIView.as_view()), name='video-list'),
re_path(r'detail/(?P<slug>[\w-]+)/$', VideoDetailAPIView.as_view(), name='video-detail'),
path('resolve-youtube/', ResolveYouTubeStreamURLAPIView.as_view(), name='resolve-youtube'),
path('youtube-metadata/', FetchYouTubeMetadataAPIView.as_view(), name='youtube-metadata'),
]

32
apps/video/views.py

@ -548,3 +548,35 @@ class ResolveYouTubeStreamURLAPIView(generics.GenericAPIView):
}, status=status.HTTP_200_OK)
class FetchYouTubeMetadataAPIView(generics.GenericAPIView):
"""
API view to extract metadata (title, description, duration, thumbnail) from a YouTube URL.
"""
permission_classes = (IsAuthenticated,)
authentication_classes = [TokenAuthentication]
swagger_schema = None
def get(self, request, *args, **kwargs):
youtube_url = request.query_params.get('url')
if not youtube_url:
return Response({'error': 'URL parameter is required.'}, status=status.HTTP_400_BAD_REQUEST)
from utils.youtube import get_youtube_video_metadata
meta = get_youtube_video_metadata(youtube_url)
if not meta or not meta.get('video_id'):
return Response({'error': 'Could not extract metadata from the provided URL.'}, status=status.HTTP_400_BAD_REQUEST)
duration_time = meta.get('duration_time')
video_time_str = duration_time.strftime('%H:%M:%S') if duration_time else None
return Response({
'video_id': meta.get('video_id'),
'title': meta.get('title') or '',
'description': meta.get('description') or '',
'duration_seconds': meta.get('duration_seconds'),
'video_time': video_time_str,
'thumbnail_url': meta.get('thumbnail_url'),
}, status=status.HTTP_200_OK)

64
utils/youtube.py

@ -116,8 +116,9 @@ def seconds_to_time(seconds) -> Optional[object]:
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):
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)
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
# 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:
import yt_dlp
@ -138,7 +186,7 @@ def get_youtube_video_metadata(youtube_url: str) -> Optional[dict]:
'nocheckcertificate': True,
'extractor_args': {
'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]
duration_secs = info.get('duration')
thumbnail = info.get('thumbnail') or fallback_thumbnail
thumbnail = fallback_thumbnail or info.get('thumbnail')
title = info.get('title')
description = info.get('description')
duration_time = seconds_to_time(duration_secs) if duration_secs is not None else None
return {
result = {
'video_id': video_id,
'duration_seconds': duration_secs,
'duration_time': duration_time,
'thumbnail_url': thumbnail,
'title': title,
'description': description,
}
if video_id:
cache.set(f"yt_metadata_v2_{video_id}", result, 3600)
return result
except Exception as 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,
'thumbnail_url': fallback_thumbnail,
'title': None,
'description': None,
}
return None

Loading…
Cancel
Save