2 from __future__ import unicode_literals
8 from .common import InfoExtractor
9 from .subtitles import SubtitlesInfoExtractor
13 compat_urllib_request,
15 get_element_by_attribute,
24 class VimeoBaseInfoExtractor(InfoExtractor):
25 _NETRC_MACHINE = 'vimeo'
26 _LOGIN_REQUIRED = False
29 (username, password) = self._get_login_info()
31 if self._LOGIN_REQUIRED:
32 raise ExtractorError(u'No login info available, needed for using %s.' % self.IE_NAME, expected=True)
35 login_url = 'https://vimeo.com/log_in'
36 webpage = self._download_webpage(login_url, None, False)
37 token = self._search_regex(r'xsrft: \'(.*?)\'', webpage, 'login token')
38 data = urlencode_postdata({
45 login_request = compat_urllib_request.Request(login_url, data)
46 login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
47 login_request.add_header('Cookie', 'xsrft=%s' % token)
48 self._download_webpage(login_request, None, False, 'Wrong login info')
51 class VimeoIE(VimeoBaseInfoExtractor, SubtitlesInfoExtractor):
52 """Information extractor for vimeo.com."""
54 # _VALID_URL matches Vimeo URLs
56 (?P<proto>(?:https?:)?//)?
57 (?:(?:www|(?P<player>player))\.)?
58 vimeo(?P<pro>pro)?\.com/
60 (?:(?:play_redirect_hls|moogaloop\.swf)\?clip_id=)?
63 /?(?:[?&].*)?(?:[#].*)?$'''
67 'url': 'http://vimeo.com/56015672#at=0',
68 'md5': '8879b6cc097e987f02484baf890129e5',
72 "upload_date": "20121220",
73 "description": "This is a test case for youtube-dl.\nFor more information, see github.com/rg3/youtube-dl\nTest chars: \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
74 "uploader_id": "user7108434",
75 "uploader": "Filippo Valsorda",
76 "title": "youtube-dl test video - \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
80 'url': 'http://vimeopro.com/openstreetmapus/state-of-the-map-us-2013/video/68093876',
81 'file': '68093876.mp4',
82 'md5': '3b5ca6aa22b60dfeeadf50b72e44ed82',
83 'note': 'Vimeo Pro video (#1197)',
85 'uploader_id': 'openstreetmapus',
86 'uploader': 'OpenStreetMap US',
87 'title': 'Andy Allan - Putting the Carto into OpenStreetMap Cartography',
91 'url': 'http://player.vimeo.com/video/54469442',
92 'file': '54469442.mp4',
93 'md5': '619b811a4417aa4abe78dc653becf511',
94 'note': 'Videos that embed the url in the player page',
96 'title': 'Kathy Sierra: Building the minimum Badass User, Business of Software',
97 'uploader': 'The BLN & Business of Software',
98 'uploader_id': 'theblnbusinessofsoftware',
102 'url': 'http://vimeo.com/68375962',
103 'file': '68375962.mp4',
104 'md5': 'aaf896bdb7ddd6476df50007a0ac0ae7',
105 'note': 'Video protected with password',
107 'title': 'youtube-dl password protected test video',
108 'upload_date': '20130614',
109 'uploader_id': 'user18948128',
110 'uploader': 'Jaime Marquínez Ferrándiz',
113 'videopassword': 'youtube-dl',
117 'url': 'http://vimeo.com/76979871',
118 'md5': '3363dd6ffebe3784d56f4132317fd446',
119 'note': 'Video with subtitles',
123 'title': 'The New Vimeo Player (You Know, For Videos)',
124 'description': 'md5:2ec900bf97c3f389378a96aee11260ea',
125 'upload_date': '20131015',
126 'uploader_id': 'staff',
127 'uploader': 'Vimeo Staff',
133 def suitable(cls, url):
134 if VimeoChannelIE.suitable(url):
135 # Otherwise channel urls like http://vimeo.com/channels/31259 would
139 return super(VimeoIE, cls).suitable(url)
141 def _verify_video_password(self, url, video_id, webpage):
142 password = self._downloader.params.get('videopassword', None)
144 raise ExtractorError('This video is protected by a password, use the --video-password option')
145 token = self._search_regex(r'xsrft: \'(.*?)\'', webpage, 'login token')
146 data = compat_urllib_parse.urlencode({'password': password,
148 # I didn't manage to use the password with https
149 if url.startswith('https'):
150 pass_url = url.replace('https','http')
153 password_request = compat_urllib_request.Request(pass_url+'/password', data)
154 password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
155 password_request.add_header('Cookie', 'xsrft=%s' % token)
156 self._download_webpage(password_request, video_id,
157 'Verifying the password',
160 def _verify_player_video_password(self, url, video_id):
161 password = self._downloader.params.get('videopassword', None)
163 raise ExtractorError('This video is protected by a password, use the --video-password option')
164 data = compat_urllib_parse.urlencode({'password': password})
165 pass_url = url + '/check-password'
166 password_request = compat_urllib_request.Request(pass_url, data)
167 password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
168 return self._download_json(
169 password_request, video_id,
170 'Verifying the password',
173 def _real_initialize(self):
176 def _real_extract(self, url):
177 url, data = unsmuggle_url(url)
178 headers = std_headers
180 headers = headers.copy()
183 # Extract ID from URL
184 mobj = re.match(self._VALID_URL, url)
185 video_id = mobj.group('id')
186 if mobj.group('pro') or mobj.group('player'):
187 url = 'http://player.vimeo.com/video/' + video_id
189 url = 'https://vimeo.com/' + video_id
191 # Retrieve video webpage to extract further information
192 request = compat_urllib_request.Request(url, None, headers)
194 webpage = self._download_webpage(request, video_id)
195 except ExtractorError as ee:
196 if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
197 errmsg = ee.cause.read()
198 if b'Because of its privacy settings, this video cannot be played here' in errmsg:
199 raise ExtractorError(
200 'Cannot download embed-only video without embedding '
201 'URL. Please call youtube-dl with the URL of the page '
202 'that embeds this video.',
206 # Now we begin extracting as much information as we can from what we
207 # retrieved. First we extract the information common to all extractors,
208 # and latter we extract those that are Vimeo specific.
209 self.report_extraction(video_id)
211 # Extract the config JSON
214 config_url = self._html_search_regex(
215 r' data-config-url="(.+?)"', webpage, 'config URL')
216 config_json = self._download_webpage(config_url, video_id)
217 config = json.loads(config_json)
218 except RegexNotFoundError:
219 # For pro videos or player.vimeo.com urls
220 # We try to find out to which variable is assigned the config dic
221 m_variable_name = re.search('(\w)\.video\.id', webpage)
222 if m_variable_name is not None:
223 config_re = r'%s=({.+?});' % re.escape(m_variable_name.group(1))
225 config_re = [r' = {config:({.+?}),assets:', r'(?:[abc])=({.+?});']
226 config = self._search_regex(config_re, webpage, 'info section',
228 config = json.loads(config)
229 except Exception as e:
230 if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
231 raise ExtractorError('The author has restricted the access to this video, try with the "--referer" option')
233 if re.search('<form[^>]+?id="pw_form"', webpage) is not None:
234 self._verify_video_password(url, video_id, webpage)
235 return self._real_extract(url)
237 raise ExtractorError('Unable to extract info section',
240 if config.get('view') == 4:
241 config = self._verify_player_video_password(url, video_id)
244 video_title = config["video"]["title"]
246 # Extract uploader and uploader_id
247 video_uploader = config["video"]["owner"]["name"]
248 video_uploader_id = config["video"]["owner"]["url"].split('/')[-1] if config["video"]["owner"]["url"] else None
250 # Extract video thumbnail
251 video_thumbnail = config["video"].get("thumbnail")
252 if video_thumbnail is None:
253 video_thumbs = config["video"].get("thumbs")
254 if video_thumbs and isinstance(video_thumbs, dict):
255 _, video_thumbnail = sorted((int(width), t_url) for (width, t_url) in video_thumbs.items())[-1]
257 # Extract video description
258 video_description = None
260 video_description = get_element_by_attribute("class", "description_wrapper", webpage)
261 if video_description:
262 video_description = clean_html(video_description)
263 except AssertionError as err:
264 # On some pages like (http://player.vimeo.com/video/54469442) the
265 # html tags are not closed, python 2.6 cannot handle it
266 if err.args[0] == 'we should not get here!':
271 # Extract upload date
272 video_upload_date = None
273 mobj = re.search(r'<meta itemprop="dateCreated" content="(\d{4})-(\d{2})-(\d{2})T', webpage)
275 video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
278 view_count = int(self._search_regex(r'UserPlays:(\d+)', webpage, 'view count'))
279 like_count = int(self._search_regex(r'UserLikes:(\d+)', webpage, 'like count'))
280 comment_count = int(self._search_regex(r'UserComments:(\d+)', webpage, 'comment count'))
281 except RegexNotFoundError:
282 # This info is only available in vimeo.com/{id} urls
287 # Vimeo specific: extract request signature and timestamp
288 sig = config['request']['signature']
289 timestamp = config['request']['timestamp']
291 # Vimeo specific: extract video codec and quality information
292 # First consider quality, then codecs, then take everything
293 codecs = [('vp6', 'flv'), ('vp8', 'flv'), ('h264', 'mp4')]
294 files = {'hd': [], 'sd': [], 'other': []}
295 config_files = config["video"].get("files") or config["request"].get("files")
296 for codec_name, codec_extension in codecs:
297 for quality in config_files.get(codec_name, []):
298 format_id = '-'.join((codec_name, quality)).lower()
299 key = quality if quality in files else 'other'
301 if isinstance(config_files[codec_name], dict):
302 file_info = config_files[codec_name][quality]
303 video_url = file_info.get('url')
306 if video_url is None:
307 video_url = "http://player.vimeo.com/play_redirect?clip_id=%s&sig=%s&time=%s&quality=%s&codecs=%s&type=moogaloop_local&embed_location=" \
308 %(video_id, sig, timestamp, quality, codec_name.upper())
311 'ext': codec_extension,
313 'format_id': format_id,
314 'width': file_info.get('width'),
315 'height': file_info.get('height'),
318 for key in ('other', 'sd', 'hd'):
319 formats += files[key]
320 if len(formats) == 0:
321 raise ExtractorError('No known codec found')
324 text_tracks = config['request'].get('text_tracks')
326 for tt in text_tracks:
327 subtitles[tt['lang']] = 'http://vimeo.com' + tt['url']
329 video_subtitles = self.extract_subtitles(video_id, subtitles)
330 if self._downloader.params.get('listsubtitles', False):
331 self._list_available_subtitles(video_id, subtitles)
336 'uploader': video_uploader,
337 'uploader_id': video_uploader_id,
338 'upload_date': video_upload_date,
339 'title': video_title,
340 'thumbnail': video_thumbnail,
341 'description': video_description,
344 'view_count': view_count,
345 'like_count': like_count,
346 'comment_count': comment_count,
347 'subtitles': video_subtitles,
351 class VimeoChannelIE(InfoExtractor):
352 IE_NAME = 'vimeo:channel'
353 _VALID_URL = r'(?:https?://)?vimeo\.com/channels/(?P<id>[^/]+)/?(\?.*)?$'
354 _MORE_PAGES_INDICATOR = r'<a.+?rel="next"'
355 _TITLE_RE = r'<link rel="alternate"[^>]+?title="(.*?)"'
357 def _page_url(self, base_url, pagenum):
358 return '%s/videos/page:%d/' % (base_url, pagenum)
360 def _extract_list_title(self, webpage):
361 return self._html_search_regex(self._TITLE_RE, webpage, 'list title')
363 def _extract_videos(self, list_id, base_url):
365 for pagenum in itertools.count(1):
366 webpage = self._download_webpage(
367 self._page_url(base_url, pagenum) ,list_id,
368 'Downloading page %s' % pagenum)
369 video_ids.extend(re.findall(r'id="clip_(\d+?)"', webpage))
370 if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
373 entries = [self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo')
374 for video_id in video_ids]
375 return {'_type': 'playlist',
377 'title': self._extract_list_title(webpage),
381 def _real_extract(self, url):
382 mobj = re.match(self._VALID_URL, url)
383 channel_id = mobj.group('id')
384 return self._extract_videos(channel_id, 'http://vimeo.com/channels/%s' % channel_id)
387 class VimeoUserIE(VimeoChannelIE):
388 IE_NAME = 'vimeo:user'
389 _VALID_URL = r'(?:https?://)?vimeo\.com/(?P<name>[^/]+)(?:/videos|[#?]|$)'
390 _TITLE_RE = r'<a[^>]+?class="user">([^<>]+?)</a>'
393 def suitable(cls, url):
394 if VimeoChannelIE.suitable(url) or VimeoIE.suitable(url) or VimeoAlbumIE.suitable(url) or VimeoGroupsIE.suitable(url):
396 return super(VimeoUserIE, cls).suitable(url)
398 def _real_extract(self, url):
399 mobj = re.match(self._VALID_URL, url)
400 name = mobj.group('name')
401 return self._extract_videos(name, 'http://vimeo.com/%s' % name)
404 class VimeoAlbumIE(VimeoChannelIE):
405 IE_NAME = 'vimeo:album'
406 _VALID_URL = r'(?:https?://)?vimeo\.com/album/(?P<id>\d+)'
407 _TITLE_RE = r'<header id="page_header">\n\s*<h1>(.*?)</h1>'
409 def _page_url(self, base_url, pagenum):
410 return '%s/page:%d/' % (base_url, pagenum)
412 def _real_extract(self, url):
413 mobj = re.match(self._VALID_URL, url)
414 album_id = mobj.group('id')
415 return self._extract_videos(album_id, 'http://vimeo.com/album/%s' % album_id)
418 class VimeoGroupsIE(VimeoAlbumIE):
419 IE_NAME = 'vimeo:group'
420 _VALID_URL = r'(?:https?://)?vimeo\.com/groups/(?P<name>[^/]+)'
422 def _extract_list_title(self, webpage):
423 return self._og_search_title(webpage)
425 def _real_extract(self, url):
426 mobj = re.match(self._VALID_URL, url)
427 name = mobj.group('name')
428 return self._extract_videos(name, 'http://vimeo.com/groups/%s' % name)
431 class VimeoReviewIE(InfoExtractor):
432 IE_NAME = 'vimeo:review'
433 IE_DESC = 'Review pages on vimeo'
434 _VALID_URL = r'(?:https?://)?vimeo\.com/[^/]+/review/(?P<id>[^/]+)'
436 'url': 'https://vimeo.com/user21297594/review/75524534/3c257a1b5d',
437 'file': '75524534.mp4',
438 'md5': 'c507a72f780cacc12b2248bb4006d253',
440 'title': "DICK HARDWICK 'Comedian'",
441 'uploader': 'Richard Hardwick',
445 def _real_extract(self, url):
446 mobj = re.match(self._VALID_URL, url)
447 video_id = mobj.group('id')
448 player_url = 'https://player.vimeo.com/player/' + video_id
449 return self.url_result(player_url, 'Vimeo', video_id)
452 class VimeoWatchLaterIE(VimeoBaseInfoExtractor, VimeoChannelIE):
453 IE_NAME = 'vimeo:watchlater'
454 IE_DESC = 'Vimeo watch later list, "vimeowatchlater" keyword (requires authentication)'
455 _VALID_URL = r'https?://vimeo\.com/home/watchlater|:vimeowatchlater'
456 _LOGIN_REQUIRED = True
457 _TITLE_RE = r'href="/home/watchlater".*?>(.*?)<'
459 def _real_initialize(self):
462 def _page_url(self, base_url, pagenum):
463 url = '%s/page:%d/' % (base_url, pagenum)
464 request = compat_urllib_request.Request(url)
465 # Set the header to get a partial html page with the ids,
466 # the normal page doesn't contain them.
467 request.add_header('X-Requested-With', 'XMLHttpRequest')
470 def _real_extract(self, url):
471 return self._extract_videos('watchlater', 'https://vimeo.com/home/watchlater')