8 from .common
import InfoExtractor
, SearchInfoExtractor
14 compat_urllib_request
,
25 class YoutubeIE(InfoExtractor
):
26 """Information extractor for youtube.com."""
30 (?:https?://)? # http(s):// (optional)
31 (?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/|
32 tube\.majestyc\.net/) # the various hostnames, with wildcard subdomains
33 (?:.*?\#/)? # handle anchor (#/) redirect urls
34 (?: # the various things that can precede the ID:
35 (?:(?:v|embed|e)/) # v/ or embed/ or e/
36 |(?: # or the v= param in all its forms
37 (?:watch(?:_popup)?(?:\.php)?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
38 (?:\?|\#!?) # the params delimiter ? or # or #!
39 (?:.*?&)? # any other preceding param (like /?s=tuff&v=xxxx)
42 )? # optional -> youtube.com/xxxx is OK
43 )? # all until now is optional -> you can pass the naked ID
44 ([0-9A-Za-z_-]+) # here is it! the YouTube video ID
45 (?(1).+)? # if we found the ID, everything can follow
47 _LANG_URL
= r
'https://www.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
48 _LOGIN_URL
= 'https://accounts.google.com/ServiceLogin'
49 _AGE_URL
= 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
50 _NEXT_URL_RE
= r
'[\?&]next_url=([^&]+)'
51 _NETRC_MACHINE
= 'youtube'
52 # Listed in order of quality
53 _available_formats
= ['38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13']
54 _available_formats_prefer_free
= ['38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13']
86 def suitable(cls
, url
):
87 """Receives a URL and returns True if suitable for this IE."""
88 if YoutubePlaylistIE
.suitable(url
): return False
89 return re
.match(cls
._VALID
_URL
, url
, re
.VERBOSE
) is not None
91 def report_lang(self
):
92 """Report attempt to set language."""
93 self
.to_screen(u
'Setting language')
95 def report_login(self
):
96 """Report attempt to log in."""
97 self
.to_screen(u
'Logging in')
99 def report_video_webpage_download(self
, video_id
):
100 """Report attempt to download video webpage."""
101 self
.to_screen(u
'%s: Downloading video webpage' % video_id
)
103 def report_video_info_webpage_download(self
, video_id
):
104 """Report attempt to download video info webpage."""
105 self
.to_screen(u
'%s: Downloading video info webpage' % video_id
)
107 def report_video_subtitles_download(self
, video_id
):
108 """Report attempt to download video info webpage."""
109 self
.to_screen(u
'%s: Checking available subtitles' % video_id
)
111 def report_video_subtitles_request(self
, video_id
, sub_lang
, format
):
112 """Report attempt to download video info webpage."""
113 self
.to_screen(u
'%s: Downloading video subtitles for %s.%s' % (video_id
, sub_lang
, format
))
115 def report_video_subtitles_available(self
, video_id
, sub_lang_list
):
116 """Report available subtitles."""
117 sub_lang
= ",".join(list(sub_lang_list
.keys()))
118 self
.to_screen(u
'%s: Available subtitles for video: %s' % (video_id
, sub_lang
))
120 def report_information_extraction(self
, video_id
):
121 """Report attempt to extract video information."""
122 self
.to_screen(u
'%s: Extracting video information' % video_id
)
124 def report_unavailable_format(self
, video_id
, format
):
125 """Report extracted video URL."""
126 self
.to_screen(u
'%s: Format %s not available' % (video_id
, format
))
128 def report_rtmp_download(self
):
129 """Indicate the download will use the RTMP protocol."""
130 self
.to_screen(u
'RTMP download detected')
132 def _decrypt_signature(self
, s
):
133 """Decrypt the key the two subkeys must have a length of 43"""
134 if self
._downloader
.params
.get('verbose'):
135 self
.to_screen('encrypted signature length %d' % (len(s
)))
138 return s
[48] + s
[81] + s
[80] + s
[79] + s
[78] + s
[77] + s
[76] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[82] + s
[66] + s
[65] + s
[64] + s
[63] + s
[85] + s
[61] + s
[60] + s
[59] + s
[58] + s
[57] + s
[56] + s
[55] + s
[54] + s
[53] + s
[52] + s
[51] + s
[50] + s
[49] + s
[67] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[40] + s
[39] + s
[38] + s
[37] + s
[36] + s
[35] + s
[34] + s
[33] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[26] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[3] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[2] + s
[12]
140 return s
[62] + s
[82] + s
[81] + s
[80] + s
[79] + s
[78] + s
[77] + s
[76] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[67] + s
[66] + s
[65] + s
[64] + s
[63] + s
[83] + s
[61] + s
[60] + s
[59] + s
[58] + s
[57] + s
[56] + s
[55] + s
[54] + s
[53] + s
[0] + s
[51] + s
[50] + s
[49] + s
[48] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[40] + s
[39] + s
[38] + s
[37] + s
[36] + s
[35] + s
[34] + s
[33] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[26] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[12] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[3]
142 return s
[2] + s
[3] + s
[4] + s
[5] + s
[6] + s
[7] + s
[8] + s
[9] + s
[10] + s
[11] + s
[12] + s
[13] + s
[14] + s
[15] + s
[16] + s
[0] + s
[18] + s
[19] + s
[20] + s
[21] + s
[22] + s
[23] + s
[24] + s
[25] + s
[26] + s
[27] + s
[28] + s
[29] + s
[30] + s
[31] + s
[32] + s
[33] + s
[34] + s
[35] + s
[36] + s
[37] + s
[38] + s
[39] + s
[40] + s
[79] + s
[42] + s
[43] + s
[44] + s
[45] + s
[46] + s
[47] + s
[48] + s
[49] + s
[50] + s
[51] + s
[52] + s
[53] + s
[54] + s
[55] + s
[56] + s
[57] + s
[58] + s
[59] + s
[60] + s
[61] + s
[62] + s
[63] + s
[64] + s
[65] + s
[66] + s
[67] + s
[68] + s
[69] + s
[70] + s
[71] + s
[72] + s
[73] + s
[74] + s
[75] + s
[76] + s
[77] + s
[78] + s
[82] + s
[80] + s
[81] + s
[41]
144 return s
[76] + s
[82] + s
[81] + s
[80] + s
[79] + s
[78] + s
[77] + s
[83] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[67] + s
[66] + s
[65] + s
[64] + s
[63] + s
[62] + s
[61] + s
[0] + s
[59] + s
[58] + s
[57] + s
[56] + s
[55] + s
[54] + s
[53] + s
[52] + s
[51] + s
[1] + s
[49] + s
[48] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[40] + s
[39] + s
[38] + s
[37] + s
[36] + s
[35] + s
[34] + s
[33] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[26] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[12] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[3]
146 return s
[83] + s
[82] + s
[81] + s
[80] + s
[79] + s
[78] + s
[77] + s
[76] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[67] + s
[66] + s
[65] + s
[64] + s
[63] + s
[62] + s
[61] + s
[60] + s
[59] + s
[58] + s
[57] + s
[56] + s
[55] + s
[54] + s
[53] + s
[52] + s
[51] + s
[50] + s
[49] + s
[48] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[40] + s
[39] + s
[38] + s
[37] + s
[2] + s
[35] + s
[34] + s
[33] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[3] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[12] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[26]
148 return s
[52] + s
[81] + s
[80] + s
[79] + s
[78] + s
[77] + s
[76] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[67] + s
[66] + s
[65] + s
[64] + s
[63] + s
[62] + s
[61] + s
[60] + s
[59] + s
[58] + s
[57] + s
[56] + s
[2] + s
[54] + s
[53] + s
[82] + s
[51] + s
[50] + s
[49] + s
[48] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[40] + s
[39] + s
[38] + s
[37] + s
[55] + s
[35] + s
[34] + s
[33] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[26] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[12] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[3] + s
[36]
150 return s
[36] + s
[79] + s
[78] + s
[77] + s
[76] + s
[75] + s
[74] + s
[73] + s
[72] + s
[71] + s
[70] + s
[69] + s
[68] + s
[81] + s
[66] + s
[65] + s
[64] + s
[63] + s
[62] + s
[61] + s
[60] + s
[59] + s
[58] + s
[57] + s
[56] + s
[55] + s
[54] + s
[53] + s
[52] + s
[51] + s
[50] + s
[49] + s
[48] + s
[47] + s
[46] + s
[45] + s
[44] + s
[43] + s
[42] + s
[41] + s
[33] + s
[39] + s
[38] + s
[37] + s
[40] + s
[35] + s
[0] + s
[67] + s
[32] + s
[31] + s
[30] + s
[29] + s
[28] + s
[27] + s
[26] + s
[25] + s
[24] + s
[23] + s
[22] + s
[21] + s
[20] + s
[19] + s
[18] + s
[17] + s
[16] + s
[15] + s
[14] + s
[13] + s
[12] + s
[11] + s
[10] + s
[9] + s
[8] + s
[7] + s
[6] + s
[5] + s
[4] + s
[3] + s
[2] + s
[1] + s
[34]
152 raise ExtractorError(u
'Unable to decrypt signature, subkeys length %d not supported; retrying might work' % (len(s
)))
154 def _get_available_subtitles(self
, video_id
):
155 self
.report_video_subtitles_download(video_id
)
156 request
= compat_urllib_request
.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id
)
158 sub_list
= compat_urllib_request
.urlopen(request
).read().decode('utf-8')
159 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
160 return (u
'unable to download video subtitles: %s' % compat_str(err
), None)
161 sub_lang_list
= re
.findall(r
'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list
)
162 sub_lang_list
= dict((l
[1], l
[0]) for l
in sub_lang_list
)
163 if not sub_lang_list
:
164 return (u
'video doesn\'t have subtitles', None)
167 def _list_available_subtitles(self
, video_id
):
168 sub_lang_list
= self
._get
_available
_subtitles
(video_id
)
169 self
.report_video_subtitles_available(video_id
, sub_lang_list
)
171 def _request_subtitle(self
, sub_lang
, sub_name
, video_id
, format
):
174 (error_message, sub_lang, sub)
176 self
.report_video_subtitles_request(video_id
, sub_lang
, format
)
177 params
= compat_urllib_parse
.urlencode({
183 url
= 'http://www.youtube.com/api/timedtext?' + params
185 sub
= compat_urllib_request
.urlopen(url
).read().decode('utf-8')
186 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
187 return (u
'unable to download video subtitles: %s' % compat_str(err
), None, None)
189 return (u
'Did not fetch video subtitles', None, None)
190 return (None, sub_lang
, sub
)
192 def _request_automatic_caption(self
, video_id
, webpage
):
193 """We need the webpage for getting the captions url, pass it as an
194 argument to speed up the process."""
195 sub_lang
= self
._downloader
.params
.get('subtitleslang') or 'en'
196 sub_format
= self
._downloader
.params
.get('subtitlesformat')
197 self
.to_screen(u
'%s: Looking for automatic captions' % video_id
)
198 mobj
= re
.search(r
';ytplayer.config = ({.*?});', webpage
)
199 err_msg
= u
'Couldn\'t find automatic captions for "%s"' % sub_lang
201 return [(err_msg
, None, None)]
202 player_config
= json
.loads(mobj
.group(1))
204 args
= player_config
[u
'args']
205 caption_url
= args
[u
'ttsurl']
206 timestamp
= args
[u
'timestamp']
207 params
= compat_urllib_parse
.urlencode({
214 subtitles_url
= caption_url
+ '&' + params
215 sub
= self
._download
_webpage
(subtitles_url
, video_id
, u
'Downloading automatic captions')
216 return [(None, sub_lang
, sub
)]
218 return [(err_msg
, None, None)]
220 def _extract_subtitle(self
, video_id
):
222 Return a list with a tuple:
223 [(error_message, sub_lang, sub)]
225 sub_lang_list
= self
._get
_available
_subtitles
(video_id
)
226 sub_format
= self
._downloader
.params
.get('subtitlesformat')
227 if isinstance(sub_lang_list
,tuple): #There was some error, it didn't get the available subtitles
228 return [(sub_lang_list
[0], None, None)]
229 if self
._downloader
.params
.get('subtitleslang', False):
230 sub_lang
= self
._downloader
.params
.get('subtitleslang')
231 elif 'en' in sub_lang_list
:
234 sub_lang
= list(sub_lang_list
.keys())[0]
235 if not sub_lang
in sub_lang_list
:
236 return [(u
'no closed captions found in the specified language "%s"' % sub_lang
, None, None)]
238 subtitle
= self
._request
_subtitle
(sub_lang
, sub_lang_list
[sub_lang
].encode('utf-8'), video_id
, sub_format
)
241 def _extract_all_subtitles(self
, video_id
):
242 sub_lang_list
= self
._get
_available
_subtitles
(video_id
)
243 sub_format
= self
._downloader
.params
.get('subtitlesformat')
244 if isinstance(sub_lang_list
,tuple): #There was some error, it didn't get the available subtitles
245 return [(sub_lang_list
[0], None, None)]
247 for sub_lang
in sub_lang_list
:
248 subtitle
= self
._request
_subtitle
(sub_lang
, sub_lang_list
[sub_lang
].encode('utf-8'), video_id
, sub_format
)
249 subtitles
.append(subtitle
)
252 def _print_formats(self
, formats
):
253 print('Available formats:')
255 print('%s\t:\t%s\t[%s]' %(x
, self
._video
_extensions
.get(x
, 'flv'), self
._video
_dimensions
.get(x
, '???')))
257 def _real_initialize(self
):
258 if self
._downloader
is None:
263 downloader_params
= self
._downloader
.params
265 # Attempt to use provided username and password or .netrc data
266 if downloader_params
.get('username', None) is not None:
267 username
= downloader_params
['username']
268 password
= downloader_params
['password']
269 elif downloader_params
.get('usenetrc', False):
271 info
= netrc
.netrc().authenticators(self
._NETRC
_MACHINE
)
276 raise netrc
.NetrcParseError('No authenticators for %s' % self
._NETRC
_MACHINE
)
277 except (IOError, netrc
.NetrcParseError
) as err
:
278 self
._downloader
.report_warning(u
'parsing .netrc: %s' % compat_str(err
))
282 request
= compat_urllib_request
.Request(self
._LANG
_URL
)
285 compat_urllib_request
.urlopen(request
).read()
286 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
287 self
._downloader
.report_warning(u
'unable to set language: %s' % compat_str(err
))
290 # No authentication to be performed
294 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
)
296 login_page
= compat_urllib_request
.urlopen(request
).read().decode('utf-8')
297 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
298 self
._downloader
.report_warning(u
'unable to fetch login page: %s' % compat_str(err
))
303 match
= re
.search(re
.compile(r
'<input.+?name="GALX".+?value="(.+?)"', re
.DOTALL
), login_page
)
305 galx
= match
.group(1)
307 match
= re
.search(re
.compile(r
'<input.+?name="dsh".+?value="(.+?)"', re
.DOTALL
), login_page
)
313 u
'continue': u
'https://www.youtube.com/signin?action_handle_signin=true&feature=sign_in_button&hl=en_US&nomobiletemp=1',
317 u
'PersistentCookie': u
'yes',
319 u
'bgresponse': u
'js_disabled',
320 u
'checkConnection': u
'',
321 u
'checkedDomains': u
'youtube',
327 u
'signIn': u
'Sign in',
329 u
'service': u
'youtube',
333 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
335 login_form
= dict((k
.encode('utf-8'), v
.encode('utf-8')) for k
,v
in login_form_strs
.items())
336 login_data
= compat_urllib_parse
.urlencode(login_form
).encode('ascii')
337 request
= compat_urllib_request
.Request(self
._LOGIN
_URL
, login_data
)
340 login_results
= compat_urllib_request
.urlopen(request
).read().decode('utf-8')
341 if re
.search(r
'(?i)<form[^>]* id="gaia_loginform"', login_results
) is not None:
342 self
._downloader
.report_warning(u
'unable to log in: bad username or password')
344 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
345 self
._downloader
.report_warning(u
'unable to log in: %s' % compat_str(err
))
351 'action_confirm': 'Confirm',
353 request
= compat_urllib_request
.Request(self
._AGE
_URL
, compat_urllib_parse
.urlencode(age_form
))
355 self
.report_age_confirmation()
356 compat_urllib_request
.urlopen(request
).read().decode('utf-8')
357 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
358 raise ExtractorError(u
'Unable to confirm age: %s' % compat_str(err
))
360 def _extract_id(self
, url
):
361 mobj
= re
.match(self
._VALID
_URL
, url
, re
.VERBOSE
)
363 raise ExtractorError(u
'Invalid URL: %s' % url
)
364 video_id
= mobj
.group(2)
367 def _real_extract(self
, url
):
368 # Extract original video URL from URL with redirection, like age verification, using next_url parameter
369 mobj
= re
.search(self
._NEXT
_URL
_RE
, url
)
371 url
= 'https://www.youtube.com/' + compat_urllib_parse
.unquote(mobj
.group(1)).lstrip('/')
372 video_id
= self
._extract
_id
(url
)
375 self
.report_video_webpage_download(video_id
)
376 url
= 'https://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1' % video_id
377 request
= compat_urllib_request
.Request(url
)
379 video_webpage_bytes
= compat_urllib_request
.urlopen(request
).read()
380 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
381 raise ExtractorError(u
'Unable to download video webpage: %s' % compat_str(err
))
383 video_webpage
= video_webpage_bytes
.decode('utf-8', 'ignore')
385 # Attempt to extract SWF player URL
386 mobj
= re
.search(r
'swfConfig.*?"(http:\\/\\/.*?watch.*?-.*?\.swf)"', video_webpage
)
388 player_url
= re
.sub(r
'\\(.)', r
'\1', mobj
.group(1))
393 self
.report_video_info_webpage_download(video_id
)
394 for el_type
in ['&el=embedded', '&el=detailpage', '&el=vevo', '']:
395 video_info_url
= ('https://www.youtube.com/get_video_info?&video_id=%s%s&ps=default&eurl=&gl=US&hl=en'
396 % (video_id
, el_type
))
397 video_info_webpage
= self
._download
_webpage
(video_info_url
, video_id
,
399 errnote
='unable to download video info webpage')
400 video_info
= compat_parse_qs(video_info_webpage
)
401 if 'token' in video_info
:
403 if 'token' not in video_info
:
404 if 'reason' in video_info
:
405 raise ExtractorError(u
'YouTube said: %s' % video_info
['reason'][0])
407 raise ExtractorError(u
'"token" parameter not in video info for unknown reason')
409 # Check for "rental" videos
410 if 'ypc_video_rental_bar_text' in video_info
and 'author' not in video_info
:
411 raise ExtractorError(u
'"rental" videos not supported')
413 # Start extracting information
414 self
.report_information_extraction(video_id
)
417 if 'author' not in video_info
:
418 raise ExtractorError(u
'Unable to extract uploader name')
419 video_uploader
= compat_urllib_parse
.unquote_plus(video_info
['author'][0])
422 video_uploader_id
= None
423 mobj
= re
.search(r
'<link itemprop="url" href="http://www.youtube.com/(?:user|channel)/([^"]+)">', video_webpage
)
425 video_uploader_id
= mobj
.group(1)
427 self
._downloader
.report_warning(u
'unable to extract uploader nickname')
430 if 'title' not in video_info
:
431 raise ExtractorError(u
'Unable to extract video title')
432 video_title
= compat_urllib_parse
.unquote_plus(video_info
['title'][0])
435 if 'thumbnail_url' not in video_info
:
436 self
._downloader
.report_warning(u
'unable to extract video thumbnail')
438 else: # don't panic if we can't find it
439 video_thumbnail
= compat_urllib_parse
.unquote_plus(video_info
['thumbnail_url'][0])
443 mobj
= re
.search(r
'id="eow-date.*?>(.*?)</span>', video_webpage
, re
.DOTALL
)
445 upload_date
= ' '.join(re
.sub(r
'[/,-]', r
' ', mobj
.group(1)).split())
446 upload_date
= unified_strdate(upload_date
)
449 video_description
= get_element_by_id("eow-description", video_webpage
)
450 if video_description
:
451 video_description
= clean_html(video_description
)
453 fd_mobj
= re
.search(r
'<meta name="description" content="([^"]+)"', video_webpage
)
455 video_description
= unescapeHTML(fd_mobj
.group(1))
457 video_description
= u
''
460 video_subtitles
= None
462 if self
._downloader
.params
.get('writesubtitles', False):
463 video_subtitles
= self
._extract
_subtitle
(video_id
)
465 (sub_error
, sub_lang
, sub
) = video_subtitles
[0]
467 self
._downloader
.report_warning(sub_error
)
469 if self
._downloader
.params
.get('writeautomaticsub', False):
470 video_subtitles
= self
._request
_automatic
_caption
(video_id
, video_webpage
)
471 (sub_error
, sub_lang
, sub
) = video_subtitles
[0]
473 self
._downloader
.report_warning(sub_error
)
475 if self
._downloader
.params
.get('allsubtitles', False):
476 video_subtitles
= self
._extract
_all
_subtitles
(video_id
)
477 for video_subtitle
in video_subtitles
:
478 (sub_error
, sub_lang
, sub
) = video_subtitle
480 self
._downloader
.report_warning(sub_error
)
482 if self
._downloader
.params
.get('listsubtitles', False):
483 self
._list
_available
_subtitles
(video_id
)
486 if 'length_seconds' not in video_info
:
487 self
._downloader
.report_warning(u
'unable to extract video duration')
490 video_duration
= compat_urllib_parse
.unquote_plus(video_info
['length_seconds'][0])
492 # Decide which formats to download
493 req_format
= self
._downloader
.params
.get('format', None)
496 mobj
= re
.search(r
';ytplayer.config = ({.*?});', video_webpage
)
498 raise ValueError('Could not find vevo ID')
499 info
= json
.loads(mobj
.group(1))
501 # Easy way to know if the 's' value is in url_encoded_fmt_stream_map
502 # this signatures are encrypted
503 m_s
= re
.search(r
'[&,]s=', args
['url_encoded_fmt_stream_map'])
505 self
.to_screen(u
'%s: Encrypted signatures detected.' % video_id
)
506 video_info
['url_encoded_fmt_stream_map'] = [args
['url_encoded_fmt_stream_map']]
510 if 'conn' in video_info
and video_info
['conn'][0].startswith('rtmp'):
511 self
.report_rtmp_download()
512 video_url_list
= [(None, video_info
['conn'][0])]
513 elif 'url_encoded_fmt_stream_map' in video_info
and len(video_info
['url_encoded_fmt_stream_map']) >= 1:
515 for url_data_str
in video_info
['url_encoded_fmt_stream_map'][0].split(','):
516 url_data
= compat_parse_qs(url_data_str
)
517 if 'itag' in url_data
and 'url' in url_data
:
518 url
= url_data
['url'][0]
519 if 'sig' in url_data
:
520 url
+= '&signature=' + url_data
['sig'][0]
521 elif 's' in url_data
:
522 signature
= self
._decrypt
_signature
(url_data
['s'][0])
523 url
+= '&signature=' + signature
524 if 'ratebypass' not in url
:
525 url
+= '&ratebypass=yes'
526 url_map
[url_data
['itag'][0]] = url
528 format_limit
= self
._downloader
.params
.get('format_limit', None)
529 available_formats
= self
._available
_formats
_prefer
_free
if self
._downloader
.params
.get('prefer_free_formats', False) else self
._available
_formats
530 if format_limit
is not None and format_limit
in available_formats
:
531 format_list
= available_formats
[available_formats
.index(format_limit
):]
533 format_list
= available_formats
534 existing_formats
= [x
for x
in format_list
if x
in url_map
]
535 if len(existing_formats
) == 0:
536 raise ExtractorError(u
'no known formats available for video')
537 if self
._downloader
.params
.get('listformats', None):
538 self
._print
_formats
(existing_formats
)
540 if req_format
is None or req_format
== 'best':
541 video_url_list
= [(existing_formats
[0], url_map
[existing_formats
[0]])] # Best quality
542 elif req_format
== 'worst':
543 video_url_list
= [(existing_formats
[len(existing_formats
)-1], url_map
[existing_formats
[len(existing_formats
)-1]])] # worst quality
544 elif req_format
in ('-1', 'all'):
545 video_url_list
= [(f
, url_map
[f
]) for f
in existing_formats
] # All formats
547 # Specific formats. We pick the first in a slash-delimeted sequence.
548 # For example, if '1/2/3/4' is requested and '2' and '4' are available, we pick '2'.
549 req_formats
= req_format
.split('/')
550 video_url_list
= None
551 for rf
in req_formats
:
553 video_url_list
= [(rf
, url_map
[rf
])]
555 if video_url_list
is None:
556 raise ExtractorError(u
'requested format not available')
558 raise ExtractorError(u
'no conn or url_encoded_fmt_stream_map information found in video info')
561 for format_param
, video_real_url
in video_url_list
:
563 video_extension
= self
._video
_extensions
.get(format_param
, 'flv')
565 video_format
= '{0} - {1}'.format(format_param
if format_param
else video_extension
,
566 self
._video
_dimensions
.get(format_param
, '???'))
570 'url': video_real_url
,
571 'uploader': video_uploader
,
572 'uploader_id': video_uploader_id
,
573 'upload_date': upload_date
,
574 'title': video_title
,
575 'ext': video_extension
,
576 'format': video_format
,
577 'thumbnail': video_thumbnail
,
578 'description': video_description
,
579 'player_url': player_url
,
580 'subtitles': video_subtitles
,
581 'duration': video_duration
585 class YoutubePlaylistIE(InfoExtractor
):
586 """Information Extractor for YouTube playlists."""
593 (?:course|view_play_list|my_playlists|artist|playlist|watch)
594 \? (?:.*?&)*? (?:p|a|list)=
597 ((?:PL|EC|UU)?[0-9A-Za-z-_]{10,})
600 ((?:PL|EC|UU)[0-9A-Za-z-_]{10,})
602 _TEMPLATE_URL
= 'https://gdata.youtube.com/feeds/api/playlists/%s?max-results=%i&start-index=%i&v=2&alt=json&safeSearch=none'
604 IE_NAME
= u
'youtube:playlist'
607 def suitable(cls
, url
):
608 """Receives a URL and returns True if suitable for this IE."""
609 return re
.match(cls
._VALID
_URL
, url
, re
.VERBOSE
) is not None
611 def _real_extract(self
, url
):
612 # Extract playlist id
613 mobj
= re
.match(self
._VALID
_URL
, url
, re
.VERBOSE
)
615 raise ExtractorError(u
'Invalid URL: %s' % url
)
617 # Download playlist videos from API
618 playlist_id
= mobj
.group(1) or mobj
.group(2)
623 url
= self
._TEMPLATE
_URL
% (playlist_id
, self
._MAX
_RESULTS
, self
._MAX
_RESULTS
* (page_num
- 1) + 1)
624 page
= self
._download
_webpage
(url
, playlist_id
, u
'Downloading page #%s' % page_num
)
627 response
= json
.loads(page
)
628 except ValueError as err
:
629 raise ExtractorError(u
'Invalid JSON in API response: ' + compat_str(err
))
631 if 'feed' not in response
:
632 raise ExtractorError(u
'Got a malformed response from YouTube API')
633 playlist_title
= response
['feed']['title']['$t']
634 if 'entry' not in response
['feed']:
635 # Number of videos is a multiple of self._MAX_RESULTS
638 for entry
in response
['feed']['entry']:
639 index
= entry
['yt$position']['$t']
640 if 'media$group' in entry
and 'media$player' in entry
['media$group']:
641 videos
.append((index
, entry
['media$group']['media$player']['url']))
643 if len(response
['feed']['entry']) < self
._MAX
_RESULTS
:
647 videos
= [v
[1] for v
in sorted(videos
)]
649 url_results
= [self
.url_result(url
, 'Youtube') for url
in videos
]
650 return [self
.playlist_result(url_results
, playlist_id
, playlist_title
)]
653 class YoutubeChannelIE(InfoExtractor
):
654 """Information Extractor for YouTube channels."""
656 _VALID_URL
= r
"^(?:https?://)?(?:youtu\.be|(?:\w+\.)?youtube(?:-nocookie)?\.com)/channel/([0-9A-Za-z_-]+)"
657 _TEMPLATE_URL
= 'http://www.youtube.com/channel/%s/videos?sort=da&flow=list&view=0&page=%s&gl=US&hl=en'
658 _MORE_PAGES_INDICATOR
= 'yt-uix-load-more'
659 _MORE_PAGES_URL
= 'http://www.youtube.com/channel_ajax?action_load_more_videos=1&flow=list&paging=%s&view=0&sort=da&channel_id=%s'
660 IE_NAME
= u
'youtube:channel'
662 def extract_videos_from_page(self
, page
):
664 for mobj
in re
.finditer(r
'href="/watch\?v=([0-9A-Za-z_-]+)&?', page
):
665 if mobj
.group(1) not in ids_in_page
:
666 ids_in_page
.append(mobj
.group(1))
669 def _real_extract(self
, url
):
671 mobj
= re
.match(self
._VALID
_URL
, url
)
673 raise ExtractorError(u
'Invalid URL: %s' % url
)
675 # Download channel page
676 channel_id
= mobj
.group(1)
680 url
= self
._TEMPLATE
_URL
% (channel_id
, pagenum
)
681 page
= self
._download
_webpage
(url
, channel_id
,
682 u
'Downloading page #%s' % pagenum
)
684 # Extract video identifiers
685 ids_in_page
= self
.extract_videos_from_page(page
)
686 video_ids
.extend(ids_in_page
)
688 # Download any subsequent channel pages using the json-based channel_ajax query
689 if self
._MORE
_PAGES
_INDICATOR
in page
:
691 pagenum
= pagenum
+ 1
693 url
= self
._MORE
_PAGES
_URL
% (pagenum
, channel_id
)
694 page
= self
._download
_webpage
(url
, channel_id
,
695 u
'Downloading page #%s' % pagenum
)
697 page
= json
.loads(page
)
699 ids_in_page
= self
.extract_videos_from_page(page
['content_html'])
700 video_ids
.extend(ids_in_page
)
702 if self
._MORE
_PAGES
_INDICATOR
not in page
['load_more_widget_html']:
705 self
._downloader
.to_screen(u
'[youtube] Channel %s: Found %i videos' % (channel_id
, len(video_ids
)))
707 urls
= ['http://www.youtube.com/watch?v=%s' % id for id in video_ids
]
708 url_entries
= [self
.url_result(url
, 'Youtube') for url
in urls
]
709 return [self
.playlist_result(url_entries
, channel_id
)]
712 class YoutubeUserIE(InfoExtractor
):
713 """Information Extractor for YouTube users."""
715 _VALID_URL
= r
'(?:(?:(?:https?://)?(?:\w+\.)?youtube\.com/user/)|ytuser:)([A-Za-z0-9_-]+)'
716 _TEMPLATE_URL
= 'http://gdata.youtube.com/feeds/api/users/%s'
717 _GDATA_PAGE_SIZE
= 50
718 _GDATA_URL
= 'http://gdata.youtube.com/feeds/api/users/%s/uploads?max-results=%d&start-index=%d'
719 _VIDEO_INDICATOR
= r
'/watch\?v=(.+?)[\<&]'
720 IE_NAME
= u
'youtube:user'
722 def _real_extract(self
, url
):
724 mobj
= re
.match(self
._VALID
_URL
, url
)
726 raise ExtractorError(u
'Invalid URL: %s' % url
)
728 username
= mobj
.group(1)
730 # Download video ids using YouTube Data API. Result size per
731 # query is limited (currently to 50 videos) so we need to query
732 # page by page until there are no video ids - it means we got
739 start_index
= pagenum
* self
._GDATA
_PAGE
_SIZE
+ 1
741 gdata_url
= self
._GDATA
_URL
% (username
, self
._GDATA
_PAGE
_SIZE
, start_index
)
742 page
= self
._download
_webpage
(gdata_url
, username
,
743 u
'Downloading video ids from %d to %d' % (start_index
, start_index
+ self
._GDATA
_PAGE
_SIZE
))
745 # Extract video identifiers
748 for mobj
in re
.finditer(self
._VIDEO
_INDICATOR
, page
):
749 if mobj
.group(1) not in ids_in_page
:
750 ids_in_page
.append(mobj
.group(1))
752 video_ids
.extend(ids_in_page
)
754 # A little optimization - if current page is not
755 # "full", ie. does not contain PAGE_SIZE video ids then
756 # we can assume that this page is the last one - there
757 # are no more ids on further pages - no need to query
760 if len(ids_in_page
) < self
._GDATA
_PAGE
_SIZE
:
765 urls
= ['http://www.youtube.com/watch?v=%s' % video_id
for video_id
in video_ids
]
766 url_results
= [self
.url_result(url
, 'Youtube') for url
in urls
]
767 return [self
.playlist_result(url_results
, playlist_title
= username
)]
769 class YoutubeSearchIE(SearchInfoExtractor
):
770 """Information Extractor for YouTube search queries."""
771 _API_URL
= 'https://gdata.youtube.com/feeds/api/videos?q=%s&start-index=%i&max-results=50&v=2&alt=jsonc'
773 IE_NAME
= u
'youtube:search'
774 _SEARCH_KEY
= 'ytsearch'
776 def report_download_page(self
, query
, pagenum
):
777 """Report attempt to download search page with given number."""
778 self
._downloader
.to_screen(u
'[youtube] query "%s": Downloading page %s' % (query
, pagenum
))
780 def _get_n_results(self
, query
, n
):
781 """Get a specified number of results for a query"""
787 while (50 * pagenum
) < limit
:
788 self
.report_download_page(query
, pagenum
+1)
789 result_url
= self
._API
_URL
% (compat_urllib_parse
.quote_plus(query
), (50*pagenum
)+1)
790 request
= compat_urllib_request
.Request(result_url
)
792 data
= compat_urllib_request
.urlopen(request
).read().decode('utf-8')
793 except (compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
) as err
:
794 raise ExtractorError(u
'Unable to download API page: %s' % compat_str(err
))
795 api_response
= json
.loads(data
)['data']
797 if not 'items' in api_response
:
798 raise ExtractorError(u
'[youtube] No video results')
800 new_ids
= list(video
['id'] for video
in api_response
['items'])
803 limit
= min(n
, api_response
['totalItems'])
806 if len(video_ids
) > n
:
807 video_ids
= video_ids
[:n
]
808 videos
= [self
.url_result('http://www.youtube.com/watch?v=%s' % id, 'Youtube') for id in video_ids
]
809 return self
.playlist_result(videos
, query
)