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