vimeo.py 31 KB

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