]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/vimeo.py
Prepare for upload.
[youtubedl] / youtube_dl / extractor / vimeo.py
1 import json
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6 compat_urllib_parse,
7 compat_urllib_request,
8
9 clean_html,
10 get_element_by_attribute,
11 ExtractorError,
12 std_headers,
13 )
14
15 class VimeoIE(InfoExtractor):
16 """Information extractor for vimeo.com."""
17
18 # _VALID_URL matches Vimeo URLs
19 _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]+)'
20 IE_NAME = u'vimeo'
21
22 def _verify_video_password(self, url, video_id, webpage):
23 password = self._downloader.params.get('videopassword', None)
24 if password is None:
25 raise ExtractorError(u'This video is protected by a password, use the --video-password option')
26 token = re.search(r'xsrft: \'(.*?)\'', webpage).group(1)
27 data = compat_urllib_parse.urlencode({'password': password,
28 'token': token})
29 # I didn't manage to use the password with https
30 if url.startswith('https'):
31 pass_url = url.replace('https','http')
32 else:
33 pass_url = url
34 password_request = compat_urllib_request.Request(pass_url+'/password', data)
35 password_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
36 password_request.add_header('Cookie', 'xsrft=%s' % token)
37 self._download_webpage(password_request, video_id,
38 u'Verifying the password',
39 u'Wrong password')
40
41 def _real_extract(self, url, new_video=True):
42 # Extract ID from URL
43 mobj = re.match(self._VALID_URL, url)
44 if mobj is None:
45 raise ExtractorError(u'Invalid URL: %s' % url)
46
47 video_id = mobj.group('id')
48 if not mobj.group('proto'):
49 url = 'https://' + url
50 if mobj.group('direct_link') or mobj.group('pro'):
51 url = 'https://vimeo.com/' + video_id
52
53 # Retrieve video webpage to extract further information
54 request = compat_urllib_request.Request(url, None, std_headers)
55 webpage = self._download_webpage(request, video_id)
56
57 # Now we begin extracting as much information as we can from what we
58 # retrieved. First we extract the information common to all extractors,
59 # and latter we extract those that are Vimeo specific.
60 self.report_extraction(video_id)
61
62 # Extract the config JSON
63 try:
64 config = webpage.split(' = {config:')[1].split(',assets:')[0]
65 config = json.loads(config)
66 except:
67 if re.search('The creator of this video has not given you permission to embed it on this domain.', webpage):
68 raise ExtractorError(u'The author has restricted the access to this video, try with the "--referer" option')
69
70 if re.search('If so please provide the correct password.', webpage):
71 self._verify_video_password(url, video_id, webpage)
72 return self._real_extract(url)
73 else:
74 raise ExtractorError(u'Unable to extract info section')
75
76 # Extract title
77 video_title = config["video"]["title"]
78
79 # Extract uploader and uploader_id
80 video_uploader = config["video"]["owner"]["name"]
81 video_uploader_id = config["video"]["owner"]["url"].split('/')[-1] if config["video"]["owner"]["url"] else None
82
83 # Extract video thumbnail
84 video_thumbnail = config["video"]["thumbnail"]
85
86 # Extract video description
87 video_description = get_element_by_attribute("itemprop", "description", webpage)
88 if video_description: video_description = clean_html(video_description)
89 else: video_description = u''
90
91 # Extract upload date
92 video_upload_date = None
93 mobj = re.search(r'<meta itemprop="dateCreated" content="(\d{4})-(\d{2})-(\d{2})T', webpage)
94 if mobj is not None:
95 video_upload_date = mobj.group(1) + mobj.group(2) + mobj.group(3)
96
97 # Vimeo specific: extract request signature and timestamp
98 sig = config['request']['signature']
99 timestamp = config['request']['timestamp']
100
101 # Vimeo specific: extract video codec and quality information
102 # First consider quality, then codecs, then take everything
103 # TODO bind to format param
104 codecs = [('h264', 'mp4'), ('vp8', 'flv'), ('vp6', 'flv')]
105 files = { 'hd': [], 'sd': [], 'other': []}
106 for codec_name, codec_extension in codecs:
107 if codec_name in config["video"]["files"]:
108 if 'hd' in config["video"]["files"][codec_name]:
109 files['hd'].append((codec_name, codec_extension, 'hd'))
110 elif 'sd' in config["video"]["files"][codec_name]:
111 files['sd'].append((codec_name, codec_extension, 'sd'))
112 else:
113 files['other'].append((codec_name, codec_extension, config["video"]["files"][codec_name][0]))
114
115 for quality in ('hd', 'sd', 'other'):
116 if len(files[quality]) > 0:
117 video_quality = files[quality][0][2]
118 video_codec = files[quality][0][0]
119 video_extension = files[quality][0][1]
120 self.to_screen(u'%s: Downloading %s file at %s quality' % (video_id, video_codec.upper(), video_quality))
121 break
122 else:
123 raise ExtractorError(u'No known codec found')
124
125 video_url = "http://player.vimeo.com/play_redirect?clip_id=%s&sig=%s&time=%s&quality=%s&codecs=%s&type=moogaloop_local&embed_location=" \
126 %(video_id, sig, timestamp, video_quality, video_codec.upper())
127
128 return [{
129 'id': video_id,
130 'url': video_url,
131 'uploader': video_uploader,
132 'uploader_id': video_uploader_id,
133 'upload_date': video_upload_date,
134 'title': video_title,
135 'ext': video_extension,
136 'thumbnail': video_thumbnail,
137 'description': video_description,
138 }]