Skip to content

Commit

Permalink
Fix exception when _captions_base_url is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
user234683 committed Mar 23, 2022
1 parent c478ba8 commit b1050e2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion youtube/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def make_caption_src(info, lang, auto=False, trans_lang=None):
if trans_lang:
label += ' -> ' + trans_lang
return {
'url': '/' + yt_data_extract.get_caption_url(info, lang, 'vtt', auto, trans_lang),
'url': util.prefix_url(yt_data_extract.get_caption_url(info, lang, 'vtt', auto, trans_lang)),
'label': label,
'srclang': trans_lang[0:2] if trans_lang else lang[0:2],
'on': False,
Expand Down Expand Up @@ -215,6 +215,8 @@ def get_subtitle_sources(info):
pref_lang (Automatic)
pref_lang (Manual)'''
sources = []
if not yt_data_extract.captions_available(info):
return []
pref_lang = settings.subtitles_language
native_video_lang = None
if info['automatic_caption_languages']:
Expand Down
2 changes: 1 addition & 1 deletion youtube/yt_data_extract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
update_with_age_restricted_info, requires_decryption,
extract_decryption_function, decrypt_signatures, _formats,
update_format_with_type_info, extract_hls_formats,
extract_watch_info_from_html)
extract_watch_info_from_html, captions_available)
5 changes: 5 additions & 0 deletions youtube/yt_data_extract/watch_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,15 @@ def extract_watch_info_from_html(watch_html):
return extract_watch_info(fake_polymer_json)


def captions_available(info):
return bool(info['_captions_base_url'])


def get_caption_url(info, language, format, automatic=False, translation_language=None):
'''Gets the url for captions with the given language and format. If automatic is True, get the automatic captions for that language. If translation_language is given, translate the captions from `language` to `translation_language`. If automatic is true and translation_language is given, the automatic captions will be translated.'''
url = info['_captions_base_url']
if not url:
return None
url += '&lang=' + language
url += '&fmt=' + format
if automatic:
Expand Down

0 comments on commit b1050e2

Please sign in to comment.