vimeo.py 32 KB

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