|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import logging |
| 10 | +from typing import Any |
10 | 11 |
|
| 12 | +from django.conf import settings |
11 | 13 | from opaque_keys.edx.keys import CourseKey, UsageKey |
12 | 14 | from xblocks_contrib.video.exceptions import TranscriptNotFoundError |
13 | 15 |
|
|
20 | 22 | from openedx.core.djangoapps.video_config.toggles import TRANSCRIPT_FEEDBACK |
21 | 23 | from openedx.core.djangoapps.video_pipeline.config.waffle import DEPRECATE_YOUTUBE |
22 | 24 |
|
| 25 | +from xmodule.exceptions import NotFoundError |
| 26 | + |
| 27 | + |
23 | 28 | log = logging.getLogger(__name__) |
24 | 29 |
|
25 | 30 |
|
@@ -112,11 +117,72 @@ def get_transcript( |
112 | 117 | """ |
113 | 118 | # Import here to avoid circular dependency |
114 | 119 | from openedx.core.djangoapps.video_config.transcripts_utils import get_transcript |
115 | | - from xmodule.exceptions import NotFoundError |
116 | 120 |
|
117 | 121 | try: |
118 | 122 | return get_transcript(video_block, lang, output_format, youtube_id) |
119 | 123 | except NotFoundError as exc: |
120 | 124 | raise TranscriptNotFoundError( |
121 | 125 | f"Failed to get transcript: {exc}" |
122 | 126 | ) from exc |
| 127 | + |
| 128 | + def available_translations( |
| 129 | + self, |
| 130 | + video_block, |
| 131 | + transcripts: dict[str, Any], |
| 132 | + verify_assets: bool | None = None, |
| 133 | + is_bumper: bool = False, |
| 134 | + ) -> list[str]: |
| 135 | + """ |
| 136 | + Return a list of language codes for which we have transcripts. |
| 137 | +
|
| 138 | + Arguments: |
| 139 | + video_block: The video XBlock instance |
| 140 | + transcripts (dict): A dict with all transcripts and a sub. |
| 141 | + verify_assets (boolean): If True, checks to ensure that the transcripts |
| 142 | + really exist in the contentstore. If False, we just look at the |
| 143 | + VideoBlock fields and do not query the contentstore. One reason |
| 144 | + we might do this is to avoid slamming contentstore() with queries |
| 145 | + when trying to make a listing of videos and their languages. |
| 146 | +
|
| 147 | + Defaults to `not FALLBACK_TO_ENGLISH_TRANSCRIPTS`. |
| 148 | +
|
| 149 | + is_bumper (boolean): If True, indicates this is a bumper video. |
| 150 | +
|
| 151 | + Returns: |
| 152 | + list[str]: List of language codes for available transcripts. |
| 153 | + """ |
| 154 | + from openedx.core.djangoapps.video_config.transcripts_utils import ( |
| 155 | + get_transcript_for_video, |
| 156 | + get_transcript, |
| 157 | + ) |
| 158 | + |
| 159 | + translations = [] |
| 160 | + if verify_assets is None: |
| 161 | + verify_assets = not settings.FEATURES.get('FALLBACK_TO_ENGLISH_TRANSCRIPTS') |
| 162 | + |
| 163 | + sub, other_langs = transcripts["sub"], transcripts["transcripts"] |
| 164 | + |
| 165 | + if verify_assets: |
| 166 | + all_langs = dict(**other_langs) |
| 167 | + if sub: |
| 168 | + all_langs.update({'en': sub}) |
| 169 | + |
| 170 | + for language, filename in all_langs.items(): |
| 171 | + try: |
| 172 | + # for bumper videos, transcripts are stored in content store only |
| 173 | + if is_bumper: |
| 174 | + get_transcript_for_video(video_block.location, filename, filename, language) |
| 175 | + else: |
| 176 | + get_transcript(video_block, language) |
| 177 | + except NotFoundError: |
| 178 | + continue |
| 179 | + |
| 180 | + translations.append(language) |
| 181 | + else: |
| 182 | + # If we're not verifying the assets, we just trust our field values |
| 183 | + translations = list(other_langs) |
| 184 | + if not translations or sub: |
| 185 | + translations += ['en'] |
| 186 | + |
| 187 | + # to clean redundant language codes. |
| 188 | + return list(set(translations)) |
0 commit comments