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.

1828 lines
80 KiB

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