You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1855 lines
77 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. # coding: utf-8
  2. import collections
  3. import errno
  4. import io
  5. import itertools
  6. import json
  7. import os.path
  8. import re
  9. import socket
  10. import string
  11. import struct
  12. import traceback
  13. import xml.etree.ElementTree
  14. import zlib
  15. from .common import InfoExtractor, SearchInfoExtractor
  16. from .subtitles import SubtitlesInfoExtractor
  17. from ..utils import (
  18. compat_chr,
  19. compat_http_client,
  20. compat_parse_qs,
  21. compat_urllib_error,
  22. compat_urllib_parse,
  23. compat_urllib_request,
  24. compat_urlparse,
  25. compat_str,
  26. clean_html,
  27. get_cachedir,
  28. get_element_by_id,
  29. ExtractorError,
  30. unescapeHTML,
  31. unified_strdate,
  32. orderedSet,
  33. write_json_file,
  34. )
  35. class YoutubeBaseInfoExtractor(InfoExtractor):
  36. """Provide base functions for Youtube extractors"""
  37. _LOGIN_URL = 'https://accounts.google.com/ServiceLogin'
  38. _LANG_URL = r'https://www.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
  39. _AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
  40. _NETRC_MACHINE = 'youtube'
  41. # If True it will raise an error if no login info is provided
  42. _LOGIN_REQUIRED = False
  43. def report_lang(self):
  44. """Report attempt to set language."""
  45. self.to_screen(u'Setting language')
  46. def _set_language(self):
  47. request = compat_urllib_request.Request(self._LANG_URL)
  48. try:
  49. self.report_lang()
  50. compat_urllib_request.urlopen(request).read()
  51. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  52. self._downloader.report_warning(u'unable to set language: %s' % compat_str(err))
  53. return False
  54. return True
  55. def _login(self):
  56. (username, password) = self._get_login_info()
  57. # No authentication to be performed
  58. if username is None:
  59. if self._LOGIN_REQUIRED:
  60. raise ExtractorError(u'No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  61. return False
  62. request = compat_urllib_request.Request(self._LOGIN_URL)
  63. try:
  64. login_page = compat_urllib_request.urlopen(request).read().decode('utf-8')
  65. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  66. self._downloader.report_warning(u'unable to fetch login page: %s' % compat_str(err))
  67. return False
  68. galx = None
  69. dsh = None
  70. match = re.search(re.compile(r'<input.+?name="GALX".+?value="(.+?)"', re.DOTALL), login_page)
  71. if match:
  72. galx = match.group(1)
  73. match = re.search(re.compile(r'<input.+?name="dsh".+?value="(.+?)"', re.DOTALL), login_page)
  74. if match:
  75. dsh = match.group(1)
  76. # Log in
  77. login_form_strs = {
  78. u'continue': u'https://www.youtube.com/signin?action_handle_signin=true&feature=sign_in_button&hl=en_US&nomobiletemp=1',
  79. u'Email': username,
  80. u'GALX': galx,
  81. u'Passwd': password,
  82. u'PersistentCookie': u'yes',
  83. u'_utf8': u'',
  84. u'bgresponse': u'js_disabled',
  85. u'checkConnection': u'',
  86. u'checkedDomains': u'youtube',
  87. u'dnConn': u'',
  88. u'dsh': dsh,
  89. u'pstMsg': u'0',
  90. u'rmShown': u'1',
  91. u'secTok': u'',
  92. u'signIn': u'Sign in',
  93. u'timeStmp': u'',
  94. u'service': u'youtube',
  95. u'uilel': u'3',
  96. u'hl': u'en_US',
  97. }
  98. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  99. # chokes on unicode
  100. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k,v in login_form_strs.items())
  101. login_data = compat_urllib_parse.urlencode(login_form).encode('ascii')
  102. request = compat_urllib_request.Request(self._LOGIN_URL, login_data)
  103. try:
  104. self.report_login()
  105. login_results = compat_urllib_request.urlopen(request).read().decode('utf-8')
  106. if re.search(r'(?i)<form[^>]* id="gaia_loginform"', login_results) is not None:
  107. self._downloader.report_warning(u'unable to log in: bad username or password')
  108. return False
  109. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  110. self._downloader.report_warning(u'unable to log in: %s' % compat_str(err))
  111. return False
  112. return True
  113. def _confirm_age(self):
  114. age_form = {
  115. 'next_url': '/',
  116. 'action_confirm': 'Confirm',
  117. }
  118. request = compat_urllib_request.Request(self._AGE_URL, compat_urllib_parse.urlencode(age_form))
  119. try:
  120. self.report_age_confirmation()
  121. compat_urllib_request.urlopen(request).read().decode('utf-8')
  122. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  123. raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))
  124. return True
  125. def _real_initialize(self):
  126. if self._downloader is None:
  127. return
  128. if not self._set_language():
  129. return
  130. if not self._login():
  131. return
  132. self._confirm_age()
  133. class YoutubeIE(YoutubeBaseInfoExtractor, SubtitlesInfoExtractor):
  134. IE_DESC = u'YouTube.com'
  135. _VALID_URL = r"""^
  136. (
  137. (?:https?://)? # http(s):// (optional)
  138. (?:(?:(?:(?:\w+\.)?youtube(?:-nocookie)?\.com/|
  139. tube\.majestyc\.net/|
  140. youtube\.googleapis\.com/) # the various hostnames, with wildcard subdomains
  141. (?:.*?\#/)? # handle anchor (#/) redirect urls
  142. (?: # the various things that can precede the ID:
  143. (?:(?:v|embed|e)/) # v/ or embed/ or e/
  144. |(?: # or the v= param in all its forms
  145. (?:(?:watch|movie)(?:_popup)?(?:\.php)?)? # preceding watch(_popup|.php) or nothing (like /?v=xxxx)
  146. (?:\?|\#!?) # the params delimiter ? or # or #!
  147. (?:.*?&)? # any other preceding param (like /?s=tuff&v=xxxx)
  148. v=
  149. )
  150. ))
  151. |youtu\.be/ # just youtu.be/xxxx
  152. )
  153. )? # all until now is optional -> you can pass the naked ID
  154. ([0-9A-Za-z_-]{11}) # here is it! the YouTube video ID
  155. (?(1).+)? # if we found the ID, everything can follow
  156. $"""
  157. _NEXT_URL_RE = r'[\?&]next_url=([^&]+)'
  158. # Listed in order of quality
  159. _available_formats = ['38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '36', '17', '13',
  160. # Apple HTTP Live Streaming
  161. '96', '95', '94', '93', '92', '132', '151',
  162. # 3D
  163. '85', '84', '102', '83', '101', '82', '100',
  164. # Dash video
  165. '138', '137', '248', '136', '247', '135', '246',
  166. '245', '244', '134', '243', '133', '242', '160',
  167. # Dash audio
  168. '141', '172', '140', '171', '139',
  169. ]
  170. _available_formats_prefer_free = ['38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '36', '17', '13',
  171. # Apple HTTP Live Streaming
  172. '96', '95', '94', '93', '92', '132', '151',
  173. # 3D
  174. '85', '102', '84', '101', '83', '100', '82',
  175. # Dash video
  176. '138', '248', '137', '247', '136', '246', '245',
  177. '244', '135', '243', '134', '242', '133', '160',
  178. # Dash audio
  179. '172', '141', '171', '140', '139',
  180. ]
  181. _video_formats_map = {
  182. 'flv': ['35', '34', '6', '5'],
  183. '3gp': ['36', '17', '13'],
  184. 'mp4': ['38', '37', '22', '18'],
  185. 'webm': ['46', '45', '44', '43'],
  186. }
  187. _video_extensions = {
  188. '13': '3gp',
  189. '17': '3gp',
  190. '18': 'mp4',
  191. '22': 'mp4',
  192. '36': '3gp',
  193. '37': 'mp4',
  194. '38': 'mp4',
  195. '43': 'webm',
  196. '44': 'webm',
  197. '45': 'webm',
  198. '46': 'webm',
  199. # 3d videos
  200. '82': 'mp4',
  201. '83': 'mp4',
  202. '84': 'mp4',
  203. '85': 'mp4',
  204. '100': 'webm',
  205. '101': 'webm',
  206. '102': 'webm',
  207. # Apple HTTP Live Streaming
  208. '92': 'mp4',
  209. '93': 'mp4',
  210. '94': 'mp4',
  211. '95': 'mp4',
  212. '96': 'mp4',
  213. '132': 'mp4',
  214. '151': 'mp4',
  215. # Dash mp4
  216. '133': 'mp4',
  217. '134': 'mp4',
  218. '135': 'mp4',
  219. '136': 'mp4',
  220. '137': 'mp4',
  221. '138': 'mp4',
  222. '139': 'mp4',
  223. '140': 'mp4',
  224. '141': 'mp4',
  225. '160': 'mp4',
  226. # Dash webm
  227. '171': 'webm',
  228. '172': 'webm',
  229. '242': 'webm',
  230. '243': 'webm',
  231. '244': 'webm',
  232. '245': 'webm',
  233. '246': 'webm',
  234. '247': 'webm',
  235. '248': 'webm',
  236. }
  237. _video_dimensions = {
  238. '5': '240x400',
  239. '6': '???',
  240. '13': '???',
  241. '17': '144x176',
  242. '18': '360x640',
  243. '22': '720x1280',
  244. '34': '360x640',
  245. '35': '480x854',
  246. '36': '240x320',
  247. '37': '1080x1920',
  248. '38': '3072x4096',
  249. '43': '360x640',
  250. '44': '480x854',
  251. '45': '720x1280',
  252. '46': '1080x1920',
  253. '82': '360p',
  254. '83': '480p',
  255. '84': '720p',
  256. '85': '1080p',
  257. '92': '240p',
  258. '93': '360p',
  259. '94': '480p',
  260. '95': '720p',
  261. '96': '1080p',
  262. '100': '360p',
  263. '101': '480p',
  264. '102': '720p',
  265. '132': '240p',
  266. '151': '72p',
  267. '133': '240p',
  268. '134': '360p',
  269. '135': '480p',
  270. '136': '720p',
  271. '137': '1080p',
  272. '138': '>1080p',
  273. '139': '48k',
  274. '140': '128k',
  275. '141': '256k',
  276. '160': '192p',
  277. '171': '128k',
  278. '172': '256k',
  279. '242': '240p',
  280. '243': '360p',
  281. '244': '480p',
  282. '245': '480p',
  283. '246': '480p',
  284. '247': '720p',
  285. '248': '1080p',
  286. }
  287. _special_itags = {
  288. '82': '3D',
  289. '83': '3D',
  290. '84': '3D',
  291. '85': '3D',
  292. '100': '3D',
  293. '101': '3D',
  294. '102': '3D',
  295. '133': 'DASH Video',
  296. '134': 'DASH Video',
  297. '135': 'DASH Video',
  298. '136': 'DASH Video',
  299. '137': 'DASH Video',
  300. '138': 'DASH Video',
  301. '139': 'DASH Audio',
  302. '140': 'DASH Audio',
  303. '141': 'DASH Audio',
  304. '160': 'DASH Video',
  305. '171': 'DASH Audio',
  306. '172': 'DASH Audio',
  307. '242': 'DASH Video',
  308. '243': 'DASH Video',
  309. '244': 'DASH Video',
  310. '245': 'DASH Video',
  311. '246': 'DASH Video',
  312. '247': 'DASH Video',
  313. '248': 'DASH Video',
  314. }
  315. IE_NAME = u'youtube'
  316. _TESTS = [
  317. {
  318. u"url": u"http://www.youtube.com/watch?v=BaW_jenozKc",
  319. u"file": u"BaW_jenozKc.mp4",
  320. u"info_dict": {
  321. u"title": u"youtube-dl test video \"'/\\ä↭𝕐",
  322. u"uploader": u"Philipp Hagemeister",
  323. u"uploader_id": u"phihag",
  324. u"upload_date": u"20121002",
  325. u"description": u"test chars: \"'/\\ä↭𝕐\n\nThis is a test video for youtube-dl.\n\nFor more information, contact phihag@phihag.de ."
  326. }
  327. },
  328. {
  329. u"url": u"http://www.youtube.com/watch?v=1ltcDfZMA3U",
  330. u"file": u"1ltcDfZMA3U.flv",
  331. u"note": u"Test VEVO video (#897)",
  332. u"info_dict": {
  333. u"upload_date": u"20070518",
  334. u"title": u"Maps - It Will Find You",
  335. u"description": u"Music video by Maps performing It Will Find You.",
  336. u"uploader": u"MuteUSA",
  337. u"uploader_id": u"MuteUSA"
  338. }
  339. },
  340. {
  341. u"url": u"http://www.youtube.com/watch?v=UxxajLWwzqY",
  342. u"file": u"UxxajLWwzqY.mp4",
  343. u"note": u"Test generic use_cipher_signature video (#897)",
  344. u"info_dict": {
  345. u"upload_date": u"20120506",
  346. u"title": u"Icona Pop - I Love It (feat. Charli XCX) [OFFICIAL VIDEO]",
  347. u"description": u"md5:5b292926389560516e384ac437c0ec07",
  348. u"uploader": u"Icona Pop",
  349. u"uploader_id": u"IconaPop"
  350. }
  351. },
  352. {
  353. u"url": u"https://www.youtube.com/watch?v=07FYdnEawAQ",
  354. u"file": u"07FYdnEawAQ.mp4",
  355. u"note": u"Test VEVO video with age protection (#956)",
  356. u"info_dict": {
  357. u"upload_date": u"20130703",
  358. u"title": u"Justin Timberlake - Tunnel Vision (Explicit)",
  359. u"description": u"md5:64249768eec3bc4276236606ea996373",
  360. u"uploader": u"justintimberlakeVEVO",
  361. u"uploader_id": u"justintimberlakeVEVO"
  362. }
  363. },
  364. ]
  365. @classmethod
  366. def suitable(cls, url):
  367. """Receives a URL and returns True if suitable for this IE."""
  368. if YoutubePlaylistIE.suitable(url): return False
  369. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  370. def __init__(self, *args, **kwargs):
  371. super(YoutubeIE, self).__init__(*args, **kwargs)
  372. self._player_cache = {}
  373. def report_video_webpage_download(self, video_id):
  374. """Report attempt to download video webpage."""
  375. self.to_screen(u'%s: Downloading video webpage' % video_id)
  376. def report_video_info_webpage_download(self, video_id):
  377. """Report attempt to download video info webpage."""
  378. self.to_screen(u'%s: Downloading video info webpage' % video_id)
  379. def report_information_extraction(self, video_id):
  380. """Report attempt to extract video information."""
  381. self.to_screen(u'%s: Extracting video information' % video_id)
  382. def report_unavailable_format(self, video_id, format):
  383. """Report extracted video URL."""
  384. self.to_screen(u'%s: Format %s not available' % (video_id, format))
  385. def report_rtmp_download(self):
  386. """Indicate the download will use the RTMP protocol."""
  387. self.to_screen(u'RTMP download detected')
  388. def _extract_signature_function(self, video_id, player_url, slen):
  389. id_m = re.match(r'.*-(?P<id>[a-zA-Z0-9_-]+)\.(?P<ext>[a-z]+)$',
  390. player_url)
  391. player_type = id_m.group('ext')
  392. player_id = id_m.group('id')
  393. # Read from filesystem cache
  394. func_id = '%s_%s_%d' % (player_type, player_id, slen)
  395. assert os.path.basename(func_id) == func_id
  396. cache_dir = get_cachedir(self._downloader.params)
  397. cache_enabled = cache_dir is not None
  398. if cache_enabled:
  399. cache_fn = os.path.join(os.path.expanduser(cache_dir),
  400. u'youtube-sigfuncs',
  401. func_id + '.json')
  402. try:
  403. with io.open(cache_fn, 'r', encoding='utf-8') as cachef:
  404. cache_spec = json.load(cachef)
  405. return lambda s: u''.join(s[i] for i in cache_spec)
  406. except IOError:
  407. pass # No cache available
  408. if player_type == 'js':
  409. code = self._download_webpage(
  410. player_url, video_id,
  411. note=u'Downloading %s player %s' % (player_type, player_id),
  412. errnote=u'Download of %s failed' % player_url)
  413. res = self._parse_sig_js(code)
  414. elif player_type == 'swf':
  415. urlh = self._request_webpage(
  416. player_url, video_id,
  417. note=u'Downloading %s player %s' % (player_type, player_id),
  418. errnote=u'Download of %s failed' % player_url)
  419. code = urlh.read()
  420. res = self._parse_sig_swf(code)
  421. else:
  422. assert False, 'Invalid player type %r' % player_type
  423. if cache_enabled:
  424. try:
  425. test_string = u''.join(map(compat_chr, range(slen)))
  426. cache_res = res(test_string)
  427. cache_spec = [ord(c) for c in cache_res]
  428. try:
  429. os.makedirs(os.path.dirname(cache_fn))
  430. except OSError as ose:
  431. if ose.errno != errno.EEXIST:
  432. raise
  433. write_json_file(cache_spec, cache_fn)
  434. except Exception:
  435. tb = traceback.format_exc()
  436. self._downloader.report_warning(
  437. u'Writing cache to %r failed: %s' % (cache_fn, tb))
  438. return res
  439. def _print_sig_code(self, func, slen):
  440. def gen_sig_code(idxs):
  441. def _genslice(start, end, step):
  442. starts = u'' if start == 0 else str(start)
  443. ends = (u':%d' % (end+step)) if end + step >= 0 else u':'
  444. steps = u'' if step == 1 else (u':%d' % step)
  445. return u's[%s%s%s]' % (starts, ends, steps)
  446. step = None
  447. start = '(Never used)' # Quelch pyflakes warnings - start will be
  448. # set as soon as step is set
  449. for i, prev in zip(idxs[1:], idxs[:-1]):
  450. if step is not None:
  451. if i - prev == step:
  452. continue
  453. yield _genslice(start, prev, step)
  454. step = None
  455. continue
  456. if i - prev in [-1, 1]:
  457. step = i - prev
  458. start = prev
  459. continue
  460. else:
  461. yield u's[%d]' % prev
  462. if step is None:
  463. yield u's[%d]' % i
  464. else:
  465. yield _genslice(start, i, step)
  466. test_string = u''.join(map(compat_chr, range(slen)))
  467. cache_res = func(test_string)
  468. cache_spec = [ord(c) for c in cache_res]
  469. expr_code = u' + '.join(gen_sig_code(cache_spec))
  470. code = u'if len(s) == %d:\n return %s\n' % (slen, expr_code)
  471. self.to_screen(u'Extracted signature function:\n' + code)
  472. def _parse_sig_js(self, jscode):
  473. funcname = self._search_regex(
  474. r'signature=([a-zA-Z]+)', jscode,
  475. u'Initial JS player signature function name')
  476. functions = {}
  477. def argidx(varname):
  478. return string.lowercase.index(varname)
  479. def interpret_statement(stmt, local_vars, allow_recursion=20):
  480. if allow_recursion < 0:
  481. raise ExtractorError(u'Recursion limit reached')
  482. if stmt.startswith(u'var '):
  483. stmt = stmt[len(u'var '):]
  484. ass_m = re.match(r'^(?P<out>[a-z]+)(?:\[(?P<index>[^\]]+)\])?' +
  485. r'=(?P<expr>.*)$', stmt)
  486. if ass_m:
  487. if ass_m.groupdict().get('index'):
  488. def assign(val):
  489. lvar = local_vars[ass_m.group('out')]
  490. idx = interpret_expression(ass_m.group('index'),
  491. local_vars, allow_recursion)
  492. assert isinstance(idx, int)
  493. lvar[idx] = val
  494. return val
  495. expr = ass_m.group('expr')
  496. else:
  497. def assign(val):
  498. local_vars[ass_m.group('out')] = val
  499. return val
  500. expr = ass_m.group('expr')
  501. elif stmt.startswith(u'return '):
  502. assign = lambda v: v
  503. expr = stmt[len(u'return '):]
  504. else:
  505. raise ExtractorError(
  506. u'Cannot determine left side of statement in %r' % stmt)
  507. v = interpret_expression(expr, local_vars, allow_recursion)
  508. return assign(v)
  509. def interpret_expression(expr, local_vars, allow_recursion):
  510. if expr.isdigit():
  511. return int(expr)
  512. if expr.isalpha():
  513. return local_vars[expr]
  514. m = re.match(r'^(?P<in>[a-z]+)\.(?P<member>.*)$', expr)
  515. if m:
  516. member = m.group('member')
  517. val = local_vars[m.group('in')]
  518. if member == 'split("")':
  519. return list(val)
  520. if member == 'join("")':
  521. return u''.join(val)
  522. if member == 'length':
  523. return len(val)
  524. if member == 'reverse()':
  525. return val[::-1]
  526. slice_m = re.match(r'slice\((?P<idx>.*)\)', member)
  527. if slice_m:
  528. idx = interpret_expression(
  529. slice_m.group('idx'), local_vars, allow_recursion-1)
  530. return val[idx:]
  531. m = re.match(
  532. r'^(?P<in>[a-z]+)\[(?P<idx>.+)\]$', expr)
  533. if m:
  534. val = local_vars[m.group('in')]
  535. idx = interpret_expression(m.group('idx'), local_vars,
  536. allow_recursion-1)
  537. return val[idx]
  538. m = re.match(r'^(?P<a>.+?)(?P<op>[%])(?P<b>.+?)$', expr)
  539. if m:
  540. a = interpret_expression(m.group('a'),
  541. local_vars, allow_recursion)
  542. b = interpret_expression(m.group('b'),
  543. local_vars, allow_recursion)
  544. return a % b
  545. m = re.match(
  546. r'^(?P<func>[a-zA-Z]+)\((?P<args>[a-z0-9,]+)\)$', expr)
  547. if m:
  548. fname = m.group('func')
  549. if fname not in functions:
  550. functions[fname] = extract_function(fname)
  551. argvals = [int(v) if v.isdigit() else local_vars[v]
  552. for v in m.group('args').split(',')]
  553. return functions[fname](argvals)
  554. raise ExtractorError(u'Unsupported JS expression %r' % expr)
  555. def extract_function(funcname):
  556. func_m = re.search(
  557. r'function ' + re.escape(funcname) +
  558. r'\((?P<args>[a-z,]+)\){(?P<code>[^}]+)}',
  559. jscode)
  560. argnames = func_m.group('args').split(',')
  561. def resf(args):
  562. local_vars = dict(zip(argnames, args))
  563. for stmt in func_m.group('code').split(';'):
  564. res = interpret_statement(stmt, local_vars)
  565. return res
  566. return resf
  567. initial_function = extract_function(funcname)
  568. return lambda s: initial_function([s])
  569. def _parse_sig_swf(self, file_contents):
  570. if file_contents[1:3] != b'WS':
  571. raise ExtractorError(
  572. u'Not an SWF file; header is %r' % file_contents[:3])
  573. if file_contents[:1] == b'C':
  574. content = zlib.decompress(file_contents[8:])
  575. else:
  576. raise NotImplementedError(u'Unsupported compression format %r' %
  577. file_contents[:1])
  578. def extract_tags(content):
  579. pos = 0
  580. while pos < len(content):
  581. header16 = struct.unpack('<H', content[pos:pos+2])[0]
  582. pos += 2
  583. tag_code = header16 >> 6
  584. tag_len = header16 & 0x3f
  585. if tag_len == 0x3f:
  586. tag_len = struct.unpack('<I', content[pos:pos+4])[0]
  587. pos += 4
  588. assert pos+tag_len <= len(content)
  589. yield (tag_code, content[pos:pos+tag_len])
  590. pos += tag_len
  591. code_tag = next(tag
  592. for tag_code, tag in extract_tags(content)
  593. if tag_code == 82)
  594. p = code_tag.index(b'\0', 4) + 1
  595. code_reader = io.BytesIO(code_tag[p:])
  596. # Parse ABC (AVM2 ByteCode)
  597. def read_int(reader=None):
  598. if reader is None:
  599. reader = code_reader
  600. res = 0
  601. shift = 0
  602. for _ in range(5):
  603. buf = reader.read(1)
  604. assert len(buf) == 1
  605. b = struct.unpack('<B', buf)[0]
  606. res = res | ((b & 0x7f) << shift)
  607. if b & 0x80 == 0:
  608. break
  609. shift += 7
  610. return res
  611. def u30(reader=None):
  612. res = read_int(reader)
  613. assert res & 0xf0000000 == 0
  614. return res
  615. u32 = read_int
  616. def s32(reader=None):
  617. v = read_int(reader)
  618. if v & 0x80000000 != 0:
  619. v = - ((v ^ 0xffffffff) + 1)
  620. return v
  621. def read_string(reader=None):
  622. if reader is None:
  623. reader = code_reader
  624. slen = u30(reader)
  625. resb = reader.read(slen)
  626. assert len(resb) == slen
  627. return resb.decode('utf-8')
  628. def read_bytes(count, reader=None):
  629. if reader is None:
  630. reader = code_reader
  631. resb = reader.read(count)
  632. assert len(resb) == count
  633. return resb
  634. def read_byte(reader=None):
  635. resb = read_bytes(1, reader=reader)
  636. res = struct.unpack('<B', resb)[0]
  637. return res
  638. # minor_version + major_version
  639. read_bytes(2 + 2)
  640. # Constant pool
  641. int_count = u30()
  642. for _c in range(1, int_count):
  643. s32()
  644. uint_count = u30()
  645. for _c in range(1, uint_count):
  646. u32()
  647. double_count = u30()
  648. read_bytes((double_count-1) * 8)
  649. string_count = u30()
  650. constant_strings = [u'']
  651. for _c in range(1, string_count):
  652. s = read_string()
  653. constant_strings.append(s)
  654. namespace_count = u30()
  655. for _c in range(1, namespace_count):
  656. read_bytes(1) # kind
  657. u30() # name
  658. ns_set_count = u30()
  659. for _c in range(1, ns_set_count):
  660. count = u30()
  661. for _c2 in range(count):
  662. u30()
  663. multiname_count = u30()
  664. MULTINAME_SIZES = {
  665. 0x07: 2, # QName
  666. 0x0d: 2, # QNameA
  667. 0x0f: 1, # RTQName
  668. 0x10: 1, # RTQNameA
  669. 0x11: 0, # RTQNameL
  670. 0x12: 0, # RTQNameLA
  671. 0x09: 2, # Multiname
  672. 0x0e: 2, # MultinameA
  673. 0x1b: 1, # MultinameL
  674. 0x1c: 1, # MultinameLA
  675. }
  676. multinames = [u'']
  677. for _c in range(1, multiname_count):
  678. kind = u30()
  679. assert kind in MULTINAME_SIZES, u'Invalid multiname kind %r' % kind
  680. if kind == 0x07:
  681. u30() # namespace_idx
  682. name_idx = u30()
  683. multinames.append(constant_strings[name_idx])
  684. else:
  685. multinames.append('[MULTINAME kind: %d]' % kind)
  686. for _c2 in range(MULTINAME_SIZES[kind]):
  687. u30()
  688. # Methods
  689. method_count = u30()
  690. MethodInfo = collections.namedtuple(
  691. 'MethodInfo',
  692. ['NEED_ARGUMENTS', 'NEED_REST'])
  693. method_infos = []
  694. for method_id in range(method_count):
  695. param_count = u30()
  696. u30() # return type
  697. for _ in range(param_count):
  698. u30() # param type
  699. u30() # name index (always 0 for youtube)
  700. flags = read_byte()
  701. if flags & 0x08 != 0:
  702. # Options present
  703. option_count = u30()
  704. for c in range(option_count):
  705. u30() # val
  706. read_bytes(1) # kind
  707. if flags & 0x80 != 0:
  708. # Param names present
  709. for _ in range(param_count):
  710. u30() # param name
  711. mi = MethodInfo(flags & 0x01 != 0, flags & 0x04 != 0)
  712. method_infos.append(mi)
  713. # Metadata
  714. metadata_count = u30()
  715. for _c in range(metadata_count):
  716. u30() # name
  717. item_count = u30()
  718. for _c2 in range(item_count):
  719. u30() # key
  720. u30() # value
  721. def parse_traits_info():
  722. trait_name_idx = u30()
  723. kind_full = read_byte()
  724. kind = kind_full & 0x0f
  725. attrs = kind_full >> 4
  726. methods = {}
  727. if kind in [0x00, 0x06]: # Slot or Const
  728. u30() # Slot id
  729. u30() # type_name_idx
  730. vindex = u30()
  731. if vindex != 0:
  732. read_byte() # vkind
  733. elif kind in [0x01, 0x02, 0x03]: # Method / Getter / Setter
  734. u30() # disp_id
  735. method_idx = u30()
  736. methods[multinames[trait_name_idx]] = method_idx
  737. elif kind == 0x04: # Class
  738. u30() # slot_id
  739. u30() # classi
  740. elif kind == 0x05: # Function
  741. u30() # slot_id
  742. function_idx = u30()
  743. methods[function_idx] = multinames[trait_name_idx]
  744. else:
  745. raise ExtractorError(u'Unsupported trait kind %d' % kind)
  746. if attrs & 0x4 != 0: # Metadata present
  747. metadata_count = u30()
  748. for _c3 in range(metadata_count):
  749. u30() # metadata index
  750. return methods
  751. # Classes
  752. TARGET_CLASSNAME = u'SignatureDecipher'
  753. searched_idx = multinames.index(TARGET_CLASSNAME)
  754. searched_class_id = None
  755. class_count = u30()
  756. for class_id in range(class_count):
  757. name_idx = u30()
  758. if name_idx == searched_idx:
  759. # We found the class we're looking for!
  760. searched_class_id = class_id
  761. u30() # super_name idx
  762. flags = read_byte()
  763. if flags & 0x08 != 0: # Protected namespace is present
  764. u30() # protected_ns_idx
  765. intrf_count = u30()
  766. for _c2 in range(intrf_count):
  767. u30()
  768. u30() # iinit
  769. trait_count = u30()
  770. for _c2 in range(trait_count):
  771. parse_traits_info()
  772. if searched_class_id is None:
  773. raise ExtractorError(u'Target class %r not found' %
  774. TARGET_CLASSNAME)
  775. method_names = {}
  776. method_idxs = {}
  777. for class_id in range(class_count):
  778. u30() # cinit
  779. trait_count = u30()
  780. for _c2 in range(trait_count):
  781. trait_methods = parse_traits_info()
  782. if class_id == searched_class_id:
  783. method_names.update(trait_methods.items())
  784. method_idxs.update(dict(
  785. (idx, name)
  786. for name, idx in trait_methods.items()))
  787. # Scripts
  788. script_count = u30()
  789. for _c in range(script_count):
  790. u30() # init
  791. trait_count = u30()
  792. for _c2 in range(trait_count):
  793. parse_traits_info()
  794. # Method bodies
  795. method_body_count = u30()
  796. Method = collections.namedtuple('Method', ['code', 'local_count'])
  797. methods = {}
  798. for _c in range(method_body_count):
  799. method_idx = u30()
  800. u30() # max_stack
  801. local_count = u30()
  802. u30() # init_scope_depth
  803. u30() # max_scope_depth
  804. code_length = u30()
  805. code = read_bytes(code_length)
  806. if method_idx in method_idxs:
  807. m = Method(code, local_count)
  808. methods[method_idxs[method_idx]] = m
  809. exception_count = u30()
  810. for _c2 in range(exception_count):
  811. u30() # from
  812. u30() # to
  813. u30() # target
  814. u30() # exc_type
  815. u30() # var_name
  816. trait_count = u30()
  817. for _c2 in range(trait_count):
  818. parse_traits_info()
  819. assert p + code_reader.tell() == len(code_tag)
  820. assert len(methods) == len(method_idxs)
  821. method_pyfunctions = {}
  822. def extract_function(func_name):
  823. if func_name in method_pyfunctions:
  824. return method_pyfunctions[func_name]
  825. if func_name not in methods:
  826. raise ExtractorError(u'Cannot find function %r' % func_name)
  827. m = methods[func_name]
  828. def resfunc(args):
  829. registers = ['(this)'] + list(args) + [None] * m.local_count
  830. stack = []
  831. coder = io.BytesIO(m.code)
  832. while True:
  833. opcode = struct.unpack('!B', coder.read(1))[0]
  834. if opcode == 36: # pushbyte
  835. v = struct.unpack('!B', coder.read(1))[0]
  836. stack.append(v)
  837. elif opcode == 44: # pushstring
  838. idx = u30(coder)
  839. stack.append(constant_strings[idx])
  840. elif opcode == 48: # pushscope
  841. # We don't implement the scope register, so we'll just
  842. # ignore the popped value
  843. stack.pop()
  844. elif opcode == 70: # callproperty
  845. index = u30(coder)
  846. mname = multinames[index]
  847. arg_count = u30(coder)
  848. args = list(reversed(
  849. [stack.pop() for _ in range(arg_count)]))
  850. obj = stack.pop()
  851. if mname == u'split':
  852. assert len(args) == 1
  853. assert isinstance(args[0], compat_str)
  854. assert isinstance(obj, compat_str)
  855. if args[0] == u'':
  856. res = list(obj)
  857. else:
  858. res = obj.split(args[0])
  859. stack.append(res)
  860. elif mname == u'slice':
  861. assert len(args) == 1
  862. assert isinstance(args[0], int)
  863. assert isinstance(obj, list)
  864. res = obj[args[0]:]
  865. stack.append(res)
  866. elif mname == u'join':
  867. assert len(args) == 1
  868. assert isinstance(args[0], compat_str)
  869. assert isinstance(obj, list)
  870. res = args[0].join(obj)
  871. stack.append(res)
  872. elif mname in method_pyfunctions:
  873. stack.append(method_pyfunctions[mname](args))
  874. else:
  875. raise NotImplementedError(
  876. u'Unsupported property %r on %r'
  877. % (mname, obj))
  878. elif opcode == 72: # returnvalue
  879. res = stack.pop()
  880. return res
  881. elif opcode == 79: # callpropvoid
  882. index = u30(coder)
  883. mname = multinames[index]
  884. arg_count = u30(coder)
  885. args = list(reversed(
  886. [stack.pop() for _ in range(arg_count)]))
  887. obj = stack.pop()
  888. if mname == u'reverse':
  889. assert isinstance(obj, list)
  890. obj.reverse()
  891. else:
  892. raise NotImplementedError(
  893. u'Unsupported (void) property %r on %r'
  894. % (mname, obj))
  895. elif opcode == 93: # findpropstrict
  896. index = u30(coder)
  897. mname = multinames[index]
  898. res = extract_function(mname)
  899. stack.append(res)
  900. elif opcode == 97: # setproperty
  901. index = u30(coder)
  902. value = stack.pop()
  903. idx = stack.pop()
  904. obj = stack.pop()
  905. assert isinstance(obj, list)
  906. assert isinstance(idx, int)
  907. obj[idx] = value
  908. elif opcode == 98: # getlocal
  909. index = u30(coder)
  910. stack.append(registers[index])
  911. elif opcode == 99: # setlocal
  912. index = u30(coder)
  913. value = stack.pop()
  914. registers[index] = value
  915. elif opcode == 102: # getproperty
  916. index = u30(coder)
  917. pname = multinames[index]
  918. if pname == u'length':
  919. obj = stack.pop()
  920. assert isinstance(obj, list)
  921. stack.append(len(obj))
  922. else: # Assume attribute access
  923. idx = stack.pop()
  924. assert isinstance(idx, int)
  925. obj = stack.pop()
  926. assert isinstance(obj, list)
  927. stack.append(obj[idx])
  928. elif opcode == 128: # coerce
  929. u30(coder)
  930. elif opcode == 133: # coerce_s
  931. assert isinstance(stack[-1], (type(None), compat_str))
  932. elif opcode == 164: # modulo
  933. value2 = stack.pop()
  934. value1 = stack.pop()
  935. res = value1 % value2
  936. stack.append(res)
  937. elif opcode == 208: # getlocal_0
  938. stack.append(registers[0])
  939. elif opcode == 209: # getlocal_1
  940. stack.append(registers[1])
  941. elif opcode == 210: # getlocal_2
  942. stack.append(registers[2])
  943. elif opcode == 211: # getlocal_3
  944. stack.append(registers[3])
  945. elif opcode == 214: # setlocal_2
  946. registers[2] = stack.pop()
  947. elif opcode == 215: # setlocal_3
  948. registers[3] = stack.pop()
  949. else:
  950. raise NotImplementedError(
  951. u'Unsupported opcode %d' % opcode)
  952. method_pyfunctions[func_name] = resfunc
  953. return resfunc
  954. initial_function = extract_function(u'decipher')
  955. return lambda s: initial_function([s])
  956. def _decrypt_signature(self, s, video_id, player_url, age_gate=False):
  957. """Turn the encrypted s field into a working signature"""
  958. if player_url is not None:
  959. try:
  960. player_id = (player_url, len(s))
  961. if player_id not in self._player_cache:
  962. func = self._extract_signature_function(
  963. video_id, player_url, len(s)
  964. )
  965. self._player_cache[player_id] = func
  966. func = self._player_cache[player_id]
  967. if self._downloader.params.get('youtube_print_sig_code'):
  968. self._print_sig_code(func, len(s))
  969. return func(s)
  970. except Exception:
  971. tb = traceback.format_exc()
  972. self._downloader.report_warning(
  973. u'Automatic signature extraction failed: ' + tb)
  974. self._downloader.report_warning(
  975. u'Warning: Falling back to static signature algorithm')
  976. return self._static_decrypt_signature(
  977. s, video_id, player_url, age_gate)
  978. def _static_decrypt_signature(self, s, video_id, player_url, age_gate):
  979. if age_gate:
  980. # The videos with age protection use another player, so the
  981. # algorithms can be different.
  982. if len(s) == 86:
  983. return s[2:63] + s[82] + s[64:82] + s[63]
  984. if len(s) == 93:
  985. return s[86:29:-1] + s[88] + s[28:5:-1]
  986. elif len(s) == 92:
  987. return s[25] + s[3:25] + s[0] + s[26:42] + s[79] + s[43:79] + s[91] + s[80:83]
  988. elif len(s) == 91:
  989. return s[84:27:-1] + s[86] + s[26:5:-1]
  990. elif len(s) == 90:
  991. return s[25] + s[3:25] + s[2] + s[26:40] + s[77] + s[41:77] + s[89] + s[78:81]
  992. elif len(s) == 89:
  993. return s[84:78:-1] + s[87] + s[77:60:-1] + s[0] + s[59:3:-1]
  994. elif len(s) == 88:
  995. return s[7:28] + s[87] + s[29:45] + s[55] + s[46:55] + s[2] + s[56:87] + s[28]
  996. elif len(s) == 87:
  997. return s[6:27] + s[4] + s[28:39] + s[27] + s[40:59] + s[2] + s[60:]
  998. elif len(s) == 86:
  999. return s[80:72:-1] + s[16] + s[71:39:-1] + s[72] + s[38:16:-1] + s[82] + s[15::-1]
  1000. elif len(s) == 85:
  1001. return s[3:11] + s[0] + s[12:55] + s[84] + s[56:84]
  1002. elif len(s) == 84:
  1003. return s[78:70:-1] + s[14] + s[69:37:-1] + s[70] + s[36:14:-1] + s[80] + s[:14][::-1]
  1004. elif len(s) == 83:
  1005. return s[80:63:-1] + s[0] + s[62:0:-1] + s[63]
  1006. elif len(s) == 82:
  1007. return s[80:37:-1] + s[7] + s[36:7:-1] + s[0] + s[6:0:-1] + s[37]
  1008. elif len(s) == 81:
  1009. 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]
  1010. elif len(s) == 80:
  1011. return s[1:19] + s[0] + s[20:68] + s[19] + s[69:80]
  1012. elif len(s) == 79:
  1013. 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]
  1014. else:
  1015. raise ExtractorError(u'Unable to decrypt signature, key length %d not supported; retrying might work' % (len(s)))
  1016. def _get_available_subtitles(self, video_id):
  1017. try:
  1018. sub_list = self._download_webpage(
  1019. 'http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id,
  1020. video_id, note=False)
  1021. except ExtractorError as err:
  1022. self._downloader.report_warning(u'unable to download video subtitles: %s' % compat_str(err))
  1023. return {}
  1024. lang_list = re.findall(r'name="([^"]*)"[^>]+lang_code="([\w\-]+)"', sub_list)
  1025. sub_lang_list = {}
  1026. for l in lang_list:
  1027. lang = l[1]
  1028. params = compat_urllib_parse.urlencode({
  1029. 'lang': lang,
  1030. 'v': video_id,
  1031. 'fmt': self._downloader.params.get('subtitlesformat'),
  1032. 'name': l[0],
  1033. })
  1034. url = u'http://www.youtube.com/api/timedtext?' + params
  1035. sub_lang_list[lang] = url
  1036. if not sub_lang_list:
  1037. self._downloader.report_warning(u'video doesn\'t have subtitles')
  1038. return {}
  1039. return sub_lang_list
  1040. def _get_available_automatic_caption(self, video_id, webpage):
  1041. """We need the webpage for getting the captions url, pass it as an
  1042. argument to speed up the process."""
  1043. sub_format = self._downloader.params.get('subtitlesformat')
  1044. self.to_screen(u'%s: Looking for automatic captions' % video_id)
  1045. mobj = re.search(r';ytplayer.config = ({.*?});', webpage)
  1046. err_msg = u'Couldn\'t find automatic captions for %s' % video_id
  1047. if mobj is None:
  1048. self._downloader.report_warning(err_msg)
  1049. return {}
  1050. player_config = json.loads(mobj.group(1))
  1051. try:
  1052. args = player_config[u'args']
  1053. caption_url = args[u'ttsurl']
  1054. timestamp = args[u'timestamp']
  1055. # We get the available subtitles
  1056. list_params = compat_urllib_parse.urlencode({
  1057. 'type': 'list',
  1058. 'tlangs': 1,
  1059. 'asrs': 1,
  1060. })
  1061. list_url = caption_url + '&' + list_params
  1062. list_page = self._download_webpage(list_url, video_id)
  1063. caption_list = xml.etree.ElementTree.fromstring(list_page.encode('utf-8'))
  1064. original_lang_node = caption_list.find('track')
  1065. if not original_lang_node or original_lang_node.attrib.get('kind') != 'asr' :
  1066. self._downloader.report_warning(u'Video doesn\'t have automatic captions')
  1067. return {}
  1068. original_lang = original_lang_node.attrib['lang_code']
  1069. sub_lang_list = {}
  1070. for lang_node in caption_list.findall('target'):
  1071. sub_lang = lang_node.attrib['lang_code']
  1072. params = compat_urllib_parse.urlencode({
  1073. 'lang': original_lang,
  1074. 'tlang': sub_lang,
  1075. 'fmt': sub_format,
  1076. 'ts': timestamp,
  1077. 'kind': 'asr',
  1078. })
  1079. sub_lang_list[sub_lang] = caption_url + '&' + params
  1080. return sub_lang_list
  1081. # An extractor error can be raise by the download process if there are
  1082. # no automatic captions but there are subtitles
  1083. except (KeyError, ExtractorError):
  1084. self._downloader.report_warning(err_msg)
  1085. return {}
  1086. def _print_formats(self, formats):
  1087. print('Available formats:')
  1088. for x in formats:
  1089. print('%s\t:\t%s\t[%s]%s' %(x, self._video_extensions.get(x, 'flv'),
  1090. self._video_dimensions.get(x, '???'),
  1091. ' ('+self._special_itags[x]+')' if x in self._special_itags else ''))
  1092. def _extract_id(self, url):
  1093. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  1094. if mobj is None:
  1095. raise ExtractorError(u'Invalid URL: %s' % url)
  1096. video_id = mobj.group(2)
  1097. return video_id
  1098. def _get_video_url_list(self, url_map):
  1099. """
  1100. Transform a dictionary in the format {itag:url} to a list of (itag, url)
  1101. with the requested formats.
  1102. """
  1103. req_format = self._downloader.params.get('format', None)
  1104. format_limit = self._downloader.params.get('format_limit', None)
  1105. available_formats = self._available_formats_prefer_free if self._downloader.params.get('prefer_free_formats', False) else self._available_formats
  1106. if format_limit is not None and format_limit in available_formats:
  1107. format_list = available_formats[available_formats.index(format_limit):]
  1108. else:
  1109. format_list = available_formats
  1110. existing_formats = [x for x in format_list if x in url_map]
  1111. if len(existing_formats) == 0:
  1112. raise ExtractorError(u'no known formats available for video')
  1113. if self._downloader.params.get('listformats', None):
  1114. self._print_formats(existing_formats)
  1115. return
  1116. if req_format is None or req_format == 'best':
  1117. video_url_list = [(existing_formats[0], url_map[existing_formats[0]])] # Best quality
  1118. elif req_format == 'worst':
  1119. video_url_list = [(existing_formats[-1], url_map[existing_formats[-1]])] # worst quality
  1120. elif req_format in ('-1', 'all'):
  1121. video_url_list = [(f, url_map[f]) for f in existing_formats] # All formats
  1122. else:
  1123. # Specific formats. We pick the first in a slash-delimeted sequence.
  1124. # Format can be specified as itag or 'mp4' or 'flv' etc. We pick the highest quality
  1125. # available in the specified format. For example,
  1126. # if '1/2/3/4' is requested and '2' and '4' are available, we pick '2'.
  1127. # if '1/mp4/3/4' is requested and '1' and '5' (is a mp4) are available, we pick '1'.
  1128. # if '1/mp4/3/4' is requested and '4' and '5' (is a mp4) are available, we pick '5'.
  1129. req_formats = req_format.split('/')
  1130. video_url_list = None
  1131. for rf in req_formats:
  1132. if rf in url_map:
  1133. video_url_list = [(rf, url_map[rf])]
  1134. break
  1135. if rf in self._video_formats_map:
  1136. for srf in self._video_formats_map[rf]:
  1137. if srf in url_map:
  1138. video_url_list = [(srf, url_map[srf])]
  1139. break
  1140. else:
  1141. continue
  1142. break
  1143. if video_url_list is None:
  1144. raise ExtractorError(u'requested format not available')
  1145. return video_url_list
  1146. def _extract_from_m3u8(self, manifest_url, video_id):
  1147. url_map = {}
  1148. def _get_urls(_manifest):
  1149. lines = _manifest.split('\n')
  1150. urls = filter(lambda l: l and not l.startswith('#'),
  1151. lines)
  1152. return urls
  1153. manifest = self._download_webpage(manifest_url, video_id, u'Downloading formats manifest')
  1154. formats_urls = _get_urls(manifest)
  1155. for format_url in formats_urls:
  1156. itag = self._search_regex(r'itag/(\d+?)/', format_url, 'itag')
  1157. url_map[itag] = format_url
  1158. return url_map
  1159. def _extract_annotations(self, video_id):
  1160. url = 'https://www.youtube.com/annotations_invideo?features=1&legacy=1&video_id=%s' % video_id
  1161. return self._download_webpage(url, video_id, note=u'Searching for annotations.', errnote=u'Unable to download video annotations.')
  1162. def _real_extract(self, url):
  1163. # Extract original video URL from URL with redirection, like age verification, using next_url parameter
  1164. mobj = re.search(self._NEXT_URL_RE, url)
  1165. if mobj:
  1166. url = 'https://www.youtube.com/' + compat_urllib_parse.unquote(mobj.group(1)).lstrip('/')
  1167. video_id = self._extract_id(url)
  1168. # Get video webpage
  1169. self.report_video_webpage_download(video_id)
  1170. url = 'https://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1' % video_id
  1171. request = compat_urllib_request.Request(url)
  1172. try:
  1173. video_webpage_bytes = compat_urllib_request.urlopen(request).read()
  1174. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  1175. raise ExtractorError(u'Unable to download video webpage: %s' % compat_str(err))
  1176. video_webpage = video_webpage_bytes.decode('utf-8', 'ignore')
  1177. # Attempt to extract SWF player URL
  1178. mobj = re.search(r'swfConfig.*?"(https?:\\/\\/.*?watch.*?-.*?\.swf)"', video_webpage)
  1179. if mobj is not None:
  1180. player_url = re.sub(r'\\(.)', r'\1', mobj.group(1))
  1181. else:
  1182. player_url = None
  1183. # Get video info
  1184. self.report_video_info_webpage_download(video_id)
  1185. if re.search(r'player-age-gate-content">', video_webpage) is not None:
  1186. self.report_age_confirmation()
  1187. age_gate = True
  1188. # We simulate the access to the video from www.youtube.com/v/{video_id}
  1189. # this can be viewed without login into Youtube
  1190. data = compat_urllib_parse.urlencode({'video_id': video_id,
  1191. 'el': 'embedded',
  1192. 'gl': 'US',
  1193. 'hl': 'en',
  1194. 'eurl': 'https://youtube.googleapis.com/v/' + video_id,
  1195. 'asv': 3,
  1196. 'sts':'1588',
  1197. })
  1198. video_info_url = 'https://www.youtube.com/get_video_info?' + data
  1199. video_info_webpage = self._download_webpage(video_info_url, video_id,
  1200. note=False,
  1201. errnote='unable to download video info webpage')
  1202. video_info = compat_parse_qs(video_info_webpage)
  1203. else:
  1204. age_gate = False
  1205. for el_type in ['&el=embedded', '&el=detailpage', '&el=vevo', '']:
  1206. video_info_url = ('https://www.youtube.com/get_video_info?&video_id=%s%s&ps=default&eurl=&gl=US&hl=en'
  1207. % (video_id, el_type))
  1208. video_info_webpage = self._download_webpage(video_info_url, video_id,
  1209. note=False,
  1210. errnote='unable to download video info webpage')
  1211. video_info = compat_parse_qs(video_info_webpage)
  1212. if 'token' in video_info:
  1213. break
  1214. if 'token' not in video_info:
  1215. if 'reason' in video_info:
  1216. raise ExtractorError(u'YouTube said: %s' % video_info['reason'][0], expected=True)
  1217. else:
  1218. raise ExtractorError(u'"token" parameter not in video info for unknown reason')
  1219. # Check for "rental" videos
  1220. if 'ypc_video_rental_bar_text' in video_info and 'author' not in video_info:
  1221. raise ExtractorError(u'"rental" videos not supported')
  1222. # Start extracting information
  1223. self.report_information_extraction(video_id)
  1224. # uploader
  1225. if 'author' not in video_info:
  1226. raise ExtractorError(u'Unable to extract uploader name')
  1227. video_uploader = compat_urllib_parse.unquote_plus(video_info['author'][0])
  1228. # uploader_id
  1229. video_uploader_id = None
  1230. mobj = re.search(r'<link itemprop="url" href="http://www.youtube.com/(?:user|channel)/([^"]+)">', video_webpage)
  1231. if mobj is not None:
  1232. video_uploader_id = mobj.group(1)
  1233. else:
  1234. self._downloader.report_warning(u'unable to extract uploader nickname')
  1235. # title
  1236. if 'title' in video_info:
  1237. video_title = compat_urllib_parse.unquote_plus(video_info['title'][0])
  1238. else:
  1239. self._downloader.report_warning(u'Unable to extract video title')
  1240. video_title = u'_'
  1241. # thumbnail image
  1242. # We try first to get a high quality image:
  1243. m_thumb = re.search(r'<span itemprop="thumbnail".*?href="(.*?)">',
  1244. video_webpage, re.DOTALL)
  1245. if m_thumb is not None:
  1246. video_thumbnail = m_thumb.group(1)
  1247. elif 'thumbnail_url' not in video_info:
  1248. self._downloader.report_warning(u'unable to extract video thumbnail')
  1249. video_thumbnail = None
  1250. else: # don't panic if we can't find it
  1251. video_thumbnail = compat_urllib_parse.unquote_plus(video_info['thumbnail_url'][0])
  1252. # upload date
  1253. upload_date = None
  1254. mobj = re.search(r'id="eow-date.*?>(.*?)</span>', video_webpage, re.DOTALL)
  1255. if mobj is not None:
  1256. upload_date = ' '.join(re.sub(r'[/,-]', r' ', mobj.group(1)).split())
  1257. upload_date = unified_strdate(upload_date)
  1258. # description
  1259. video_description = get_element_by_id("eow-description", video_webpage)
  1260. if video_description:
  1261. video_description = clean_html(video_description)
  1262. else:
  1263. fd_mobj = re.search(r'<meta name="description" content="([^"]+)"', video_webpage)
  1264. if fd_mobj:
  1265. video_description = unescapeHTML(fd_mobj.group(1))
  1266. else:
  1267. video_description = u''
  1268. # subtitles
  1269. video_subtitles = self.extract_subtitles(video_id, video_webpage)
  1270. if self._downloader.params.get('listsubtitles', False):
  1271. self._list_available_subtitles(video_id, video_webpage)
  1272. return
  1273. if 'length_seconds' not in video_info:
  1274. self._downloader.report_warning(u'unable to extract video duration')
  1275. video_duration = ''
  1276. else:
  1277. video_duration = compat_urllib_parse.unquote_plus(video_info['length_seconds'][0])
  1278. # annotations
  1279. video_annotations = None
  1280. if self._downloader.params.get('writeannotations', False):
  1281. video_annotations = self._extract_annotations(video_id)
  1282. # Decide which formats to download
  1283. try:
  1284. mobj = re.search(r';ytplayer.config = ({.*?});', video_webpage)
  1285. if not mobj:
  1286. raise ValueError('Could not find vevo ID')
  1287. info = json.loads(mobj.group(1))
  1288. args = info['args']
  1289. # Easy way to know if the 's' value is in url_encoded_fmt_stream_map
  1290. # this signatures are encrypted
  1291. if 'url_encoded_fmt_stream_map' not in args:
  1292. raise ValueError(u'No stream_map present') # caught below
  1293. m_s = re.search(r'[&,]s=', args['url_encoded_fmt_stream_map'])
  1294. if m_s is not None:
  1295. self.to_screen(u'%s: Encrypted signatures detected.' % video_id)
  1296. video_info['url_encoded_fmt_stream_map'] = [args['url_encoded_fmt_stream_map']]
  1297. m_s = re.search(r'[&,]s=', args.get('adaptive_fmts', u''))
  1298. if m_s is not None:
  1299. if 'url_encoded_fmt_stream_map' in video_info:
  1300. video_info['url_encoded_fmt_stream_map'][0] += ',' + args['adaptive_fmts']
  1301. else:
  1302. video_info['url_encoded_fmt_stream_map'] = [args['adaptive_fmts']]
  1303. elif 'adaptive_fmts' in video_info:
  1304. if 'url_encoded_fmt_stream_map' in video_info:
  1305. video_info['url_encoded_fmt_stream_map'][0] += ',' + video_info['adaptive_fmts'][0]
  1306. else:
  1307. video_info['url_encoded_fmt_stream_map'] = video_info['adaptive_fmts']
  1308. except ValueError:
  1309. pass
  1310. if 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
  1311. self.report_rtmp_download()
  1312. video_url_list = [(None, video_info['conn'][0])]
  1313. elif 'url_encoded_fmt_stream_map' in video_info and len(video_info['url_encoded_fmt_stream_map']) >= 1:
  1314. if 'rtmpe%3Dyes' in video_info['url_encoded_fmt_stream_map'][0]:
  1315. raise ExtractorError('rtmpe downloads are not supported, see https://github.com/rg3/youtube-dl/issues/343 for more information.', expected=True)
  1316. url_map = {}
  1317. for url_data_str in video_info['url_encoded_fmt_stream_map'][0].split(','):
  1318. url_data = compat_parse_qs(url_data_str)
  1319. if 'itag' in url_data and 'url' in url_data:
  1320. url = url_data['url'][0]
  1321. if 'sig' in url_data:
  1322. url += '&signature=' + url_data['sig'][0]
  1323. elif 's' in url_data:
  1324. encrypted_sig = url_data['s'][0]
  1325. if self._downloader.params.get('verbose'):
  1326. if age_gate:
  1327. if player_url is None:
  1328. player_version = 'unknown'
  1329. else:
  1330. player_version = self._search_regex(
  1331. r'-(.+)\.swf$', player_url,
  1332. u'flash player', fatal=False)
  1333. player_desc = 'flash player %s' % player_version
  1334. else:
  1335. player_version = self._search_regex(
  1336. r'html5player-(.+?)\.js', video_webpage,
  1337. 'html5 player', fatal=False)
  1338. player_desc = u'html5 player %s' % player_version
  1339. parts_sizes = u'.'.join(compat_str(len(part)) for part in encrypted_sig.split('.'))
  1340. self.to_screen(u'encrypted signature length %d (%s), itag %s, %s' %
  1341. (len(encrypted_sig), parts_sizes, url_data['itag'][0], player_desc))
  1342. if not age_gate:
  1343. jsplayer_url_json = self._search_regex(
  1344. r'"assets":.+?"js":\s*("[^"]+")',
  1345. video_webpage, u'JS player URL')
  1346. player_url = json.loads(jsplayer_url_json)
  1347. signature = self._decrypt_signature(
  1348. encrypted_sig, video_id, player_url, age_gate)
  1349. url += '&signature=' + signature
  1350. if 'ratebypass' not in url:
  1351. url += '&ratebypass=yes'
  1352. url_map[url_data['itag'][0]] = url
  1353. video_url_list = self._get_video_url_list(url_map)
  1354. if not video_url_list:
  1355. return
  1356. elif video_info.get('hlsvp'):
  1357. manifest_url = video_info['hlsvp'][0]
  1358. url_map = self._extract_from_m3u8(manifest_url, video_id)
  1359. video_url_list = self._get_video_url_list(url_map)
  1360. if not video_url_list:
  1361. return
  1362. else:
  1363. raise ExtractorError(u'no conn, hlsvp or url_encoded_fmt_stream_map information found in video info')
  1364. results = []
  1365. for format_param, video_real_url in video_url_list:
  1366. # Extension
  1367. video_extension = self._video_extensions.get(format_param, 'flv')
  1368. video_format = '{0} - {1}{2}'.format(format_param if format_param else video_extension,
  1369. self._video_dimensions.get(format_param, '???'),
  1370. ' ('+self._special_itags[format_param]+')' if format_param in self._special_itags else '')
  1371. results.append({
  1372. 'id': video_id,
  1373. 'url': video_real_url,
  1374. 'uploader': video_uploader,
  1375. 'uploader_id': video_uploader_id,
  1376. 'upload_date': upload_date,
  1377. 'title': video_title,
  1378. 'ext': video_extension,
  1379. 'format': video_format,
  1380. 'thumbnail': video_thumbnail,
  1381. 'description': video_description,
  1382. 'player_url': player_url,
  1383. 'subtitles': video_subtitles,
  1384. 'duration': video_duration,
  1385. 'age_limit': 18 if age_gate else 0,
  1386. 'annotations': video_annotations
  1387. })
  1388. return results
  1389. class YoutubePlaylistIE(InfoExtractor):
  1390. IE_DESC = u'YouTube.com playlists'
  1391. _VALID_URL = r"""(?:
  1392. (?:https?://)?
  1393. (?:\w+\.)?
  1394. youtube\.com/
  1395. (?:
  1396. (?:course|view_play_list|my_playlists|artist|playlist|watch)
  1397. \? (?:.*?&)*? (?:p|a|list)=
  1398. | p/
  1399. )
  1400. ((?:PL|EC|UU|FL)?[0-9A-Za-z-_]{10,})
  1401. .*
  1402. |
  1403. ((?:PL|EC|UU|FL)[0-9A-Za-z-_]{10,})
  1404. )"""
  1405. _TEMPLATE_URL = 'https://gdata.youtube.com/feeds/api/playlists/%s?max-results=%i&start-index=%i&v=2&alt=json&safeSearch=none'
  1406. _MAX_RESULTS = 50
  1407. IE_NAME = u'youtube:playlist'
  1408. @classmethod
  1409. def suitable(cls, url):
  1410. """Receives a URL and returns True if suitable for this IE."""
  1411. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  1412. def _real_extract(self, url):
  1413. # Extract playlist id
  1414. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  1415. if mobj is None:
  1416. raise ExtractorError(u'Invalid URL: %s' % url)
  1417. playlist_id = mobj.group(1) or mobj.group(2)
  1418. # Check if it's a video-specific URL
  1419. query_dict = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
  1420. if 'v' in query_dict:
  1421. video_id = query_dict['v'][0]
  1422. if self._downloader.params.get('noplaylist'):
  1423. self.to_screen(u'Downloading just video %s because of --no-playlist' % video_id)
  1424. return self.url_result('https://www.youtube.com/watch?v=' + video_id, 'Youtube')
  1425. else:
  1426. self.to_screen(u'Downloading playlist PL%s - add --no-playlist to just download video %s' % (playlist_id, video_id))
  1427. # Download playlist videos from API
  1428. videos = []
  1429. for page_num in itertools.count(1):
  1430. start_index = self._MAX_RESULTS * (page_num - 1) + 1
  1431. if start_index >= 1000:
  1432. self._downloader.report_warning(u'Max number of results reached')
  1433. break
  1434. url = self._TEMPLATE_URL % (playlist_id, self._MAX_RESULTS, start_index)
  1435. page = self._download_webpage(url, playlist_id, u'Downloading page #%s' % page_num)
  1436. try:
  1437. response = json.loads(page)
  1438. except ValueError as err:
  1439. raise ExtractorError(u'Invalid JSON in API response: ' + compat_str(err))
  1440. if 'feed' not in response:
  1441. raise ExtractorError(u'Got a malformed response from YouTube API')
  1442. playlist_title = response['feed']['title']['$t']
  1443. if 'entry' not in response['feed']:
  1444. # Number of videos is a multiple of self._MAX_RESULTS
  1445. break
  1446. for entry in response['feed']['entry']:
  1447. index = entry['yt$position']['$t']
  1448. if 'media$group' in entry and 'yt$videoid' in entry['media$group']:
  1449. videos.append((
  1450. index,
  1451. 'https://www.youtube.com/watch?v=' + entry['media$group']['yt$videoid']['$t']
  1452. ))
  1453. videos = [v[1] for v in sorted(videos)]
  1454. url_results = [self.url_result(vurl, 'Youtube') for vurl in videos]
  1455. return [self.playlist_result(url_results, playlist_id, playlist_title)]
  1456. class YoutubeChannelIE(InfoExtractor):
  1457. IE_DESC = u'YouTube.com channels'
  1458. _VALID_URL = r"^(?:https?://)?(?:youtu\.be|(?:\w+\.)?youtube(?:-nocookie)?\.com)/channel/([0-9A-Za-z_-]+)"
  1459. _TEMPLATE_URL = 'http://www.youtube.com/channel/%s/videos?sort=da&flow=list&view=0&page=%s&gl=US&hl=en'
  1460. _MORE_PAGES_INDICATOR = 'yt-uix-load-more'
  1461. _MORE_PAGES_URL = 'http://www.youtube.com/c4_browse_ajax?action_load_more_videos=1&flow=list&paging=%s&view=0&sort=da&channel_id=%s'
  1462. IE_NAME = u'youtube:channel'
  1463. def extract_videos_from_page(self, page):
  1464. ids_in_page = []
  1465. for mobj in re.finditer(r'href="/watch\?v=([0-9A-Za-z_-]+)&?', page):
  1466. if mobj.group(1) not in ids_in_page:
  1467. ids_in_page.append(mobj.group(1))
  1468. return ids_in_page
  1469. def _real_extract(self, url):
  1470. # Extract channel id
  1471. mobj = re.match(self._VALID_URL, url)
  1472. if mobj is None:
  1473. raise ExtractorError(u'Invalid URL: %s' % url)
  1474. # Download channel page
  1475. channel_id = mobj.group(1)
  1476. video_ids = []
  1477. pagenum = 1
  1478. url = self._TEMPLATE_URL % (channel_id, pagenum)
  1479. page = self._download_webpage(url, channel_id,
  1480. u'Downloading page #%s' % pagenum)
  1481. # Extract video identifiers
  1482. ids_in_page = self.extract_videos_from_page(page)
  1483. video_ids.extend(ids_in_page)
  1484. # Download any subsequent channel pages using the json-based channel_ajax query
  1485. if self._MORE_PAGES_INDICATOR in page:
  1486. for pagenum in itertools.count(1):
  1487. url = self._MORE_PAGES_URL % (pagenum, channel_id)
  1488. page = self._download_webpage(url, channel_id,
  1489. u'Downloading page #%s' % pagenum)
  1490. page = json.loads(page)
  1491. ids_in_page = self.extract_videos_from_page(page['content_html'])
  1492. video_ids.extend(ids_in_page)
  1493. if self._MORE_PAGES_INDICATOR not in page['load_more_widget_html']:
  1494. break
  1495. self._downloader.to_screen(u'[youtube] Channel %s: Found %i videos' % (channel_id, len(video_ids)))
  1496. urls = ['http://www.youtube.com/watch?v=%s' % id for id in video_ids]
  1497. url_entries = [self.url_result(eurl, 'Youtube') for eurl in urls]
  1498. return [self.playlist_result(url_entries, channel_id)]
  1499. class YoutubeUserIE(InfoExtractor):
  1500. IE_DESC = u'YouTube.com user videos (URL or "ytuser" keyword)'
  1501. _VALID_URL = r'(?:(?:(?:https?://)?(?:\w+\.)?youtube\.com/(?:user/)?(?!(?:attribution_link|watch)(?:$|[^a-z_A-Z0-9-])))|ytuser:)(?!feed/)([A-Za-z0-9_-]+)'
  1502. _TEMPLATE_URL = 'http://gdata.youtube.com/feeds/api/users/%s'
  1503. _GDATA_PAGE_SIZE = 50
  1504. _GDATA_URL = 'http://gdata.youtube.com/feeds/api/users/%s/uploads?max-results=%d&start-index=%d&alt=json'
  1505. IE_NAME = u'youtube:user'
  1506. @classmethod
  1507. def suitable(cls, url):
  1508. # Don't return True if the url can be extracted with other youtube
  1509. # extractor, the regex would is too permissive and it would match.
  1510. other_ies = iter(klass for (name, klass) in globals().items() if name.endswith('IE') and klass is not cls)
  1511. if any(ie.suitable(url) for ie in other_ies): return False
  1512. else: return super(YoutubeUserIE, cls).suitable(url)
  1513. def _real_extract(self, url):
  1514. # Extract username
  1515. mobj = re.match(self._VALID_URL, url)
  1516. if mobj is None:
  1517. raise ExtractorError(u'Invalid URL: %s' % url)
  1518. username = mobj.group(1)
  1519. # Download video ids using YouTube Data API. Result size per
  1520. # query is limited (currently to 50 videos) so we need to query
  1521. # page by page until there are no video ids - it means we got
  1522. # all of them.
  1523. video_ids = []
  1524. for pagenum in itertools.count(0):
  1525. start_index = pagenum * self._GDATA_PAGE_SIZE + 1
  1526. gdata_url = self._GDATA_URL % (username, self._GDATA_PAGE_SIZE, start_index)
  1527. page = self._download_webpage(gdata_url, username,
  1528. u'Downloading video ids from %d to %d' % (start_index, start_index + self._GDATA_PAGE_SIZE))
  1529. try:
  1530. response = json.loads(page)
  1531. except ValueError as err:
  1532. raise ExtractorError(u'Invalid JSON in API response: ' + compat_str(err))
  1533. if 'entry' not in response['feed']:
  1534. # Number of videos is a multiple of self._MAX_RESULTS
  1535. break
  1536. # Extract video identifiers
  1537. ids_in_page = []
  1538. for entry in response['feed']['entry']:
  1539. ids_in_page.append(entry['id']['$t'].split('/')[-1])
  1540. video_ids.extend(ids_in_page)
  1541. # A little optimization - if current page is not
  1542. # "full", ie. does not contain PAGE_SIZE video ids then
  1543. # we can assume that this page is the last one - there
  1544. # are no more ids on further pages - no need to query
  1545. # again.
  1546. if len(ids_in_page) < self._GDATA_PAGE_SIZE:
  1547. break
  1548. urls = ['http://www.youtube.com/watch?v=%s' % video_id for video_id in video_ids]
  1549. url_results = [self.url_result(rurl, 'Youtube') for rurl in urls]
  1550. return [self.playlist_result(url_results, playlist_title = username)]
  1551. class YoutubeSearchIE(SearchInfoExtractor):
  1552. IE_DESC = u'YouTube.com searches'
  1553. _API_URL = 'https://gdata.youtube.com/feeds/api/videos?q=%s&start-index=%i&max-results=50&v=2&alt=jsonc'
  1554. _MAX_RESULTS = 1000
  1555. IE_NAME = u'youtube:search'
  1556. _SEARCH_KEY = 'ytsearch'
  1557. def report_download_page(self, query, pagenum):
  1558. """Report attempt to download search page with given number."""
  1559. self._downloader.to_screen(u'[youtube] query "%s": Downloading page %s' % (query, pagenum))
  1560. def _get_n_results(self, query, n):
  1561. """Get a specified number of results for a query"""
  1562. video_ids = []
  1563. pagenum = 0
  1564. limit = n
  1565. while (50 * pagenum) < limit:
  1566. self.report_download_page(query, pagenum+1)
  1567. result_url = self._API_URL % (compat_urllib_parse.quote_plus(query), (50*pagenum)+1)
  1568. request = compat_urllib_request.Request(result_url)
  1569. try:
  1570. data = compat_urllib_request.urlopen(request).read().decode('utf-8')
  1571. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  1572. raise ExtractorError(u'Unable to download API page: %s' % compat_str(err))
  1573. api_response = json.loads(data)['data']
  1574. if not 'items' in api_response:
  1575. raise ExtractorError(u'[youtube] No video results')
  1576. new_ids = list(video['id'] for video in api_response['items'])
  1577. video_ids += new_ids
  1578. limit = min(n, api_response['totalItems'])
  1579. pagenum += 1
  1580. if len(video_ids) > n:
  1581. video_ids = video_ids[:n]
  1582. videos = [self.url_result('http://www.youtube.com/watch?v=%s' % id, 'Youtube') for id in video_ids]
  1583. return self.playlist_result(videos, query)
  1584. class YoutubeShowIE(InfoExtractor):
  1585. IE_DESC = u'YouTube.com (multi-season) shows'
  1586. _VALID_URL = r'https?://www\.youtube\.com/show/(.*)'
  1587. IE_NAME = u'youtube:show'
  1588. def _real_extract(self, url):
  1589. mobj = re.match(self._VALID_URL, url)
  1590. show_name = mobj.group(1)
  1591. webpage = self._download_webpage(url, show_name, u'Downloading show webpage')
  1592. # There's one playlist for each season of the show
  1593. m_seasons = list(re.finditer(r'href="(/playlist\?list=.*?)"', webpage))
  1594. self.to_screen(u'%s: Found %s seasons' % (show_name, len(m_seasons)))
  1595. return [self.url_result('https://www.youtube.com' + season.group(1), 'YoutubePlaylist') for season in m_seasons]
  1596. class YoutubeFeedsInfoExtractor(YoutubeBaseInfoExtractor):
  1597. """
  1598. Base class for extractors that fetch info from
  1599. http://www.youtube.com/feed_ajax
  1600. Subclasses must define the _FEED_NAME and _PLAYLIST_TITLE properties.
  1601. """
  1602. _LOGIN_REQUIRED = True
  1603. _PAGING_STEP = 30
  1604. # use action_load_personal_feed instead of action_load_system_feed
  1605. _PERSONAL_FEED = False
  1606. @property
  1607. def _FEED_TEMPLATE(self):
  1608. action = 'action_load_system_feed'
  1609. if self._PERSONAL_FEED:
  1610. action = 'action_load_personal_feed'
  1611. return 'http://www.youtube.com/feed_ajax?%s=1&feed_name=%s&paging=%%s' % (action, self._FEED_NAME)
  1612. @property
  1613. def IE_NAME(self):
  1614. return u'youtube:%s' % self._FEED_NAME
  1615. def _real_initialize(self):
  1616. self._login()
  1617. def _real_extract(self, url):
  1618. feed_entries = []
  1619. # The step argument is available only in 2.7 or higher
  1620. for i in itertools.count(0):
  1621. paging = i*self._PAGING_STEP
  1622. info = self._download_webpage(self._FEED_TEMPLATE % paging,
  1623. u'%s feed' % self._FEED_NAME,
  1624. u'Downloading page %s' % i)
  1625. info = json.loads(info)
  1626. feed_html = info['feed_html']
  1627. m_ids = re.finditer(r'"/watch\?v=(.*?)["&]', feed_html)
  1628. ids = orderedSet(m.group(1) for m in m_ids)
  1629. feed_entries.extend(self.url_result(id, 'Youtube') for id in ids)
  1630. if info['paging'] is None:
  1631. break
  1632. return self.playlist_result(feed_entries, playlist_title=self._PLAYLIST_TITLE)
  1633. class YoutubeSubscriptionsIE(YoutubeFeedsInfoExtractor):
  1634. IE_DESC = u'YouTube.com subscriptions feed, "ytsubs" keyword(requires authentication)'
  1635. _VALID_URL = r'https?://www\.youtube\.com/feed/subscriptions|:ytsubs(?:criptions)?'
  1636. _FEED_NAME = 'subscriptions'
  1637. _PLAYLIST_TITLE = u'Youtube Subscriptions'
  1638. class YoutubeRecommendedIE(YoutubeFeedsInfoExtractor):
  1639. IE_DESC = u'YouTube.com recommended videos, "ytrec" keyword (requires authentication)'
  1640. _VALID_URL = r'https?://www\.youtube\.com/feed/recommended|:ytrec(?:ommended)?'
  1641. _FEED_NAME = 'recommended'
  1642. _PLAYLIST_TITLE = u'Youtube Recommended videos'
  1643. class YoutubeWatchLaterIE(YoutubeFeedsInfoExtractor):
  1644. IE_DESC = u'Youtube watch later list, "ytwatchlater" keyword (requires authentication)'
  1645. _VALID_URL = r'https?://www\.youtube\.com/feed/watch_later|:ytwatchlater'
  1646. _FEED_NAME = 'watch_later'
  1647. _PLAYLIST_TITLE = u'Youtube Watch Later'
  1648. _PAGING_STEP = 100
  1649. _PERSONAL_FEED = True
  1650. class YoutubeFavouritesIE(YoutubeBaseInfoExtractor):
  1651. IE_NAME = u'youtube:favorites'
  1652. IE_DESC = u'YouTube.com favourite videos, "ytfav" keyword (requires authentication)'
  1653. _VALID_URL = r'https?://www\.youtube\.com/my_favorites|:ytfav(?:ou?rites)?'
  1654. _LOGIN_REQUIRED = True
  1655. def _real_extract(self, url):
  1656. webpage = self._download_webpage('https://www.youtube.com/my_favorites', 'Youtube Favourites videos')
  1657. playlist_id = self._search_regex(r'list=(.+?)["&]', webpage, u'favourites playlist id')
  1658. return self.url_result(playlist_id, 'YoutubePlaylist')
  1659. class YoutubeTruncatedURLIE(InfoExtractor):
  1660. IE_NAME = 'youtube:truncated_url'
  1661. IE_DESC = False # Do not list
  1662. _VALID_URL = r'(?:https?://)?[^/]+/watch\?feature=[a-z_]+$'
  1663. def _real_extract(self, url):
  1664. raise ExtractorError(
  1665. u'Did you forget to quote the URL? Remember that & is a meta '
  1666. u'character in most shells, so you want to put the URL in quotes, '
  1667. u'like youtube-dl '
  1668. u'\'http://www.youtube.com/watch?feature=foo&v=BaW_jenozKc\''
  1669. u' (or simply youtube-dl BaW_jenozKc ).',
  1670. expected=True)