From 027d980b6b860421fbba360bcb1adf63a9c10c8a Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Mon, 15 Jul 2024 16:40:06 +0500 Subject: [PATCH] feat: Added management command to fetch skills for videos --- .../management/commands/fetch_video_skills.py | 54 +++++++++++++++++++ .../apps/video_catalog/utils.py | 14 +++-- 2 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 enterprise_catalog/apps/video_catalog/management/commands/fetch_video_skills.py diff --git a/enterprise_catalog/apps/video_catalog/management/commands/fetch_video_skills.py b/enterprise_catalog/apps/video_catalog/management/commands/fetch_video_skills.py new file mode 100644 index 000000000..c66015ab1 --- /dev/null +++ b/enterprise_catalog/apps/video_catalog/management/commands/fetch_video_skills.py @@ -0,0 +1,54 @@ +""" +Management command for fetching video skills from taxonomy connector +""" +import logging + +from django.core.management.base import BaseCommand + +from enterprise_catalog.apps.video_catalog.models import Video, VideoSkill +from enterprise_catalog.apps.video_catalog.utils import store_video_skills + + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + """ + Management command for fetching video skills from taxonomy connector + + Example Usage: + >> python manage.py fetch_video_skills + """ + help = ( + 'Fetch the skills associated with videos from taxonomy connector' + ) + + def add_arguments(self, parser): + parser.add_argument( + '--force', + default=False, + action='store_true', + help='Force execution and re-process any previously processed rows.', + ) + + def handle(self, *args, **options): + """ + Fetch video content metadata from LMS. + """ + processed_videos = VideoSkill.objects.all().values_list('video__edx_video_id', flat=True) + videos = Video.objects.exclude(edx_video_id__in=processed_videos) + if options.get('force', False): + videos = Video.objects.all() + for video in videos: + try: + store_video_skills(video) + logger.info( + '[FETCH_VIDEO_SKILLS] Skills for Video id: "%s" saved successfully.', + video.edx_video_id + ) + except Exception as ex: # pylint: disable=broad-exception-caught + logger.error( + '[FETCH_VIDEO_SKILLS] Skills for Video id: "%s" could not be fetched. Ex: "%s".', + video.edx_video_id, + str(ex) + ) diff --git a/enterprise_catalog/apps/video_catalog/utils.py b/enterprise_catalog/apps/video_catalog/utils.py index 0014cebd5..bedece9e5 100644 --- a/enterprise_catalog/apps/video_catalog/utils.py +++ b/enterprise_catalog/apps/video_catalog/utils.py @@ -59,6 +59,9 @@ def fetch_course_video_metadata(course_run_key, video_usage_key): def fetch_video(video): """ Fetch and store video metadata. + + Arguments: + video (Video): The Video object. """ try: video_usage_key = UsageKey.from_string(video.video_usage_key) @@ -68,16 +71,14 @@ def fetch_video(video): fetch_course_video_metadata(course_run_key, video.video_usage_key) -def store_video_skills(edx_video_id): +def store_video_skills(video): """ Fetch and store video skills for a video. Arguments: - edx_video_id (str): The video id for which to fetch the skills. + video (Video): The Video object. """ - video_skills = [] - video = Video.objects.filter(edx_video_id=edx_video_id).first() - if video and video.video_usage_key: + try: video_skills = DiscoveryApiClient().get_video_skills(video.video_usage_key) for skill in video_skills: VideoSkill.objects.update_or_create( @@ -85,6 +86,9 @@ def store_video_skills(edx_video_id): skill_id=skill.get('id'), name=skill.get('name'), ) + except Exception as exc: + logger.exception(f'Could not retrieve and store video skills {exc}') + raise exc def get_transcript_summary(transcript: str, max_length: int = 260) -> str: