update get_transcript util
This commit is contained in:
@@ -760,7 +760,11 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
|
||||
self.user = UserFactory.create()
|
||||
self.vertical = ItemFactory.create(category='vertical', parent_location=self.course.location)
|
||||
self.video = ItemFactory.create(category='video', parent_location=self.vertical.location)
|
||||
self.video = ItemFactory.create(
|
||||
category='video',
|
||||
parent_location=self.vertical.location,
|
||||
edx_video_id=u'1234-5678-90'
|
||||
)
|
||||
|
||||
def create_transcript(self, subs_id, language=u'en', filename='video.srt'):
|
||||
"""
|
||||
@@ -774,7 +778,8 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
category='video',
|
||||
parent_location=self.vertical.location,
|
||||
sub=subs_id,
|
||||
transcripts=transcripts
|
||||
transcripts=transcripts,
|
||||
edx_video_id=u'1234-5678-90'
|
||||
)
|
||||
|
||||
if subs_id:
|
||||
@@ -823,7 +828,10 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
Verify that `NotFoundError` exception is raised when transcript is not found in both the content store and val.
|
||||
"""
|
||||
with self.assertRaises(NotFoundError):
|
||||
transcripts_utils.get_transcript(self.course.id, self.video.location.block_id, lang=lang)
|
||||
transcripts_utils.get_transcript(
|
||||
self.video,
|
||||
lang=lang
|
||||
)
|
||||
|
||||
@ddt.data(
|
||||
{
|
||||
@@ -845,8 +853,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
self.upload_file(self.create_srt_file(self.subs_srt), self.video.location, filename)
|
||||
self.create_transcript(subs_id, language, filename)
|
||||
content, filename, mimetype = transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
language
|
||||
)
|
||||
|
||||
@@ -861,8 +868,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
language = u'ur'
|
||||
self.create_transcript(self.subs_id, language)
|
||||
content, filename, mimetype = transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
language,
|
||||
output_format=transcripts_utils.Transcript.SJSON
|
||||
)
|
||||
@@ -871,10 +877,6 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
self.assertEqual(filename, 'ur_video_101.sjson')
|
||||
self.assertEqual(mimetype, self.sjson_mime_type)
|
||||
|
||||
@patch(
|
||||
'openedx.core.djangoapps.video_config.models.VideoTranscriptEnabledFlag.feature_enabled',
|
||||
Mock(return_value=True),
|
||||
)
|
||||
@patch('xmodule.video_module.transcripts_utils.get_video_transcript_content')
|
||||
def test_get_transcript_from_val(self, mock_get_video_transcript_content):
|
||||
"""
|
||||
@@ -886,8 +888,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
}
|
||||
|
||||
content, filename, mimetype = transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
)
|
||||
self.assertEqual(content, self.subs_srt)
|
||||
self.assertEqual(filename, 'edx.srt')
|
||||
@@ -899,8 +900,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
"""
|
||||
with self.assertRaises(NotFoundError) as invalid_format_exception:
|
||||
transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
'ur',
|
||||
output_format='mpeg'
|
||||
)
|
||||
@@ -917,8 +917,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
|
||||
with self.assertRaises(NotFoundError) as no_content_exception:
|
||||
transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
'ur'
|
||||
)
|
||||
|
||||
@@ -933,8 +932,7 @@ class TestGetTranscript(SharedModuleStoreTestCase):
|
||||
self.store.update_item(self.video, self.user.id)
|
||||
with self.assertRaises(NotFoundError) as no_en_transcript_exception:
|
||||
transcripts_utils.get_transcript(
|
||||
self.course.id,
|
||||
self.video.location.block_id,
|
||||
self.video,
|
||||
'en'
|
||||
)
|
||||
|
||||
|
||||
@@ -904,6 +904,8 @@ def get_transcript_for_video(video_location, subs_id, file_name, language):
|
||||
tuple containing transcript input_format, basename, content
|
||||
"""
|
||||
try:
|
||||
if subs_id is None:
|
||||
raise NotFoundError
|
||||
content = Transcript.asset(video_location, subs_id, language).data
|
||||
base_name = subs_id
|
||||
input_format = Transcript.SJSON
|
||||
@@ -915,7 +917,7 @@ def get_transcript_for_video(video_location, subs_id, file_name, language):
|
||||
return input_format, base_name, content
|
||||
|
||||
|
||||
def get_transcript_from_contentstore(video, language, output_format, youtube_id=None, is_bumper=False):
|
||||
def get_transcript_from_contentstore(video, language, output_format, transcripts_info, youtube_id=None):
|
||||
"""
|
||||
Get video transcript from content store.
|
||||
|
||||
@@ -923,8 +925,8 @@ def get_transcript_from_contentstore(video, language, output_format, youtube_id=
|
||||
video (Video Descriptor): Video descriptor
|
||||
language (unicode): transcript language
|
||||
output_format (unicode): transcript output format
|
||||
transcripts_info (dict): transcript info for a video
|
||||
youtube_id (unicode): youtube video id
|
||||
is_bumper (bool): indicates bumper video
|
||||
|
||||
Returns:
|
||||
tuple containing content, filename, mimetype
|
||||
@@ -932,7 +934,6 @@ def get_transcript_from_contentstore(video, language, output_format, youtube_id=
|
||||
if output_format not in (Transcript.SRT, Transcript.SJSON, Transcript.TXT):
|
||||
raise NotFoundError('Invalid transcript format `{output_format}`'.format(output_format=output_format))
|
||||
|
||||
transcripts_info = video.get_transcripts_info(is_bumper=is_bumper)
|
||||
sub, other_languages = transcripts_info['sub'], transcripts_info['transcripts']
|
||||
transcripts = dict(other_languages)
|
||||
|
||||
@@ -949,8 +950,8 @@ def get_transcript_from_contentstore(video, language, output_format, youtube_id=
|
||||
try:
|
||||
input_format, base_name, transcript_content = get_transcript_for_video(
|
||||
video.location,
|
||||
subs_id=transcripts['en'],
|
||||
file_name=language and transcripts[language],
|
||||
subs_id=transcripts.get('en'),
|
||||
file_name=transcripts[language],
|
||||
language=language
|
||||
)
|
||||
except KeyError:
|
||||
@@ -973,29 +974,33 @@ def get_transcript_from_contentstore(video, language, output_format, youtube_id=
|
||||
return transcript_content, transcript_name, Transcript.mime_types[output_format]
|
||||
|
||||
|
||||
def get_transcript(course_id, block_id, lang=None, output_format=Transcript.SRT, is_bumper=False):
|
||||
def get_transcript(video, lang=None, output_format=Transcript.SRT, youtube_id=None):
|
||||
"""
|
||||
Get video transcript from edx-val or content store.
|
||||
|
||||
Arguments:
|
||||
course_id (CourseLocator): course identifier
|
||||
block_id (unicode): a unique identifier for an item in modulestore
|
||||
video (Video Descriptor): Video Descriptor
|
||||
lang (unicode): transcript language
|
||||
output_format (unicode): transcript output format
|
||||
is_bumper (bool): indicates bumper video
|
||||
youtube_id (unicode): youtube video id
|
||||
|
||||
Returns:
|
||||
tuple containing content, filename, mimetype
|
||||
"""
|
||||
usage_key = BlockUsageLocator(course_id, block_type='video', block_id=block_id)
|
||||
video_descriptor = modulestore().get_item(usage_key)
|
||||
transcripts_info = video.get_transcripts_info()
|
||||
if not lang:
|
||||
lang = video.get_default_transcript_language(transcripts_info)
|
||||
|
||||
try:
|
||||
return get_transcript_from_val(video_descriptor.edx_video_id, lang, output_format)
|
||||
edx_video_id = clean_video_id(video.edx_video_id)
|
||||
if not edx_video_id:
|
||||
raise NotFoundError
|
||||
return get_transcript_from_val(edx_video_id, lang, output_format)
|
||||
except NotFoundError:
|
||||
return get_transcript_from_contentstore(
|
||||
video_descriptor,
|
||||
video,
|
||||
lang,
|
||||
youtube_id=youtube_id,
|
||||
output_format=output_format,
|
||||
is_bumper=is_bumper
|
||||
transcripts_info=transcripts_info
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user