chore: move get_transcript method to video service (#37635)
https://github.com/openedx/public-engineering/issues/445
This commit is contained in:
committed by
GitHub
parent
d592784b35
commit
b57e2ac1ea
@@ -56,13 +56,14 @@ from openedx.core.djangoapps.video_config.transcripts_utils import (
|
||||
clean_video_id,
|
||||
get_endonym_or_label,
|
||||
get_html5_ids,
|
||||
get_transcript,
|
||||
subs_filename
|
||||
)
|
||||
from .video_handlers import VideoStudentViewHandlers, VideoStudioViewHandlers
|
||||
from .video_utils import create_youtube_string, format_xml_exception_message, get_poster, rewrite_video_url
|
||||
from .video_xfields import VideoFields
|
||||
|
||||
from xblocks_contrib.video.exceptions import TranscriptNotFoundError
|
||||
|
||||
# The following import/except block for edxval is temporary measure until
|
||||
# edxval is a proper XBlock Runtime Service.
|
||||
#
|
||||
@@ -652,13 +653,15 @@ class _BuiltInVideoBlock(
|
||||
# construct transcripts info and also find if `en` subs exist
|
||||
transcripts_info = self.get_transcripts_info()
|
||||
possible_sub_ids = [self.sub, self.youtube_id_1_0] + get_html5_ids(self.html5_sources)
|
||||
for sub_id in possible_sub_ids:
|
||||
try:
|
||||
_, sub_id, _ = get_transcript(self, lang='en', output_format=Transcript.TXT)
|
||||
transcripts_info['transcripts'] = dict(transcripts_info['transcripts'], en=sub_id)
|
||||
break
|
||||
except NotFoundError:
|
||||
continue
|
||||
video_config_service = self.runtime.service(self, 'video_config')
|
||||
if video_config_service:
|
||||
for sub_id in possible_sub_ids:
|
||||
try:
|
||||
_, sub_id, _ = video_config_service.get_transcript(self, lang='en', output_format=Transcript.TXT)
|
||||
transcripts_info['transcripts'] = dict(transcripts_info['transcripts'], en=sub_id)
|
||||
break
|
||||
except TranscriptNotFoundError:
|
||||
continue
|
||||
|
||||
editable_fields['transcripts']['value'] = transcripts_info['transcripts']
|
||||
editable_fields['transcripts']['urlRoot'] = self.runtime.handler_url(
|
||||
@@ -1091,12 +1094,16 @@ class _BuiltInVideoBlock(
|
||||
|
||||
def _update_transcript_for_index(language=None):
|
||||
""" Find video transcript - if not found, don't update index """
|
||||
try:
|
||||
transcript = get_transcript(self, lang=language, output_format=Transcript.TXT)[0].replace("\n", " ")
|
||||
transcript_index_name = f"transcript_{language if language else self.transcript_language}"
|
||||
video_body.update({transcript_index_name: transcript})
|
||||
except NotFoundError:
|
||||
pass
|
||||
video_config_service = self.runtime.service(self, 'video_config')
|
||||
if video_config_service:
|
||||
try:
|
||||
transcript = video_config_service.get_transcript(
|
||||
self, lang=language, output_format=Transcript.TXT
|
||||
)[0].replace("\n", " ")
|
||||
transcript_index_name = f"transcript_{language if language else self.transcript_language}"
|
||||
video_body.update({transcript_index_name: transcript})
|
||||
except TranscriptNotFoundError:
|
||||
pass
|
||||
|
||||
if self.sub:
|
||||
_update_transcript_for_index()
|
||||
|
||||
@@ -25,21 +25,44 @@ from openedx.core.djangoapps.content_libraries import api as lib_api
|
||||
from openedx.core.djangoapps.video_config.transcripts_utils import (
|
||||
Transcript,
|
||||
TranscriptException,
|
||||
TranscriptsGenerationException,
|
||||
clean_video_id,
|
||||
generate_sjson_for_all_speeds,
|
||||
get_html5_ids,
|
||||
get_or_create_sjson,
|
||||
get_transcript,
|
||||
get_transcript_from_contentstore,
|
||||
remove_subs_from_store,
|
||||
subs_filename,
|
||||
youtube_speed_dict
|
||||
)
|
||||
from xblocks_contrib.video.exceptions import (
|
||||
TranscriptsGenerationException,
|
||||
TranscriptNotFoundError,
|
||||
)
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_transcript(
|
||||
video_block,
|
||||
lang: str | None = None,
|
||||
output_format: str = 'srt',
|
||||
youtube_id: str | None = None,
|
||||
) -> tuple[bytes, str, str]:
|
||||
"""
|
||||
Retrieve a transcript using a video block's configuration service.
|
||||
|
||||
Returns:
|
||||
tuple(bytes, str, str): transcript content, filename, and mimetype.
|
||||
|
||||
Raises:
|
||||
Exception: If the video config service is not available or the transcript cannot be retrieved.
|
||||
"""
|
||||
video_config_service = video_block.runtime.service(video_block, 'video_config')
|
||||
if not video_config_service:
|
||||
raise Exception("Video config service not found")
|
||||
return video_config_service.get_transcript(video_block, lang, output_format, youtube_id)
|
||||
|
||||
|
||||
# Disable no-member warning:
|
||||
# pylint: disable=no-member
|
||||
|
||||
@@ -356,7 +379,7 @@ class VideoStudentViewHandlers:
|
||||
mimetype,
|
||||
add_attachment_header=False
|
||||
)
|
||||
except NotFoundError as exc:
|
||||
except (NotFoundError, TranscriptNotFoundError) as exc:
|
||||
edx_video_id = clean_video_id(self.edx_video_id)
|
||||
log.warning(
|
||||
'[Translation Dispatch] %s: %s',
|
||||
@@ -370,7 +393,7 @@ class VideoStudentViewHandlers:
|
||||
|
||||
try:
|
||||
content, filename, mimetype = get_transcript(self, lang, output_format=self.transcript_download_format)
|
||||
except NotFoundError:
|
||||
except TranscriptNotFoundError:
|
||||
return Response(status=404)
|
||||
|
||||
response = self.make_transcript_http_response(
|
||||
@@ -660,8 +683,11 @@ class VideoStudioViewHandlers:
|
||||
return Response(json={'error': _('Language is required.')}, status=400)
|
||||
|
||||
try:
|
||||
transcript_content, transcript_name, mime_type = get_transcript(
|
||||
video=self, lang=language, output_format=Transcript.SRT
|
||||
video_config_service = self.runtime.service(self, 'video_config')
|
||||
if not video_config_service:
|
||||
return Response(status=404)
|
||||
transcript_content, transcript_name, mime_type = video_config_service.get_transcript(
|
||||
self, lang=language, output_format=Transcript.SRT
|
||||
)
|
||||
response = Response(transcript_content, headerlist=[
|
||||
(
|
||||
@@ -671,6 +697,10 @@ class VideoStudioViewHandlers:
|
||||
('Content-Language', language),
|
||||
('Content-Type', mime_type)
|
||||
])
|
||||
except (UnicodeDecodeError, TranscriptsGenerationException, NotFoundError):
|
||||
except (
|
||||
UnicodeDecodeError,
|
||||
TranscriptsGenerationException,
|
||||
TranscriptNotFoundError
|
||||
):
|
||||
response = Response(status=404)
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user