Skip to content

Commit

Permalink
feat: Added management command to fetch skills for videos
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanuddinahmad committed Jul 15, 2024
1 parent b771d8b commit 027d980
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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)
)
14 changes: 9 additions & 5 deletions enterprise_catalog/apps/video_catalog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -68,23 +71,24 @@ 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(
video=video,
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:
Expand Down

0 comments on commit 027d980

Please sign in to comment.