6 from .common import InfoExtractor
12 get_element_by_attribute,
20 class VimeoIE(InfoExtractor):
21 """Information extractor for vimeo.com."""
23 # _VALID_URL matches Vimeo URLs
26 (?:(?:www|(?P<player>player))\.)?
27 vimeo(?P<pro>pro)?\.com/
29 (?:(?:play_redirect_hls|moogaloop\.swf)\?clip_id=)?
32 /?(?:[?&].*)?(?:[#].*)?$'''
33 _NETRC_MACHINE = 'vimeo'
37 u'url': u'http://vimeo.com/56015672#at=0',
38 u'file': u'56015672.mp4',
39 u'md5': u'8879b6cc097e987f02484baf890129e5',
41 u"upload_date": u"20121220",
42 u"description": u"This is a test case for youtube-dl.\nFor more information, see github.com/rg3/youtube-dl\nTest chars: \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
43 u"uploader_id": u"user7108434",
44 u"uploader": u"Filippo Valsorda",
45 u"title": u"youtube-dl test video - \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550",
49 u'url': u'http://vimeopro.com/openstreetmapus/state-of-the-map-us-2013/video/68093876',
50 u'file': u'68093876.mp4',
51 u'md5': u'3b5ca6aa22b60dfeeadf50b72e44ed82',
52 u'note': u'Vimeo Pro video (#1197)',
54 u'uploader_id': u'openstreetmapus',
55 u'uploader': u'OpenStreetMap US',
56 u'title': u'Andy Allan - Putting the Carto into OpenStreetMap Cartography',
60 u'url': u'http://player.vimeo.com/video/54469442',
61 u'file': u'54469442.mp4',
62 u'md5': u'619b811a4417aa4abe78dc653becf511',
63 u'note': u'Videos that embed the url in the player page',
65 u'title': u'Kathy Sierra: Building the minimum Badass User, Business of Software',
66 u'uploader': u'The BLN & Business of Software',
70 u'url': u'http://vimeo.com/68375962',
71 u'file': u'68375962.mp4',
72 u'md5': u'aaf896bdb7ddd6476df50007a0ac0ae7',
73 u'note': u'Video protected with password',
75 u'title': u'youtube-dl password protected test video',
76 u'upload_date': u'20130614',
77 u'uploader_id': u'user18948128',
78 u'uploader': u'Jaime Marquínez Ferrándiz',
81 u'videopassword': u'youtube-dl',
87 (username, password) = self._get_login_info()
91 login_url = 'https://vimeo.com/log_in'
92 webpage = self._download_webpage(login_url, None, False)
93 token = re.search(r'xsrft: \'(.*?)\'', webpage).group(1)
94 data = compat_urllib_parse.urlencode({'email': username,
100 login_request = compat_urllib_request.Request(login_url, data)
101 login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
102 login_request.add_header('Cookie', 'xsrft=%s' % token)
103 self._download_webpage(login_request, None, False, u'Wrong login info')
105 def _verify_video_password(self, url, video_id, webpage):
106 password = self._downloader.params.get('videopassword', None)
108 raise ExtractorError(u'This video is protected by a password, use the --video-password option')
109 token = re.search(r'xsrft: \'(.*?)\'', webpage).group(1)
110 data = compat_urllib_parse.urlencode({'password': password,
112 # I didn't manage to use the password with https
113 if url.startswith('https'):
114 pass_url = url.replace('https','http')
117 password_request = compat_urllib_request.Request(pass_url+'/password', data)
118 password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
119 password_request.add_header('Cookie', 'xsrft=%s' % token)
120 self._download_webpage(password_request, video_id,
121 u'Verifying the password',
124 def _real_initialize(self):
127 def _real_extract(self, url):
128 url, data = unsmuggle_url(url)
129 headers = std_headers
131 headers = headers.copy()
134 # Extract ID from URL
135 mobj = re.match(self._VALID_URL, url)
137 raise ExtractorError(u'Invalid URL: %s' % url)
139 video_id = mobj.group('id')
140 if mobj.group('pro') or mobj.group('player'):
141 url = 'http://player.vimeo.com/video/' + video_id
143 url = 'https://vimeo.com/' + video_id
145 # Retrieve video webpage to extract further information
146 request = compat_urllib_request.Request(url, None, headers)
147 webpage = self._download_webpage(request, video_id)
149 # Now we begin extracting as much information as we can from what we
150 # retrieved. First we extract the information common to all extractors,
151 # and latter we extract those that are Vimeo specific.
152 self.report_extraction(video_id)
154 # Extract the config JSON
157 config_url = self._html_search_regex(
158 r' data-config-url="(.+?)"', webpage, u'config URL')
159 config_json = self._download_webpage(config_url, video_id)
160 config = json.loads(config_json)
161 except RegexNotFoundError:
162 # For pro videos or player.vimeo.com urls
163 # We try to find out to which variable is assigned the config dic
164 m_variable_name = re.search('(\w)\.video\.id', webpage)
165 if m_variable_name is not None:
166 config_re = r'%s=({.+?});' % re.escape(m_variable_name.group(1))
168 config_re = [r' = {config:({.+?}),assets:', r'(?:[abc])=({.+?});']
169 config = self._search_regex(config_re, webpage, u'info section',
171 config = json.loads(config)
172 except Exception as e:
173 if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
174 raise ExtractorError(u'The author has restricted the access to this video, try with the "--referer" option')
176 if re.search('<form[^>]+?id="pw_form"', webpage) is not None:
177 self._verify_video_password(url, video_id, webpage)
178 return self._real_extract(url)
180 raise ExtractorError(u'Unable to extract info section',
183 if config.get('view') == 4:
184 self._verify_video_password(url, video_id, webpage)
185 return self._real_extract(url)
188 video_title = config["video"]["title"]
190 # Extract uploader and uploader_id
191 video_uploader = config["video"]["owner"]["name"]
192 video_uploader_id = config["video"]["owner"]["url"].split('/')[-1] if config["video"]["owner"]["url"] else None
194 # Extract video thumbnail
195 video_thumbnail = config["video"].get("thumbnail")
196 if video_thumbnail is None:
197 _, video_thumbnail = sorted((int(width), t_url) for (width, t_url) in config["video"]["thumbs"].items())[-1]
199 # Extract video description
200 video_description = None
202 video_description = get_element_by_attribute("itemprop", "description", webpage)
203 if video_description: video_description = clean_html(video_description)
204 except AssertionError as err:
205 # On some pages like (http://player.vimeo.com/video/54469442) the
206 # html tags are not closed, python 2.6 cannot handle it
207 if err.args[0] == 'we should not get here!':
212 # Extract upload date
213 video_upload_date = None
214 mobj = re.search(r'<meta itemprop="dateCreated" content="(\d{4})-(\d{2})-(\d{2})T', webpage)
216 video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
219 view_count = int(self._search_regex(r'UserPlays:(\d+)', webpage, u'view count'))
220 like_count = int(self._search_regex(r'UserLikes:(\d+)', webpage, u'like count'))
221 comment_count = int(self._search_regex(r'UserComments:(\d+)', webpage, u'comment count'))
222 except RegexNotFoundError:
223 # This info is only available in vimeo.com/{id} urls
228 # Vimeo specific: extract request signature and timestamp
229 sig = config['request']['signature']
230 timestamp = config['request']['timestamp']
232 # Vimeo specific: extract video codec and quality information
233 # First consider quality, then codecs, then take everything
234 codecs = [('vp6', 'flv'), ('vp8', 'flv'), ('h264', 'mp4')]
235 files = {'hd': [], 'sd': [], 'other': []}
236 config_files = config["video"].get("files") or config["request"].get("files")
237 for codec_name, codec_extension in codecs:
238 for quality in config_files.get(codec_name, []):
239 format_id = '-'.join((codec_name, quality)).lower()
240 key = quality if quality in files else 'other'
242 if isinstance(config_files[codec_name], dict):
243 file_info = config_files[codec_name][quality]
244 video_url = file_info.get('url')
247 if video_url is None:
248 video_url = "http://player.vimeo.com/play_redirect?clip_id=%s&sig=%s&time=%s&quality=%s&codecs=%s&type=moogaloop_local&embed_location=" \
249 %(video_id, sig, timestamp, quality, codec_name.upper())
252 'ext': codec_extension,
254 'format_id': format_id,
255 'width': file_info.get('width'),
256 'height': file_info.get('height'),
259 for key in ('other', 'sd', 'hd'):
260 formats += files[key]
261 if len(formats) == 0:
262 raise ExtractorError(u'No known codec found')
266 'uploader': video_uploader,
267 'uploader_id': video_uploader_id,
268 'upload_date': video_upload_date,
269 'title': video_title,
270 'thumbnail': video_thumbnail,
271 'description': video_description,
274 'view_count': view_count,
275 'like_count': like_count,
276 'comment_count': comment_count,
280 class VimeoChannelIE(InfoExtractor):
281 IE_NAME = u'vimeo:channel'
282 _VALID_URL = r'(?:https?://)?vimeo.\com/channels/(?P<id>[^/]+)'
283 _MORE_PAGES_INDICATOR = r'<a.+?rel="next"'
284 _TITLE_RE = r'<link rel="alternate"[^>]+?title="(.*?)"'
286 def _page_url(self, base_url, pagenum):
287 return '%s/videos/page:%d/' % (base_url, pagenum)
289 def _extract_list_title(self, webpage):
290 return self._html_search_regex(self._TITLE_RE, webpage, u'list title')
292 def _extract_videos(self, list_id, base_url):
294 for pagenum in itertools.count(1):
295 webpage = self._download_webpage(
296 self._page_url(base_url, pagenum) ,list_id,
297 u'Downloading page %s' % pagenum)
298 video_ids.extend(re.findall(r'id="clip_(\d+?)"', webpage))
299 if re.search(self._MORE_PAGES_INDICATOR, webpage, re.DOTALL) is None:
302 entries = [self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo')
303 for video_id in video_ids]
304 return {'_type': 'playlist',
306 'title': self._extract_list_title(webpage),
310 def _real_extract(self, url):
311 mobj = re.match(self._VALID_URL, url)
312 channel_id = mobj.group('id')
313 return self._extract_videos(channel_id, 'http://vimeo.com/channels/%s' % channel_id)
316 class VimeoUserIE(VimeoChannelIE):
317 IE_NAME = u'vimeo:user'
318 _VALID_URL = r'(?:https?://)?vimeo.\com/(?P<name>[^/]+)(?:[#?]|$)'
319 _TITLE_RE = r'<a[^>]+?class="user">([^<>]+?)</a>'
322 def suitable(cls, url):
323 if VimeoChannelIE.suitable(url) or VimeoIE.suitable(url) or VimeoAlbumIE.suitable(url) or VimeoGroupsIE.suitable(url):
325 return super(VimeoUserIE, cls).suitable(url)
327 def _real_extract(self, url):
328 mobj = re.match(self._VALID_URL, url)
329 name = mobj.group('name')
330 return self._extract_videos(name, 'http://vimeo.com/%s' % name)
333 class VimeoAlbumIE(VimeoChannelIE):
334 IE_NAME = u'vimeo:album'
335 _VALID_URL = r'(?:https?://)?vimeo.\com/album/(?P<id>\d+)'
336 _TITLE_RE = r'<header id="page_header">\n\s*<h1>(.*?)</h1>'
338 def _page_url(self, base_url, pagenum):
339 return '%s/page:%d/' % (base_url, pagenum)
341 def _real_extract(self, url):
342 mobj = re.match(self._VALID_URL, url)
343 album_id = mobj.group('id')
344 return self._extract_videos(album_id, 'http://vimeo.com/album/%s' % album_id)
347 class VimeoGroupsIE(VimeoAlbumIE):
348 IE_NAME = u'vimeo:group'
349 _VALID_URL = r'(?:https?://)?vimeo.\com/groups/(?P<name>[^/]+)'
351 def _extract_list_title(self, webpage):
352 return self._og_search_title(webpage)
354 def _real_extract(self, url):
355 mobj = re.match(self._VALID_URL, url)
356 name = mobj.group('name')
357 return self._extract_videos(name, 'http://vimeo.com/groups/%s' % name)
360 class VimeoReviewIE(InfoExtractor):
361 IE_NAME = u'vimeo:review'
362 IE_DESC = u'Review pages on vimeo'
363 _VALID_URL = r'(?:https?://)?vimeo.\com/[^/]+/review/(?P<id>[^/]+)'
365 'url': 'https://vimeo.com/user21297594/review/75524534/3c257a1b5d',
366 'file': '75524534.mp4',
367 'md5': 'c507a72f780cacc12b2248bb4006d253',
369 'title': "DICK HARDWICK 'Comedian'",
370 'uploader': 'Richard Hardwick',
374 def _real_extract(self, url):
375 mobj = re.match(self._VALID_URL, url)
376 video_id = mobj.group('id')
377 player_url = 'https://player.vimeo.com/player/' + video_id
378 return self.url_result(player_url, 'Vimeo', video_id)