Merge remote-tracking branch 'origin/vimeo_passworded_videos'
[youtube-dl] / youtube_dl / InfoExtractors.py
index 4aec8c6879e79ccc13cf5e9bcfbf9abc17c4d1d1..574d417beecc97718edcc6da864acb4d503d325a 100755 (executable)
@@ -1096,6 +1096,25 @@ class VimeoIE(InfoExtractor):
     _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]+)'
     IE_NAME = u'vimeo'
 
+    def _verify_video_password(self, url, video_id, webpage):
+        password = self._downloader.params.get('password', None)
+        if password is None:
+            raise ExtractorError(u'This video is protected by a password, use the --password option')
+        token = re.search(r'xsrft: \'(.*?)\'', webpage).group(1)
+        data = compat_urllib_parse.urlencode({'password': password,
+                                              'token': token})
+        # I didn't manage to use the password with https
+        if url.startswith('https'):
+            pass_url = url.replace('https','http')
+        else:
+            pass_url = url
+        password_request = compat_urllib_request.Request(pass_url+'/password', data)
+        password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
+        password_request.add_header('Cookie', 'xsrft=%s' % token)
+        pass_web = self._download_webpage(password_request, video_id,
+                                          u'Verifying the password',
+                                          u'Wrong password')
+
     def _real_extract(self, url, new_video=True):
         # Extract ID from URL
         mobj = re.match(self._VALID_URL, url)
@@ -1124,6 +1143,10 @@ class VimeoIE(InfoExtractor):
         except:
             if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
                 raise ExtractorError(u'The author has restricted the access to this video, try with the "--referer" option')
+
+            if re.search('If so please provide the correct password.', webpage):
+                self._verify_video_password(url, video_id, webpage)
+                return self._real_extract(url)
             else:
                 raise ExtractorError(u'Unable to extract info section')
 
@@ -4552,20 +4575,28 @@ class GametrailersIE(InfoExtractor):
                 'description': video_description,
                 }
 
-class StatigrIE(InfoExtractor):
+class StatigramIE(InfoExtractor):
     _VALID_URL = r'(?:http://)?(?:www\.)?statigr\.am/p/([^/]+)'
 
     def _real_extract(self, url):
         mobj = re.match(self._VALID_URL, url)
-        if mobj is None:
-            raise ExtractorError(u'Invalid URL: %s' % url)
+
         video_id = mobj.group(1)
         webpage = self._download_webpage(url, video_id)
-        video_url = re.search(r'<meta property="og:video:secure_url" content="(.+?)">',webpage).group(1)
-        thumbnail_url = re.search(r'<meta property="og:image" content="(.+?)" />',webpage).group(1)
-        title = (re.search(r'<title>(.+?)</title>',webpage).group(1)).strip("| Statigram")
-        uploader = re.search(r'@(.+) \(Videos\)',title).group(1)
-        ext = "mp4"
+        video_url = self._html_search_regex(
+            r'<meta property="og:video:secure_url" content="(.+?)">',
+            webpage, u'video URL')
+        thumbnail_url = self._html_search_regex(
+            r'<meta property="og:image" content="(.+?)" />',
+            webpage, u'thumbnail URL', fatal=False)
+        html_title = self._html_search_regex(
+            r'<title>(.+?)</title>',
+            webpage, u'title')
+        title = html_title.rpartition(u' | Statigram')[0]
+        uploader = self._html_search_regex(
+            r'@(.+) \(Videos\)', title, u'uploader name', fatal=False)
+        ext = 'mp4'
+
         return [{
             'id':        video_id,
             'url':       video_url,
@@ -4641,7 +4672,7 @@ def gen_extractors():
         HypemIE(),
         Vbox7IE(),
         GametrailersIE(),
-        StatigrIE(),
+        StatigramIE(),
         GenericIE()
     ]