|
|
@ -423,7 +423,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
elif len(s) == 87:
|
|
|
|
elif len(s) == 87:
|
|
|
|
return s[6:27] + s[4] + s[28:39] + s[27] + s[40:59] + s[2] + s[60:]
|
|
|
|
return s[6:27] + s[4] + s[28:39] + s[27] + s[40:59] + s[2] + s[60:]
|
|
|
|
elif len(s) == 86:
|
|
|
|
elif len(s) == 86:
|
|
|
|
return s[5:20] + s[2] + s[21:]
|
|
|
|
return s[5:40] + s[3] + s[41:48] + s[0] + s[49:86]
|
|
|
|
elif len(s) == 85:
|
|
|
|
elif len(s) == 85:
|
|
|
|
return s[83:34:-1] + s[0] + s[33:27:-1] + s[3] + s[26:19:-1] + s[34] + s[18:3:-1] + s[27]
|
|
|
|
return s[83:34:-1] + s[0] + s[33:27:-1] + s[3] + s[26:19:-1] + s[34] + s[18:3:-1] + s[27]
|
|
|
|
elif len(s) == 84:
|
|
|
|
elif len(s) == 84:
|
|
|
@ -434,6 +434,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
return s[1:19] + s[0] + s[20:68] + s[19] + s[69:82]
|
|
|
|
return s[1:19] + s[0] + s[20:68] + s[19] + s[69:82]
|
|
|
|
elif len(s) == 81:
|
|
|
|
elif len(s) == 81:
|
|
|
|
return s[56] + s[79:56:-1] + s[41] + s[55:41:-1] + s[80] + s[40:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9]
|
|
|
|
return s[56] + s[79:56:-1] + s[41] + s[55:41:-1] + s[80] + s[40:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9]
|
|
|
|
|
|
|
|
elif len(s) == 80:
|
|
|
|
|
|
|
|
return s[1:19] + s[0] + s[20:68] + s[19] + s[69:80]
|
|
|
|
elif len(s) == 79:
|
|
|
|
elif len(s) == 79:
|
|
|
|
return s[54] + s[77:54:-1] + s[39] + s[53:39:-1] + s[78] + s[38:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9]
|
|
|
|
return s[54] + s[77:54:-1] + s[39] + s[53:39:-1] + s[78] + s[38:34:-1] + s[0] + s[33:29:-1] + s[34] + s[28:9:-1] + s[29] + s[8:0:-1] + s[9]
|
|
|
|
|
|
|
|
|
|
|
@ -456,11 +458,13 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
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'unable to download video subtitles: %s' % compat_str(err), None)
|
|
|
|
self._downloader.report_warning(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 = 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'video doesn\'t have subtitles', None)
|
|
|
|
self._downloader.report_warning(u'video doesn\'t have subtitles')
|
|
|
|
|
|
|
|
return {}
|
|
|
|
return sub_lang_list
|
|
|
|
return sub_lang_list
|
|
|
|
|
|
|
|
|
|
|
|
def _list_available_subtitles(self, video_id):
|
|
|
|
def _list_available_subtitles(self, video_id):
|
|
|
@ -469,8 +473,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
|
|
|
|
|
|
|
|
def _request_subtitle(self, sub_lang, sub_name, video_id, format):
|
|
|
|
def _request_subtitle(self, sub_lang, sub_name, video_id, format):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Return tuple:
|
|
|
|
Return the subtitle as a string or None if they are not found
|
|
|
|
(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({
|
|
|
@ -483,21 +486,24 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
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'unable to download video subtitles: %s' % compat_str(err), None, None)
|
|
|
|
self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
|
|
|
|
|
|
|
|
return
|
|
|
|
if not sub:
|
|
|
|
if not sub:
|
|
|
|
return (u'Did not fetch video subtitles', None, None)
|
|
|
|
self._downloader.report_warning(u'Did not fetch video subtitles')
|
|
|
|
return (None, sub_lang, sub)
|
|
|
|
return
|
|
|
|
|
|
|
|
return sub
|
|
|
|
|
|
|
|
|
|
|
|
def _request_automatic_caption(self, video_id, webpage):
|
|
|
|
def _request_automatic_caption(self, video_id, webpage):
|
|
|
|
"""We need the webpage for getting the captions url, pass it as an
|
|
|
|
"""We need the webpage for getting the captions url, pass it as an
|
|
|
|
argument to speed up the process."""
|
|
|
|
argument to speed up the process."""
|
|
|
|
sub_lang = self._downloader.params.get('subtitleslang') or 'en'
|
|
|
|
sub_lang = (self._downloader.params.get('subtitleslangs') or ['en'])[0]
|
|
|
|
sub_format = self._downloader.params.get('subtitlesformat')
|
|
|
|
sub_format = self._downloader.params.get('subtitlesformat')
|
|
|
|
self.to_screen(u'%s: Looking for automatic captions' % video_id)
|
|
|
|
self.to_screen(u'%s: Looking for automatic captions' % video_id)
|
|
|
|
mobj = re.search(r';ytplayer.config = ({.*?});', webpage)
|
|
|
|
mobj = re.search(r';ytplayer.config = ({.*?});', webpage)
|
|
|
|
err_msg = u'Couldn\'t find automatic captions for "%s"' % sub_lang
|
|
|
|
err_msg = u'Couldn\'t find automatic captions for "%s"' % sub_lang
|
|
|
|
if mobj is None:
|
|
|
|
if mobj is None:
|
|
|
|
return [(err_msg, None, None)]
|
|
|
|
self._downloader.report_warning(err_msg)
|
|
|
|
|
|
|
|
return {}
|
|
|
|
player_config = json.loads(mobj.group(1))
|
|
|
|
player_config = json.loads(mobj.group(1))
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
args = player_config[u'args']
|
|
|
|
args = player_config[u'args']
|
|
|
@ -512,40 +518,43 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
})
|
|
|
|
})
|
|
|
|
subtitles_url = caption_url + '&' + params
|
|
|
|
subtitles_url = caption_url + '&' + params
|
|
|
|
sub = self._download_webpage(subtitles_url, video_id, u'Downloading automatic captions')
|
|
|
|
sub = self._download_webpage(subtitles_url, video_id, u'Downloading automatic captions')
|
|
|
|
return [(None, sub_lang, sub)]
|
|
|
|
return {sub_lang: sub}
|
|
|
|
except KeyError:
|
|
|
|
# An extractor error can be raise by the download process if there are
|
|
|
|
return [(err_msg, None, None)]
|
|
|
|
# no automatic captions but there are subtitles
|
|
|
|
|
|
|
|
except (KeyError, ExtractorError):
|
|
|
|
def _extract_subtitle(self, video_id):
|
|
|
|
self._downloader.report_warning(err_msg)
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_subtitles(self, video_id):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Return a list with a tuple:
|
|
|
|
Return a dictionary: {language: subtitles} or {} if the subtitles
|
|
|
|
[(error_message, sub_lang, sub)]
|
|
|
|
couldn't be found
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
sub_lang_list = self._get_available_subtitles(video_id)
|
|
|
|
available_subs_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
|
|
|
|
if not available_subs_list: #There was some error, it didn't get the available subtitles
|
|
|
|
return [(sub_lang_list[0], None, None)]
|
|
|
|
return {}
|
|
|
|
if self._downloader.params.get('subtitleslang', False):
|
|
|
|
if self._downloader.params.get('allsubtitles', False):
|
|
|
|
sub_lang = self._downloader.params.get('subtitleslang')
|
|
|
|
sub_lang_list = available_subs_list
|
|
|
|
elif 'en' in sub_lang_list:
|
|
|
|
|
|
|
|
sub_lang = 'en'
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
sub_lang = list(sub_lang_list.keys())[0]
|
|
|
|
if self._downloader.params.get('subtitleslangs', False):
|
|
|
|
if not sub_lang in sub_lang_list:
|
|
|
|
reqested_langs = self._downloader.params.get('subtitleslangs')
|
|
|
|
return [(u'no closed captions found in the specified language "%s"' % sub_lang, None, None)]
|
|
|
|
elif 'en' in available_subs_list:
|
|
|
|
|
|
|
|
reqested_langs = ['en']
|
|
|
|
subtitle = self._request_subtitle(sub_lang, sub_lang_list[sub_lang].encode('utf-8'), video_id, sub_format)
|
|
|
|
else:
|
|
|
|
return [subtitle]
|
|
|
|
reqested_langs = [list(available_subs_list.keys())[0]]
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_all_subtitles(self, video_id):
|
|
|
|
sub_lang_list = {}
|
|
|
|
sub_lang_list = self._get_available_subtitles(video_id)
|
|
|
|
for sub_lang in reqested_langs:
|
|
|
|
sub_format = self._downloader.params.get('subtitlesformat')
|
|
|
|
if not sub_lang in available_subs_list:
|
|
|
|
if isinstance(sub_lang_list,tuple): #There was some error, it didn't get the available subtitles
|
|
|
|
self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
|
|
|
|
return [(sub_lang_list[0], None, None)]
|
|
|
|
continue
|
|
|
|
subtitles = []
|
|
|
|
sub_lang_list[sub_lang] = available_subs_list[sub_lang]
|
|
|
|
|
|
|
|
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)
|
|
|
|
subtitles.append(subtitle)
|
|
|
|
if subtitle:
|
|
|
|
|
|
|
|
subtitles[sub_lang] = subtitle
|
|
|
|
return subtitles
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
|
|
|
|
def _print_formats(self, formats):
|
|
|
|
def _print_formats(self, formats):
|
|
|
@ -736,25 +745,10 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
|
|
|
|
# subtitles
|
|
|
|
# subtitles
|
|
|
|
video_subtitles = None
|
|
|
|
video_subtitles = None
|
|
|
|
|
|
|
|
|
|
|
|
if self._downloader.params.get('writesubtitles', False):
|
|
|
|
if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
|
|
|
|
video_subtitles = self._extract_subtitle(video_id)
|
|
|
|
video_subtitles = self._extract_subtitles(video_id)
|
|
|
|
if video_subtitles:
|
|
|
|
elif self._downloader.params.get('writeautomaticsub', False):
|
|
|
|
(sub_error, sub_lang, sub) = video_subtitles[0]
|
|
|
|
|
|
|
|
if sub_error:
|
|
|
|
|
|
|
|
self._downloader.report_warning(sub_error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self._downloader.params.get('writeautomaticsub', False):
|
|
|
|
|
|
|
|
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
|
|
|
|
video_subtitles = self._request_automatic_caption(video_id, video_webpage)
|
|
|
|
(sub_error, sub_lang, sub) = video_subtitles[0]
|
|
|
|
|
|
|
|
if sub_error:
|
|
|
|
|
|
|
|
self._downloader.report_warning(sub_error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self._downloader.params.get('allsubtitles', False):
|
|
|
|
|
|
|
|
video_subtitles = self._extract_all_subtitles(video_id)
|
|
|
|
|
|
|
|
for video_subtitle in video_subtitles:
|
|
|
|
|
|
|
|
(sub_error, sub_lang, sub) = video_subtitle
|
|
|
|
|
|
|
|
if sub_error:
|
|
|
|
|
|
|
|
self._downloader.report_warning(sub_error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self._downloader.params.get('listsubtitles', False):
|
|
|
|
if self._downloader.params.get('listsubtitles', False):
|
|
|
|
self._list_available_subtitles(video_id)
|
|
|
|
self._list_available_subtitles(video_id)
|
|
|
|