5 from .common 
import InfoExtractor
 
  11     get_element_by_attribute
, 
  17 class VimeoIE(InfoExtractor
): 
  18     """Information extractor for vimeo.com.""" 
  20     # _VALID_URL matches Vimeo URLs 
  21     _VALID_URL 
= r
'(?P<proto>https?://)?(?:(?:www|player)\.)?vimeo(?P<pro>pro)?\.com/(?:(?:(?:groups|album)/[^/]+)|(?:.*?)/)?(?P<direct_link>play_redirect_hls\?clip_id=)?(?:videos?/)?(?P<id>[0-9]+)/?(?:[?].*)?$' 
  22     _NETRC_MACHINE 
= 'vimeo' 
  26             u
'url': u
'http://vimeo.com/56015672', 
  27             u
'file': u
'56015672.mp4', 
  28             u
'md5': u
'8879b6cc097e987f02484baf890129e5', 
  30                 u
"upload_date": u
"20121220",  
  31                 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",  
  32                 u
"uploader_id": u
"user7108434",  
  33                 u
"uploader": u
"Filippo Valsorda",  
  34                 u
"title": u
"youtube-dl test video - \u2605 \" ' \u5e78 / \\ \u00e4 \u21ad \U0001d550", 
  38             u
'url': u
'http://vimeopro.com/openstreetmapus/state-of-the-map-us-2013/video/68093876', 
  39             u
'file': u
'68093876.mp4', 
  40             u
'md5': u
'3b5ca6aa22b60dfeeadf50b72e44ed82', 
  41             u
'note': u
'Vimeo Pro video (#1197)', 
  43                 u
'uploader_id': u
'openstreetmapus',  
  44                 u
'uploader': u
'OpenStreetMap US',  
  45                 u
'title': u
'Andy Allan - Putting the Carto into OpenStreetMap Cartography', 
  49             u
'url': u
'http://player.vimeo.com/video/54469442', 
  50             u
'file': u
'54469442.mp4', 
  51             u
'md5': u
'619b811a4417aa4abe78dc653becf511', 
  52             u
'note': u
'Videos that embed the url in the player page', 
  54                 u
'title': u
'Kathy Sierra: Building the minimum Badass User, Business of Software', 
  55                 u
'uploader': u
'The BLN & Business of Software', 
  61         (username
, password
) = self
._get
_login
_info
() 
  65         login_url 
= 'https://vimeo.com/log_in' 
  66         webpage 
= self
._download
_webpage
(login_url
, None, False) 
  67         token 
= re
.search(r
'xsrft: \'(.*?
)\'', webpage).group(1) 
  68         data = compat_urllib_parse.urlencode({'email
': username, 
  74         login_request = compat_urllib_request.Request(login_url, data) 
  75         login_request.add_header('Content
-Type
', 'application
/x
-www
-form
-urlencoded
') 
  76         login_request.add_header('Cookie
', 'xsrft
=%s' % token) 
  77         self._download_webpage(login_request, None, False, u'Wrong login info
') 
  79     def _verify_video_password(self, url, video_id, webpage): 
  80         password = self._downloader.params.get('videopassword
', None) 
  82             raise ExtractorError(u'This video 
is protected by a password
, use the 
--video
-password option
') 
  83         token = re.search(r'xsrft
: \'(.*?
)\'', webpage).group(1) 
  84         data = compat_urllib_parse.urlencode({'password
': password, 
  86         # I didn't manage to use the password 
with https
 
  87         if url
.startswith('https'): 
  88             pass_url 
= url
.replace('https','http') 
  91         password_request 
= compat_urllib_request
.Request(pass_url
+'/password', data
) 
  92         password_request
.add_header('Content-Type', 'application/x-www-form-urlencoded') 
  93         password_request
.add_header('Cookie', 'xsrft=%s' % token
) 
  94         self
._download
_webpage
(password_request
, video_id
, 
  95                                u
'Verifying the password', 
  98     def _real_initialize(self
): 
 101     def _real_extract(self
, url
, new_video
=True): 
 102         url
, data 
= unsmuggle_url(url
) 
 103         headers 
= std_headers
 
 105             headers 
= headers
.copy() 
 108         # Extract ID from URL 
 109         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 111             raise ExtractorError(u
'Invalid URL: %s' % url
) 
 113         video_id 
= mobj
.group('id') 
 114         if not mobj
.group('proto'): 
 115             url 
= 'https://' + url
 
 116         elif mobj
.group('pro'): 
 117             url 
= 'http://player.vimeo.com/video/' + video_id
 
 118         elif mobj
.group('direct_link'): 
 119             url 
= 'https://vimeo.com/' + video_id
 
 121         # Retrieve video webpage to extract further information 
 122         request 
= compat_urllib_request
.Request(url
, None, headers
) 
 123         webpage 
= self
._download
_webpage
(request
, video_id
) 
 125         # Now we begin extracting as much information as we can from what we 
 126         # retrieved. First we extract the information common to all extractors, 
 127         # and latter we extract those that are Vimeo specific. 
 128         self
.report_extraction(video_id
) 
 130         # Extract the config JSON 
 132             config 
= self
._search
_regex
([r
' = {config:({.+?}),assets:', r
'c=({.+?);'], 
 133                 webpage
, u
'info section', flags
=re
.DOTALL
) 
 134             config 
= json
.loads(config
) 
 136             if re
.search('The creator of this video has not given you permission to embed it on this domain.', webpage
): 
 137                 raise ExtractorError(u
'The author has restricted the access to this video, try with the "--referer" option') 
 139             if re
.search('If so please provide the correct password.', webpage
): 
 140                 self
._verify
_video
_password
(url
, video_id
, webpage
) 
 141                 return self
._real
_extract
(url
) 
 143                 raise ExtractorError(u
'Unable to extract info section') 
 146         video_title 
= config
["video"]["title"] 
 148         # Extract uploader and uploader_id 
 149         video_uploader 
= config
["video"]["owner"]["name"] 
 150         video_uploader_id 
= config
["video"]["owner"]["url"].split('/')[-1] if config
["video"]["owner"]["url"] else None 
 152         # Extract video thumbnail 
 153         video_thumbnail 
= config
["video"].get("thumbnail") 
 154         if video_thumbnail 
is None: 
 155             _
, video_thumbnail 
= sorted((int(width
), t_url
) for (width
, t_url
) in config
["video"]["thumbs"].items())[-1] 
 157         # Extract video description 
 158         video_description 
= None 
 160             video_description 
= get_element_by_attribute("itemprop", "description", webpage
) 
 161             if video_description
: video_description 
= clean_html(video_description
) 
 162         except AssertionError as err
: 
 163             # On some pages like (http://player.vimeo.com/video/54469442) the 
 164             # html tags are not closed, python 2.6 cannot handle it 
 165             if err
.args
[0] == 'we should not get here!': 
 170         # Extract upload date 
 171         video_upload_date 
= None 
 172         mobj 
= re
.search(r
'<meta itemprop="dateCreated" content="(\d{4})-(\d{2})-(\d{2})T', webpage
) 
 174             video_upload_date 
= mobj
.group(1) + mobj
.group(2) + mobj
.group(3) 
 176         # Vimeo specific: extract request signature and timestamp 
 177         sig 
= config
['request']['signature'] 
 178         timestamp 
= config
['request']['timestamp'] 
 180         # Vimeo specific: extract video codec and quality information 
 181         # First consider quality, then codecs, then take everything 
 182         codecs 
= [('vp6', 'flv'), ('vp8', 'flv'), ('h264', 'mp4')] 
 183         files 
= { 'hd': [], 'sd': [], 'other': []} 
 184         config_files 
= config
["video"].get("files") or config
["request"].get("files") 
 185         for codec_name
, codec_extension 
in codecs
: 
 186             for quality 
in config_files
.get(codec_name
, []): 
 187                 format_id 
= '-'.join((codec_name
, quality
)).lower() 
 188                 key 
= quality 
if quality 
in files 
else 'other' 
 190                 if isinstance(config_files
[codec_name
], dict): 
 191                     file_info 
= config_files
[codec_name
][quality
] 
 192                     video_url 
= file_info
.get('url') 
 195                 if video_url 
is None: 
 196                     video_url 
= "http://player.vimeo.com/play_redirect?clip_id=%s&sig=%s&time=%s&quality=%s&codecs=%s&type=moogaloop_local&embed_location=" \
 
 197                         %(video_id
, sig
, timestamp
, quality
, codec_name
.upper()) 
 200                     'ext': codec_extension
, 
 202                     'format_id': format_id
, 
 203                     'width': file_info
.get('width'), 
 204                     'height': file_info
.get('height'), 
 207         for key 
in ('other', 'sd', 'hd'): 
 208             formats 
+= files
[key
] 
 209         if len(formats
) == 0: 
 210             raise ExtractorError(u
'No known codec found') 
 214             'uploader': video_uploader
, 
 215             'uploader_id': video_uploader_id
, 
 216             'upload_date':  video_upload_date
, 
 217             'title':    video_title
, 
 218             'thumbnail':    video_thumbnail
, 
 219             'description':  video_description
, 
 224 class VimeoChannelIE(InfoExtractor
): 
 225     IE_NAME 
= u
'vimeo:channel' 
 226     _VALID_URL 
= r
'(?:https?://)?vimeo.\com/channels/(?P<id>[^/]+)' 
 227     _MORE_PAGES_INDICATOR 
= r
'<a.+?rel="next"' 
 229     def _real_extract(self
, url
): 
 230         mobj 
= re
.match(self
._VALID
_URL
, url
) 
 231         channel_id 
=  mobj
.group('id') 
 234         for pagenum 
in itertools
.count(1): 
 235             webpage 
= self
._download
_webpage
('http://vimeo.com/channels/%s/videos/page:%d' % (channel_id
, pagenum
), 
 236                                              channel_id
, u
'Downloading page %s' % pagenum
) 
 237             video_ids
.extend(re
.findall(r
'id="clip_(\d+?)"', webpage
)) 
 238             if re
.search(self
._MORE
_PAGES
_INDICATOR
, webpage
, re
.DOTALL
) is None: 
 241         entries 
= [self
.url_result('http://vimeo.com/%s' % video_id
, 'Vimeo') 
 242                    for video_id 
in video_ids
] 
 243         channel_title 
= self
._html
_search
_regex
(r
'<a href="/channels/%s">(.*?)</a>' % channel_id
, 
 244                                                 webpage
, u
'channel title') 
 245         return {'_type': 'playlist', 
 247                 'title': channel_title
,