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.

968 lines
39 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. import itertools
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_HTTPError,
  9. compat_str,
  10. compat_urlparse,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. InAdvancePagedList,
  16. int_or_none,
  17. NO_DEFAULT,
  18. RegexNotFoundError,
  19. sanitized_Request,
  20. smuggle_url,
  21. std_headers,
  22. unified_strdate,
  23. unsmuggle_url,
  24. urlencode_postdata,
  25. unescapeHTML,
  26. parse_filesize,
  27. try_get,
  28. )
  29. class VimeoBaseInfoExtractor(InfoExtractor):
  30. _NETRC_MACHINE = 'vimeo'
  31. _LOGIN_REQUIRED = False
  32. _LOGIN_URL = 'https://vimeo.com/log_in'
  33. def _login(self):
  34. (username, password) = self._get_login_info()
  35. if username is None:
  36. if self._LOGIN_REQUIRED:
  37. raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  38. return
  39. self.report_login()
  40. webpage = self._download_webpage(self._LOGIN_URL, None, False)
  41. token, vuid = self._extract_xsrft_and_vuid(webpage)
  42. data = urlencode_postdata({
  43. 'action': 'login',
  44. 'email': username,
  45. 'password': password,
  46. 'service': 'vimeo',
  47. 'token': token,
  48. })
  49. login_request = sanitized_Request(self._LOGIN_URL, data)
  50. login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  51. login_request.add_header('Referer', self._LOGIN_URL)
  52. self._set_vimeo_cookie('vuid', vuid)
  53. self._download_webpage(login_request, None, False, 'Wrong login info')
  54. def _verify_video_password(self, url, video_id, webpage):
  55. password = self._downloader.params.get('videopassword')
  56. if password is None:
  57. raise ExtractorError('This video is protected by a password, use the --video-password option', expected=True)
  58. token, vuid = self._extract_xsrft_and_vuid(webpage)
  59. data = urlencode_postdata({
  60. 'password': password,
  61. 'token': token,
  62. })
  63. if url.startswith('http://'):
  64. # vimeo only supports https now, but the user can give an http url
  65. url = url.replace('http://', 'https://')
  66. password_request = sanitized_Request(url + '/password', data)
  67. password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  68. password_request.add_header('Referer', url)
  69. self._set_vimeo_cookie('vuid', vuid)
  70. return self._download_webpage(
  71. password_request, video_id,
  72. 'Verifying the password', 'Wrong password')
  73. def _extract_xsrft_and_vuid(self, webpage):
  74. xsrft = self._search_regex(
  75. r'(?:(?P<q1>["\'])xsrft(?P=q1)\s*:|xsrft\s*[=:])\s*(?P<q>["\'])(?P<xsrft>.+?)(?P=q)',
  76. webpage, 'login token', group='xsrft')
  77. vuid = self._search_regex(
  78. r'["\']vuid["\']\s*:\s*(["\'])(?P<vuid>.+?)\1',
  79. webpage, 'vuid', group='vuid')
  80. return xsrft, vuid
  81. def _set_vimeo_cookie(self, name, value):
  82. self._set_cookie('vimeo.com', name, value)
  83. def _vimeo_sort_formats(self, formats):
  84. # Bitrates are completely broken. Single m3u8 may contain entries in kbps and bps
  85. # at the same time without actual units specified. This lead to wrong sorting.
  86. self._sort_formats(formats, field_preference=('preference', 'height', 'width', 'fps', 'format_id'))
  87. def _parse_config(self, config, video_id):
  88. # Extract title
  89. video_title = config['video']['title']
  90. # Extract uploader, uploader_url and uploader_id
  91. video_uploader = config['video'].get('owner', {}).get('name')
  92. video_uploader_url = config['video'].get('owner', {}).get('url')
  93. video_uploader_id = video_uploader_url.split('/')[-1] if video_uploader_url else None
  94. # Extract video thumbnail
  95. video_thumbnail = config['video'].get('thumbnail')
  96. if video_thumbnail is None:
  97. video_thumbs = config['video'].get('thumbs')
  98. if video_thumbs and isinstance(video_thumbs, dict):
  99. _, video_thumbnail = sorted((int(width if width.isdigit() else 0), t_url) for (width, t_url) in video_thumbs.items())[-1]
  100. # Extract video duration
  101. video_duration = int_or_none(config['video'].get('duration'))
  102. formats = []
  103. config_files = config['video'].get('files') or config['request'].get('files', {})
  104. for f in config_files.get('progressive', []):
  105. video_url = f.get('url')
  106. if not video_url:
  107. continue
  108. formats.append({
  109. 'url': video_url,
  110. 'format_id': 'http-%s' % f.get('quality'),
  111. 'width': int_or_none(f.get('width')),
  112. 'height': int_or_none(f.get('height')),
  113. 'fps': int_or_none(f.get('fps')),
  114. 'tbr': int_or_none(f.get('bitrate')),
  115. })
  116. m3u8_url = config_files.get('hls', {}).get('url')
  117. if m3u8_url:
  118. formats.extend(self._extract_m3u8_formats(
  119. m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  120. subtitles = {}
  121. text_tracks = config['request'].get('text_tracks')
  122. if text_tracks:
  123. for tt in text_tracks:
  124. subtitles[tt['lang']] = [{
  125. 'ext': 'vtt',
  126. 'url': 'https://vimeo.com' + tt['url'],
  127. }]
  128. return {
  129. 'title': video_title,
  130. 'uploader': video_uploader,
  131. 'uploader_id': video_uploader_id,
  132. 'uploader_url': video_uploader_url,
  133. 'thumbnail': video_thumbnail,
  134. 'duration': video_duration,
  135. 'formats': formats,
  136. 'subtitles': subtitles,
  137. }
  138. class VimeoIE(VimeoBaseInfoExtractor):
  139. """Information extractor for vimeo.com."""
  140. # _VALID_URL matches Vimeo URLs
  141. _VALID_URL = r'''(?x)
  142. https?://
  143. (?:
  144. (?:
  145. www|
  146. (?P<player>player)
  147. )
  148. \.
  149. )?
  150. vimeo(?P<pro>pro)?\.com/
  151. (?!(?:channels|album)/[^/?#]+/?(?:$|[?#])|[^/]+/review/|ondemand/)
  152. (?:.*?/)?
  153. (?:
  154. (?:
  155. play_redirect_hls|
  156. moogaloop\.swf)\?clip_id=
  157. )?
  158. (?:videos?/)?
  159. (?P<id>[0-9]+)
  160. (?:/[\da-f]+)?
  161. /?(?:[?&].*)?(?:[#].*)?$
  162. '''
  163. IE_NAME = 'vimeo'
  164. _TESTS = [
  165. {
  166. 'url': 'http://vimeo.com/56015672#at=0',
  167. 'md5': '8879b6cc097e987f02484baf890129e5',
  168. 'info_dict': {
  169. 'id': '56015672',
  170. 'ext': 'mp4',
  171. 'title': "youtube-dl test video - \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
  172. 'description': 'md5:2d3305bad981a06ff79f027f19865021',
  173. 'upload_date': '20121220',
  174. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/user7108434',
  175. 'uploader_id': 'user7108434',
  176. 'uploader': 'Filippo Valsorda',
  177. 'duration': 10,
  178. },
  179. },
  180. {
  181. 'url': 'http://vimeopro.com/openstreetmapus/state-of-the-map-us-2013/video/68093876',
  182. 'md5': '3b5ca6aa22b60dfeeadf50b72e44ed82',
  183. 'note': 'Vimeo Pro video (#1197)',
  184. 'info_dict': {
  185. 'id': '68093876',
  186. 'ext': 'mp4',
  187. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/openstreetmapus',
  188. 'uploader_id': 'openstreetmapus',
  189. 'uploader': 'OpenStreetMap US',
  190. 'title': 'Andy Allan - Putting the Carto into OpenStreetMap Cartography',
  191. 'description': 'md5:fd69a7b8d8c34a4e1d2ec2e4afd6ec30',
  192. 'duration': 1595,
  193. },
  194. },
  195. {
  196. 'url': 'http://player.vimeo.com/video/54469442',
  197. 'md5': '619b811a4417aa4abe78dc653becf511',
  198. 'note': 'Videos that embed the url in the player page',
  199. 'info_dict': {
  200. 'id': '54469442',
  201. 'ext': 'mp4',
  202. 'title': 'Kathy Sierra: Building the minimum Badass User, Business of Software 2012',
  203. 'uploader': 'The BLN & Business of Software',
  204. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/theblnbusinessofsoftware',
  205. 'uploader_id': 'theblnbusinessofsoftware',
  206. 'duration': 3610,
  207. 'description': None,
  208. },
  209. },
  210. {
  211. 'url': 'http://vimeo.com/68375962',
  212. 'md5': 'aaf896bdb7ddd6476df50007a0ac0ae7',
  213. 'note': 'Video protected with password',
  214. 'info_dict': {
  215. 'id': '68375962',
  216. 'ext': 'mp4',
  217. 'title': 'youtube-dl password protected test video',
  218. 'upload_date': '20130614',
  219. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/user18948128',
  220. 'uploader_id': 'user18948128',
  221. 'uploader': 'Jaime Marquínez Ferrándiz',
  222. 'duration': 10,
  223. 'description': 'This is "youtube-dl password protected test video" by on Vimeo, the home for high quality videos and the people who love them.',
  224. },
  225. 'params': {
  226. 'videopassword': 'youtube-dl',
  227. },
  228. },
  229. {
  230. 'url': 'http://vimeo.com/channels/keypeele/75629013',
  231. 'md5': '2f86a05afe9d7abc0b9126d229bbe15d',
  232. 'info_dict': {
  233. 'id': '75629013',
  234. 'ext': 'mp4',
  235. 'title': 'Key & Peele: Terrorist Interrogation',
  236. 'description': 'md5:8678b246399b070816b12313e8b4eb5c',
  237. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/atencio',
  238. 'uploader_id': 'atencio',
  239. 'uploader': 'Peter Atencio',
  240. 'upload_date': '20130927',
  241. 'duration': 187,
  242. },
  243. },
  244. {
  245. 'url': 'http://vimeo.com/76979871',
  246. 'note': 'Video with subtitles',
  247. 'info_dict': {
  248. 'id': '76979871',
  249. 'ext': 'mp4',
  250. 'title': 'The New Vimeo Player (You Know, For Videos)',
  251. 'description': 'md5:2ec900bf97c3f389378a96aee11260ea',
  252. 'upload_date': '20131015',
  253. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/staff',
  254. 'uploader_id': 'staff',
  255. 'uploader': 'Vimeo Staff',
  256. 'duration': 62,
  257. }
  258. },
  259. {
  260. # from https://www.ouya.tv/game/Pier-Solar-and-the-Great-Architects/
  261. 'url': 'https://player.vimeo.com/video/98044508',
  262. 'note': 'The js code contains assignments to the same variable as the config',
  263. 'info_dict': {
  264. 'id': '98044508',
  265. 'ext': 'mp4',
  266. 'title': 'Pier Solar OUYA Official Trailer',
  267. 'uploader': 'Tulio Gonçalves',
  268. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/user28849593',
  269. 'uploader_id': 'user28849593',
  270. },
  271. },
  272. {
  273. # contains original format
  274. 'url': 'https://vimeo.com/33951933',
  275. 'md5': '2d9f5475e0537f013d0073e812ab89e6',
  276. 'info_dict': {
  277. 'id': '33951933',
  278. 'ext': 'mp4',
  279. 'title': 'FOX CLASSICS - Forever Classic ID - A Full Minute',
  280. 'uploader': 'The DMCI',
  281. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/dmci',
  282. 'uploader_id': 'dmci',
  283. 'upload_date': '20111220',
  284. 'description': 'md5:ae23671e82d05415868f7ad1aec21147',
  285. },
  286. },
  287. {
  288. # only available via https://vimeo.com/channels/tributes/6213729 and
  289. # not via https://vimeo.com/6213729
  290. 'url': 'https://vimeo.com/channels/tributes/6213729',
  291. 'info_dict': {
  292. 'id': '6213729',
  293. 'ext': 'mp4',
  294. 'title': 'Vimeo Tribute: The Shining',
  295. 'uploader': 'Casey Donahue',
  296. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/caseydonahue',
  297. 'uploader_id': 'caseydonahue',
  298. 'upload_date': '20090821',
  299. 'description': 'md5:bdbf314014e58713e6e5b66eb252f4a6',
  300. },
  301. 'params': {
  302. 'skip_download': True,
  303. },
  304. 'expected_warnings': ['Unable to download JSON metadata'],
  305. },
  306. {
  307. # redirects to ondemand extractor and should be passed throught it
  308. # for successful extraction
  309. 'url': 'https://vimeo.com/73445910',
  310. 'info_dict': {
  311. 'id': '73445910',
  312. 'ext': 'mp4',
  313. 'title': 'The Reluctant Revolutionary',
  314. 'uploader': '10Ft Films',
  315. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/tenfootfilms',
  316. 'uploader_id': 'tenfootfilms',
  317. },
  318. 'params': {
  319. 'skip_download': True,
  320. },
  321. },
  322. {
  323. 'url': 'http://vimeo.com/moogaloop.swf?clip_id=2539741',
  324. 'only_matching': True,
  325. },
  326. {
  327. 'url': 'https://vimeo.com/109815029',
  328. 'note': 'Video not completely processed, "failed" seed status',
  329. 'only_matching': True,
  330. },
  331. {
  332. 'url': 'https://vimeo.com/groups/travelhd/videos/22439234',
  333. 'only_matching': True,
  334. },
  335. {
  336. 'url': 'https://vimeo.com/album/2632481/video/79010983',
  337. 'only_matching': True,
  338. },
  339. {
  340. # source file returns 403: Forbidden
  341. 'url': 'https://vimeo.com/7809605',
  342. 'only_matching': True,
  343. },
  344. {
  345. 'url': 'https://vimeo.com/160743502/abd0e13fb4',
  346. 'only_matching': True,
  347. }
  348. ]
  349. @staticmethod
  350. def _smuggle_referrer(url, referrer_url):
  351. return smuggle_url(url, {'http_headers': {'Referer': referrer_url}})
  352. @staticmethod
  353. def _extract_urls(url, webpage):
  354. urls = []
  355. # Look for embedded (iframe) Vimeo player
  356. for mobj in re.finditer(
  357. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.vimeo\.com/video/.+?)\1',
  358. webpage):
  359. urls.append(VimeoIE._smuggle_referrer(unescapeHTML(mobj.group('url')), url))
  360. PLAIN_EMBED_RE = (
  361. # Look for embedded (swf embed) Vimeo player
  362. r'<embed[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?vimeo\.com/moogaloop\.swf.+?)\1',
  363. # Look more for non-standard embedded Vimeo player
  364. r'<video[^>]+src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?vimeo\.com/[0-9]+)\1',
  365. )
  366. for embed_re in PLAIN_EMBED_RE:
  367. for mobj in re.finditer(embed_re, webpage):
  368. urls.append(mobj.group('url'))
  369. return urls
  370. @staticmethod
  371. def _extract_url(url, webpage):
  372. urls = VimeoIE._extract_urls(url, webpage)
  373. return urls[0] if urls else None
  374. def _verify_player_video_password(self, url, video_id):
  375. password = self._downloader.params.get('videopassword')
  376. if password is None:
  377. raise ExtractorError('This video is protected by a password, use the --video-password option')
  378. data = urlencode_postdata({'password': password})
  379. pass_url = url + '/check-password'
  380. password_request = sanitized_Request(pass_url, data)
  381. password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  382. password_request.add_header('Referer', url)
  383. return self._download_json(
  384. password_request, video_id,
  385. 'Verifying the password', 'Wrong password')
  386. def _real_initialize(self):
  387. self._login()
  388. def _real_extract(self, url):
  389. url, data = unsmuggle_url(url, {})
  390. headers = std_headers.copy()
  391. if 'http_headers' in data:
  392. headers.update(data['http_headers'])
  393. if 'Referer' not in headers:
  394. headers['Referer'] = url
  395. # Extract ID from URL
  396. mobj = re.match(self._VALID_URL, url)
  397. video_id = mobj.group('id')
  398. orig_url = url
  399. if mobj.group('pro') or mobj.group('player'):
  400. url = 'https://player.vimeo.com/video/' + video_id
  401. elif any(p in url for p in ('play_redirect_hls', 'moogaloop.swf')):
  402. url = 'https://vimeo.com/' + video_id
  403. # Retrieve video webpage to extract further information
  404. request = sanitized_Request(url, headers=headers)
  405. try:
  406. webpage, urlh = self._download_webpage_handle(request, video_id)
  407. # Some URLs redirect to ondemand can't be extracted with
  408. # this extractor right away thus should be passed through
  409. # ondemand extractor (e.g. https://vimeo.com/73445910)
  410. if VimeoOndemandIE.suitable(urlh.geturl()):
  411. return self.url_result(urlh.geturl(), VimeoOndemandIE.ie_key())
  412. except ExtractorError as ee:
  413. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
  414. errmsg = ee.cause.read()
  415. if b'Because of its privacy settings, this video cannot be played here' in errmsg:
  416. raise ExtractorError(
  417. 'Cannot download embed-only video without embedding '
  418. 'URL. Please call youtube-dl with the URL of the page '
  419. 'that embeds this video.',
  420. expected=True)
  421. raise
  422. # Now we begin extracting as much information as we can from what we
  423. # retrieved. First we extract the information common to all extractors,
  424. # and latter we extract those that are Vimeo specific.
  425. self.report_extraction(video_id)
  426. vimeo_config = self._search_regex(
  427. r'vimeo\.config\s*=\s*(?:({.+?})|_extend\([^,]+,\s+({.+?})\));', webpage,
  428. 'vimeo config', default=None)
  429. if vimeo_config:
  430. seed_status = self._parse_json(vimeo_config, video_id).get('seed_status', {})
  431. if seed_status.get('state') == 'failed':
  432. raise ExtractorError(
  433. '%s said: %s' % (self.IE_NAME, seed_status['title']),
  434. expected=True)
  435. # Extract the config JSON
  436. try:
  437. try:
  438. config_url = self._html_search_regex(
  439. r' data-config-url="(.+?)"', webpage,
  440. 'config URL', default=None)
  441. if not config_url:
  442. # Sometimes new react-based page is served instead of old one that require
  443. # different config URL extraction approach (see
  444. # https://github.com/rg3/youtube-dl/pull/7209)
  445. vimeo_clip_page_config = self._search_regex(
  446. r'vimeo\.clip_page_config\s*=\s*({.+?});', webpage,
  447. 'vimeo clip page config')
  448. config_url = self._parse_json(
  449. vimeo_clip_page_config, video_id)['player']['config_url']
  450. config_json = self._download_webpage(config_url, video_id)
  451. config = json.loads(config_json)
  452. except RegexNotFoundError:
  453. # For pro videos or player.vimeo.com urls
  454. # We try to find out to which variable is assigned the config dic
  455. m_variable_name = re.search('(\w)\.video\.id', webpage)
  456. if m_variable_name is not None:
  457. config_re = r'%s=({[^}].+?});' % re.escape(m_variable_name.group(1))
  458. else:
  459. config_re = [r' = {config:({.+?}),assets:', r'(?:[abc])=({.+?});']
  460. config = self._search_regex(config_re, webpage, 'info section',
  461. flags=re.DOTALL)
  462. config = json.loads(config)
  463. except Exception as e:
  464. if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
  465. raise ExtractorError('The author has restricted the access to this video, try with the "--referer" option')
  466. if re.search(r'<form[^>]+?id="pw_form"', webpage) is not None:
  467. if '_video_password_verified' in data:
  468. raise ExtractorError('video password verification failed!')
  469. self._verify_video_password(url, video_id, webpage)
  470. return self._real_extract(
  471. smuggle_url(url, {'_video_password_verified': 'verified'}))
  472. else:
  473. raise ExtractorError('Unable to extract info section',
  474. cause=e)
  475. else:
  476. if config.get('view') == 4:
  477. config = self._verify_player_video_password(url, video_id)
  478. def is_rented():
  479. if '>You rented this title.<' in webpage:
  480. return True
  481. if config.get('user', {}).get('purchased'):
  482. return True
  483. label = try_get(
  484. config, lambda x: x['video']['vod']['purchase_options'][0]['label_string'], compat_str)
  485. if label and label.startswith('You rented this'):
  486. return True
  487. return False
  488. if is_rented():
  489. feature_id = config.get('video', {}).get('vod', {}).get('feature_id')
  490. if feature_id and not data.get('force_feature_id', False):
  491. return self.url_result(smuggle_url(
  492. 'https://player.vimeo.com/player/%s' % feature_id,
  493. {'force_feature_id': True}), 'Vimeo')
  494. # Extract video description
  495. video_description = self._html_search_regex(
  496. r'(?s)<div\s+class="[^"]*description[^"]*"[^>]*>(.*?)</div>',
  497. webpage, 'description', default=None)
  498. if not video_description:
  499. video_description = self._html_search_meta(
  500. 'description', webpage, default=None)
  501. if not video_description and mobj.group('pro'):
  502. orig_webpage = self._download_webpage(
  503. orig_url, video_id,
  504. note='Downloading webpage for description',
  505. fatal=False)
  506. if orig_webpage:
  507. video_description = self._html_search_meta(
  508. 'description', orig_webpage, default=None)
  509. if not video_description and not mobj.group('player'):
  510. self._downloader.report_warning('Cannot find video description')
  511. # Extract upload date
  512. video_upload_date = None
  513. mobj = re.search(r'<time[^>]+datetime="([^"]+)"', webpage)
  514. if mobj is not None:
  515. video_upload_date = unified_strdate(mobj.group(1))
  516. try:
  517. view_count = int(self._search_regex(r'UserPlays:(\d+)', webpage, 'view count'))
  518. like_count = int(self._search_regex(r'UserLikes:(\d+)', webpage, 'like count'))
  519. comment_count = int(self._search_regex(r'UserComments:(\d+)', webpage, 'comment count'))
  520. except RegexNotFoundError:
  521. # This info is only available in vimeo.com/{id} urls
  522. view_count = None
  523. like_count = None
  524. comment_count = None
  525. formats = []
  526. download_request = sanitized_Request('https://vimeo.com/%s?action=load_download_config' % video_id, headers={
  527. 'X-Requested-With': 'XMLHttpRequest'})
  528. download_data = self._download_json(download_request, video_id, fatal=False)
  529. if download_data:
  530. source_file = download_data.get('source_file')
  531. if isinstance(source_file, dict):
  532. download_url = source_file.get('download_url')
  533. if download_url and not source_file.get('is_cold') and not source_file.get('is_defrosting'):
  534. source_name = source_file.get('public_name', 'Original')
  535. if self._is_valid_url(download_url, video_id, '%s video' % source_name):
  536. ext = source_file.get('extension', determine_ext(download_url)).lower()
  537. formats.append({
  538. 'url': download_url,
  539. 'ext': ext,
  540. 'width': int_or_none(source_file.get('width')),
  541. 'height': int_or_none(source_file.get('height')),
  542. 'filesize': parse_filesize(source_file.get('size')),
  543. 'format_id': source_name,
  544. 'preference': 1,
  545. })
  546. info_dict = self._parse_config(config, video_id)
  547. formats.extend(info_dict['formats'])
  548. self._vimeo_sort_formats(formats)
  549. info_dict.update({
  550. 'id': video_id,
  551. 'formats': formats,
  552. 'upload_date': video_upload_date,
  553. 'description': video_description,
  554. 'webpage_url': url,
  555. 'view_count': view_count,
  556. 'like_count': like_count,
  557. 'comment_count': comment_count,
  558. })
  559. return info_dict
  560. class VimeoOndemandIE(VimeoBaseInfoExtractor):
  561. IE_NAME = 'vimeo:ondemand'
  562. _VALID_URL = r'https?://(?:www\.)?vimeo\.com/ondemand/(?P<id>[^/?#&]+)'
  563. _TESTS = [{
  564. # ondemand video not available via https://vimeo.com/id
  565. 'url': 'https://vimeo.com/ondemand/20704',
  566. 'md5': 'c424deda8c7f73c1dfb3edd7630e2f35',
  567. 'info_dict': {
  568. 'id': '105442900',
  569. 'ext': 'mp4',
  570. 'title': 'המעבדה - במאי יותם פלדמן',
  571. 'uploader': 'גם סרטים',
  572. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/gumfilms',
  573. 'uploader_id': 'gumfilms',
  574. },
  575. }, {
  576. # requires Referer to be passed along with og:video:url
  577. 'url': 'https://vimeo.com/ondemand/36938/126682985',
  578. 'info_dict': {
  579. 'id': '126682985',
  580. 'ext': 'mp4',
  581. 'title': 'Rävlock, rätt läte på rätt plats',
  582. 'uploader': 'Lindroth & Norin',
  583. 'uploader_url': 're:https?://(?:www\.)?vimeo\.com/user14430847',
  584. 'uploader_id': 'user14430847',
  585. },
  586. 'params': {
  587. 'skip_download': True,
  588. },
  589. }, {
  590. 'url': 'https://vimeo.com/ondemand/nazmaalik',
  591. 'only_matching': True,
  592. }, {
  593. 'url': 'https://vimeo.com/ondemand/141692381',
  594. 'only_matching': True,
  595. }, {
  596. 'url': 'https://vimeo.com/ondemand/thelastcolony/150274832',
  597. 'only_matching': True,
  598. }]
  599. def _real_extract(self, url):
  600. video_id = self._match_id(url)
  601. webpage = self._download_webpage(url, video_id)
  602. return self.url_result(
  603. # Some videos require Referer to be passed along with og:video:url
  604. # similarly to generic vimeo embeds (e.g.
  605. # https://vimeo.com/ondemand/36938/126682985).
  606. VimeoIE._smuggle_referrer(self._og_search_video_url(webpage), url),
  607. VimeoIE.ie_key())
  608. class VimeoChannelIE(VimeoBaseInfoExtractor):
  609. IE_NAME = 'vimeo:channel'
  610. _VALID_URL = r'https://vimeo\.com/channels/(?P<id>[^/?#]+)/?(?:$|[?#])'
  611. _MORE_PAGES_INDICATOR = r'<a.+?rel="next"'
  612. _TITLE = None
  613. _TITLE_RE = r'<link rel="alternate"[^>]+?title="(.*?)"'
  614. _TESTS = [{
  615. 'url': 'https://vimeo.com/channels/tributes',
  616. 'info_dict': {
  617. 'id': 'tributes',
  618. 'title': 'Vimeo Tributes',
  619. },
  620. 'playlist_mincount': 25,
  621. }]
  622. def _page_url(self, base_url, pagenum):
  623. return '%s/videos/page:%d/' % (base_url, pagenum)
  624. def _extract_list_title(self, webpage):
  625. return self._TITLE or self._html_search_regex(self._TITLE_RE, webpage, 'list title')
  626. def _login_list_password(self, page_url, list_id, webpage):
  627. login_form = self._search_regex(
  628. r'(?s)<form[^>]+?id="pw_form"(.*?)</form>',
  629. webpage, 'login form', default=None)
  630. if not login_form:
  631. return webpage
  632. password = self._downloader.params.get('videopassword')
  633. if password is None:
  634. raise ExtractorError('This album is protected by a password, use the --video-password option', expected=True)
  635. fields = self._hidden_inputs(login_form)
  636. token, vuid = self._extract_xsrft_and_vuid(webpage)
  637. fields['token'] = token
  638. fields['password'] = password
  639. post = urlencode_postdata(fields)
  640. password_path = self._search_regex(
  641. r'action="([^"]+)"', login_form, 'password URL')
  642. password_url = compat_urlparse.urljoin(page_url, password_path)
  643. password_request = sanitized_Request(password_url, post)
  644. password_request.add_header('Content-type', 'application/x-www-form-urlencoded')
  645. self._set_vimeo_cookie('vuid', vuid)
  646. self._set_vimeo_cookie('xsrft', token)
  647. return self._download_webpage(
  648. password_request, list_id,
  649. 'Verifying the password', 'Wrong password')
  650. def _title_and_entries(self, list_id, base_url):
  651. for pagenum in itertools.count(1):
  652. page_url = self._page_url(base_url, pagenum)
  653. webpage = self._download_webpage(
  654. page_url, list_id,
  655. 'Downloading page %s' % pagenum)
  656. if pagenum == 1:
  657. webpage = self._login_list_password(page_url, list_id, webpage)
  658. yield self._extract_list_title(webpage)
  659. # Try extracting href first since not all videos are available via
  660. # short https://vimeo.com/id URL (e.g. https://vimeo.com/channels/tributes/6213729)
  661. clips = re.findall(
  662. r'id="clip_(\d+)"[^>]*>\s*<a[^>]+href="(/(?:[^/]+/)*\1)', webpage)
  663. if clips:
  664. for video_id, video_url in clips:
  665. yield self.url_result(
  666. compat_urlparse.urljoin(base_url, video_url),
  667. VimeoIE.ie_key(), video_id=video_id)
  668. # More relaxed fallback
  669. else:
  670. for video_id in re.findall(r'id=["\']clip_(\d+)', webpage):
  671. yield self.url_result(
  672. 'https://vimeo.com/%s' % video_id,
  673. VimeoIE.ie_key(), video_id=video_id)
  674. if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
  675. break
  676. def _extract_videos(self, list_id, base_url):
  677. title_and_entries = self._title_and_entries(list_id, base_url)
  678. list_title = next(title_and_entries)
  679. return self.playlist_result(title_and_entries, list_id, list_title)
  680. def _real_extract(self, url):
  681. mobj = re.match(self._VALID_URL, url)
  682. channel_id = mobj.group('id')
  683. return self._extract_videos(channel_id, 'https://vimeo.com/channels/%s' % channel_id)
  684. class VimeoUserIE(VimeoChannelIE):
  685. IE_NAME = 'vimeo:user'
  686. _VALID_URL = r'https://vimeo\.com/(?!(?:[0-9]+|watchlater)(?:$|[?#/]))(?P<name>[^/]+)(?:/videos|[#?]|$)'
  687. _TITLE_RE = r'<a[^>]+?class="user">([^<>]+?)</a>'
  688. _TESTS = [{
  689. 'url': 'https://vimeo.com/nkistudio/videos',
  690. 'info_dict': {
  691. 'title': 'Nki',
  692. 'id': 'nkistudio',
  693. },
  694. 'playlist_mincount': 66,
  695. }]
  696. def _real_extract(self, url):
  697. mobj = re.match(self._VALID_URL, url)
  698. name = mobj.group('name')
  699. return self._extract_videos(name, 'https://vimeo.com/%s' % name)
  700. class VimeoAlbumIE(VimeoChannelIE):
  701. IE_NAME = 'vimeo:album'
  702. _VALID_URL = r'https://vimeo\.com/album/(?P<id>\d+)(?:$|[?#]|/(?!video))'
  703. _TITLE_RE = r'<header id="page_header">\n\s*<h1>(.*?)</h1>'
  704. _TESTS = [{
  705. 'url': 'https://vimeo.com/album/2632481',
  706. 'info_dict': {
  707. 'id': '2632481',
  708. 'title': 'Staff Favorites: November 2013',
  709. },
  710. 'playlist_mincount': 13,
  711. }, {
  712. 'note': 'Password-protected album',
  713. 'url': 'https://vimeo.com/album/3253534',
  714. 'info_dict': {
  715. 'title': 'test',
  716. 'id': '3253534',
  717. },
  718. 'playlist_count': 1,
  719. 'params': {
  720. 'videopassword': 'youtube-dl',
  721. }
  722. }, {
  723. 'url': 'https://vimeo.com/album/2632481/sort:plays/format:thumbnail',
  724. 'only_matching': True,
  725. }, {
  726. # TODO: respect page number
  727. 'url': 'https://vimeo.com/album/2632481/page:2/sort:plays/format:thumbnail',
  728. 'only_matching': True,
  729. }]
  730. def _page_url(self, base_url, pagenum):
  731. return '%s/page:%d/' % (base_url, pagenum)
  732. def _real_extract(self, url):
  733. album_id = self._match_id(url)
  734. return self._extract_videos(album_id, 'https://vimeo.com/album/%s' % album_id)
  735. class VimeoGroupsIE(VimeoAlbumIE):
  736. IE_NAME = 'vimeo:group'
  737. _VALID_URL = r'https://vimeo\.com/groups/(?P<name>[^/]+)(?:/(?!videos?/\d+)|$)'
  738. _TESTS = [{
  739. 'url': 'https://vimeo.com/groups/rolexawards',
  740. 'info_dict': {
  741. 'id': 'rolexawards',
  742. 'title': 'Rolex Awards for Enterprise',
  743. },
  744. 'playlist_mincount': 73,
  745. }]
  746. def _extract_list_title(self, webpage):
  747. return self._og_search_title(webpage)
  748. def _real_extract(self, url):
  749. mobj = re.match(self._VALID_URL, url)
  750. name = mobj.group('name')
  751. return self._extract_videos(name, 'https://vimeo.com/groups/%s' % name)
  752. class VimeoReviewIE(VimeoBaseInfoExtractor):
  753. IE_NAME = 'vimeo:review'
  754. IE_DESC = 'Review pages on vimeo'
  755. _VALID_URL = r'https://vimeo\.com/[^/]+/review/(?P<id>[^/]+)'
  756. _TESTS = [{
  757. 'url': 'https://vimeo.com/user21297594/review/75524534/3c257a1b5d',
  758. 'md5': 'c507a72f780cacc12b2248bb4006d253',
  759. 'info_dict': {
  760. 'id': '75524534',
  761. 'ext': 'mp4',
  762. 'title': "DICK HARDWICK 'Comedian'",
  763. 'uploader': 'Richard Hardwick',
  764. 'uploader_id': 'user21297594',
  765. }
  766. }, {
  767. 'note': 'video player needs Referer',
  768. 'url': 'https://vimeo.com/user22258446/review/91613211/13f927e053',
  769. 'md5': '6295fdab8f4bf6a002d058b2c6dce276',
  770. 'info_dict': {
  771. 'id': '91613211',
  772. 'ext': 'mp4',
  773. 'title': 're:(?i)^Death by dogma versus assembling agile . Sander Hoogendoorn',
  774. 'uploader': 'DevWeek Events',
  775. 'duration': 2773,
  776. 'thumbnail': 're:^https?://.*\.jpg$',
  777. 'uploader_id': 'user22258446',
  778. }
  779. }, {
  780. 'note': 'Password protected',
  781. 'url': 'https://vimeo.com/user37284429/review/138823582/c4d865efde',
  782. 'info_dict': {
  783. 'id': '138823582',
  784. 'ext': 'mp4',
  785. 'title': 'EFFICIENT PICKUP MASTERCLASS MODULE 1',
  786. 'uploader': 'TMB',
  787. 'uploader_id': 'user37284429',
  788. },
  789. 'params': {
  790. 'videopassword': 'holygrail',
  791. },
  792. 'skip': 'video gone',
  793. }]
  794. def _real_initialize(self):
  795. self._login()
  796. def _get_config_url(self, webpage_url, video_id, video_password_verified=False):
  797. webpage = self._download_webpage(webpage_url, video_id)
  798. data = self._parse_json(self._search_regex(
  799. r'window\s*=\s*_extend\(window,\s*({.+?})\);', webpage, 'data',
  800. default=NO_DEFAULT if video_password_verified else '{}'), video_id)
  801. config_url = data.get('vimeo_esi', {}).get('config', {}).get('configUrl')
  802. if config_url is None:
  803. self._verify_video_password(webpage_url, video_id, webpage)
  804. config_url = self._get_config_url(
  805. webpage_url, video_id, video_password_verified=True)
  806. return config_url
  807. def _real_extract(self, url):
  808. video_id = self._match_id(url)
  809. config_url = self._get_config_url(url, video_id)
  810. config = self._download_json(config_url, video_id)
  811. info_dict = self._parse_config(config, video_id)
  812. self._vimeo_sort_formats(info_dict['formats'])
  813. info_dict['id'] = video_id
  814. return info_dict
  815. class VimeoWatchLaterIE(VimeoChannelIE):
  816. IE_NAME = 'vimeo:watchlater'
  817. IE_DESC = 'Vimeo watch later list, "vimeowatchlater" keyword (requires authentication)'
  818. _VALID_URL = r'https://vimeo\.com/(?:home/)?watchlater|:vimeowatchlater'
  819. _TITLE = 'Watch Later'
  820. _LOGIN_REQUIRED = True
  821. _TESTS = [{
  822. 'url': 'https://vimeo.com/watchlater',
  823. 'only_matching': True,
  824. }]
  825. def _real_initialize(self):
  826. self._login()
  827. def _page_url(self, base_url, pagenum):
  828. url = '%s/page:%d/' % (base_url, pagenum)
  829. request = sanitized_Request(url)
  830. # Set the header to get a partial html page with the ids,
  831. # the normal page doesn't contain them.
  832. request.add_header('X-Requested-With', 'XMLHttpRequest')
  833. return request
  834. def _real_extract(self, url):
  835. return self._extract_videos('watchlater', 'https://vimeo.com/watchlater')
  836. class VimeoLikesIE(InfoExtractor):
  837. _VALID_URL = r'https://(?:www\.)?vimeo\.com/user(?P<id>[0-9]+)/likes/?(?:$|[?#]|sort:)'
  838. IE_NAME = 'vimeo:likes'
  839. IE_DESC = 'Vimeo user likes'
  840. _TEST = {
  841. 'url': 'https://vimeo.com/user755559/likes/',
  842. 'playlist_mincount': 293,
  843. 'info_dict': {
  844. 'id': 'user755559_likes',
  845. 'description': 'See all the videos urza likes',
  846. 'title': 'Videos urza likes',
  847. },
  848. }
  849. def _real_extract(self, url):
  850. user_id = self._match_id(url)
  851. webpage = self._download_webpage(url, user_id)
  852. page_count = self._int(
  853. self._search_regex(
  854. r'''(?x)<li><a\s+href="[^"]+"\s+data-page="([0-9]+)">
  855. .*?</a></li>\s*<li\s+class="pagination_next">
  856. ''', webpage, 'page count'),
  857. 'page count', fatal=True)
  858. PAGE_SIZE = 12
  859. title = self._html_search_regex(
  860. r'(?s)<h1>(.+?)</h1>', webpage, 'title', fatal=False)
  861. description = self._html_search_meta('description', webpage)
  862. def _get_page(idx):
  863. page_url = 'https://vimeo.com/user%s/likes/page:%d/sort:date' % (
  864. user_id, idx + 1)
  865. webpage = self._download_webpage(
  866. page_url, user_id,
  867. note='Downloading page %d/%d' % (idx + 1, page_count))
  868. video_list = self._search_regex(
  869. r'(?s)<ol class="js-browse_list[^"]+"[^>]*>(.*?)</ol>',
  870. webpage, 'video content')
  871. paths = re.findall(
  872. r'<li[^>]*>\s*<a\s+href="([^"]+)"', video_list)
  873. for path in paths:
  874. yield {
  875. '_type': 'url',
  876. 'url': compat_urlparse.urljoin(page_url, path),
  877. }
  878. pl = InAdvancePagedList(_get_page, page_count, PAGE_SIZE)
  879. return {
  880. '_type': 'playlist',
  881. 'id': 'user%s_likes' % user_id,
  882. 'title': title,
  883. 'description': description,
  884. 'entries': pl,
  885. }