1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-15 23:52:36 +01:00

Merge pull request #764 from jaimeMF/subtitles_not_found

Fix crash when subtitles are not found
This commit is contained in:
Filippo Valsorda 2013-03-31 19:02:18 -07:00
commit f33154cd39
2 changed files with 41 additions and 23 deletions

View File

@ -485,6 +485,9 @@ def process_info(self, info_dict):
subtitle = info_dict['subtitles'][0] subtitle = info_dict['subtitles'][0]
(sub_error, sub_lang, sub) = subtitle (sub_error, sub_lang, sub) = subtitle
sub_format = self.params.get('subtitlesformat') sub_format = self.params.get('subtitlesformat')
if sub_error:
self.report_warning("Some error while getting the subtitles")
else:
try: try:
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
self.report_writesubtitles(sub_filename) self.report_writesubtitles(sub_filename)
@ -501,6 +504,9 @@ def process_info(self, info_dict):
sub_format = self.params.get('subtitlesformat') sub_format = self.params.get('subtitlesformat')
for subtitle in subtitles: for subtitle in subtitles:
(sub_error, sub_lang, sub) = subtitle (sub_error, sub_lang, sub) = subtitle
if sub_error:
self.report_warning("Some error while getting the subtitles")
else:
try: try:
sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format sub_filename = filename.rsplit('.', 1)[0] + u'.' + sub_lang + u'.' + sub_format
self.report_writesubtitles(sub_filename) self.report_writesubtitles(sub_filename)

View File

@ -253,11 +253,11 @@ def _get_available_subtitles(self, video_id):
try: try:
sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8') sub_list = compat_urllib_request.urlopen(request).read().decode('utf-8')
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
return (u'WARNING: unable to download video subtitles: %s' % compat_str(err), None) return (u'unable to download video subtitles: %s' % compat_str(err), None)
sub_lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list) 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) sub_lang_list = dict((l[1], l[0]) for l in sub_lang_list)
if not sub_lang_list: if not sub_lang_list:
return (u'WARNING: video doesn\'t have subtitles', None) return (u'video doesn\'t have subtitles', None)
return sub_lang_list return sub_lang_list
def _list_available_subtitles(self, video_id): def _list_available_subtitles(self, video_id):
@ -265,6 +265,10 @@ def _list_available_subtitles(self, video_id):
self.report_video_subtitles_available(video_id, sub_lang_list) self.report_video_subtitles_available(video_id, sub_lang_list)
def _request_subtitle(self, sub_lang, sub_name, video_id, format): def _request_subtitle(self, sub_lang, sub_name, video_id, format):
"""
Return tuple:
(error_message, sub_lang, sub)
"""
self.report_video_subtitles_request(video_id, sub_lang, format) self.report_video_subtitles_request(video_id, sub_lang, format)
params = compat_urllib_parse.urlencode({ params = compat_urllib_parse.urlencode({
'lang': sub_lang, 'lang': sub_lang,
@ -276,14 +280,20 @@ def _request_subtitle(self, sub_lang, sub_name, video_id, format):
try: try:
sub = compat_urllib_request.urlopen(url).read().decode('utf-8') sub = compat_urllib_request.urlopen(url).read().decode('utf-8')
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
return (u'WARNING: unable to download video subtitles: %s' % compat_str(err), None) return (u'unable to download video subtitles: %s' % compat_str(err), None, None)
if not sub: if not sub:
return (u'WARNING: Did not fetch video subtitles', None) return (u'Did not fetch video subtitles', None, None)
return (None, sub_lang, sub) return (None, sub_lang, sub)
def _extract_subtitle(self, video_id): def _extract_subtitle(self, video_id):
"""
Return a list with a tuple:
[(error_message, sub_lang, sub)]
"""
sub_lang_list = self._get_available_subtitles(video_id) sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat') 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 self._downloader.params.get('subtitleslang', False): if self._downloader.params.get('subtitleslang', False):
sub_lang = self._downloader.params.get('subtitleslang') sub_lang = self._downloader.params.get('subtitleslang')
elif 'en' in sub_lang_list: elif 'en' in sub_lang_list:
@ -291,7 +301,7 @@ def _extract_subtitle(self, video_id):
else: else:
sub_lang = list(sub_lang_list.keys())[0] sub_lang = list(sub_lang_list.keys())[0]
if not sub_lang in sub_lang_list: if not sub_lang in sub_lang_list:
return (u'WARNING: no closed captions found in the specified language "%s"' % sub_lang, None) return [(u'no closed captions found in the specified language "%s"' % sub_lang, None, None)]
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format) subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
return [subtitle] return [subtitle]
@ -299,6 +309,8 @@ def _extract_subtitle(self, video_id):
def _extract_all_subtitles(self, video_id): def _extract_all_subtitles(self, video_id):
sub_lang_list = self._get_available_subtitles(video_id) sub_lang_list = self._get_available_subtitles(video_id)
sub_format = self._downloader.params.get('subtitlesformat') 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)]
subtitles = [] subtitles = []
for sub_lang in sub_lang_list: 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) subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
@ -532,14 +544,14 @@ def _real_extract(self, url):
if video_subtitles: if video_subtitles:
(sub_error, sub_lang, sub) = video_subtitles[0] (sub_error, sub_lang, sub) = video_subtitles[0]
if sub_error: if sub_error:
self._downloader.trouble(sub_error) self._downloader.report_error(sub_error)
if self._downloader.params.get('allsubtitles', False): if self._downloader.params.get('allsubtitles', False):
video_subtitles = self._extract_all_subtitles(video_id) video_subtitles = self._extract_all_subtitles(video_id)
for video_subtitle in video_subtitles: for video_subtitle in video_subtitles:
(sub_error, sub_lang, sub) = video_subtitle (sub_error, sub_lang, sub) = video_subtitle
if sub_error: if sub_error:
self._downloader.trouble(sub_error) self._downloader.report_error(sub_error)
if self._downloader.params.get('listsubtitles', False): if self._downloader.params.get('listsubtitles', False):
sub_lang_list = self._list_available_subtitles(video_id) sub_lang_list = self._list_available_subtitles(video_id)