vimeo.py 34 KB

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