1
0
mirror of https://github.com/l1ving/youtube-dl synced 2025-02-11 15:12:57 +08:00

fix #1213 by returning an empty dictionary if there is no subtitle.

This commit is contained in:
Géraud Le Falher 2013-08-10 02:12:06 +02:00
parent 0f399e6e5e
commit 577dae1dfa

View File

@ -287,6 +287,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
},
},
]
_subtitle_error_message = u''
@classmethod
@ -375,12 +376,12 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
try:
sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
return (u'unable to download video subtitles: %s' % compat_str(err), None)
self._subtitle_error_message = u'unable to download video subtitles: %s' % compat_str(err)
return {}
sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list)
sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
if not sub_lang_list:
return (u'video doesn\'t have subtitles', None)
return sub_lang_list
if len(sub_lang_list) == 0:
self._subtitle_error_message = u'video doesn\'t have subtitles'
return dict((l[1], l[0]) for l in sub_lang_list)
def _list_available_subtitles(self, video_id):
sub_lang_list = self._get_available_subtitles(video_id)
@ -442,8 +443,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
"""
sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat')
if isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
return [(sub_lang_list[0], None, None)]
if not sub_lang_list: #There was some error, it didn't get the available subtitles
return [(self._subtitle_error_message, None, None)]
if self._downloader.params.get('subtitleslang', False):
sub_lang = self._downloader.params.get('subtitleslang')
elif 'en' in sub_lang_list:
@ -459,8 +460,9 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
def _extract_all_subtitles(self, video_id):
sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat')
if isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
return [(sub_lang_list[0], None, None)]
if not sub_lang_list: #There was some error, it didn't get the available subtitles
print(self._subtitle_error_message)
return [(self._subtitle_error_message, None, None)]
subtitles = []
for sub_lang in sub_lang_list:
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)