]>
Commit | Line | Data |
---|---|---|
cfe50f04 JMF |
1 | # encoding: utf-8 |
2 | ||
79649588 PH |
3 | from __future__ import unicode_literals |
4 | ||
9b122384 PH |
5 | import os |
6 | import re | |
7 | ||
8 | from .common import InfoExtractor | |
fc9713a1 | 9 | from .youtube import YoutubeIE |
9b122384 PH |
10 | from ..utils import ( |
11 | compat_urllib_error, | |
12 | compat_urllib_parse, | |
13 | compat_urllib_request, | |
a5caba1e | 14 | compat_urlparse, |
9b122384 PH |
15 | |
16 | ExtractorError, | |
aa94a6d3 | 17 | HEADRequest, |
9d4660ca PH |
18 | smuggle_url, |
19 | unescapeHTML, | |
42393ce2 PH |
20 | unified_strdate, |
21 | url_basename, | |
9b122384 | 22 | ) |
cfe50f04 | 23 | from .brightcove import BrightcoveIE |
c0d0b01f | 24 | from .ooyala import OoyalaIE |
9b122384 | 25 | |
0838239e | 26 | |
9b122384 | 27 | class GenericIE(InfoExtractor): |
79649588 | 28 | IE_DESC = 'Generic downloader that works on some sites' |
9b122384 | 29 | _VALID_URL = r'.*' |
79649588 | 30 | IE_NAME = 'generic' |
cfe50f04 JMF |
31 | _TESTS = [ |
32 | { | |
79649588 PH |
33 | 'url': 'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html', |
34 | 'file': '13601338388002.mp4', | |
35 | 'md5': '6e15c93721d7ec9e9ca3fdbf07982cfd', | |
36 | 'info_dict': { | |
37 | 'uploader': 'www.hodiho.fr', | |
38 | 'title': 'R\u00e9gis plante sa Jeep', | |
cfe50f04 JMF |
39 | } |
40 | }, | |
9d4660ca PH |
41 | # embedded vimeo video |
42 | { | |
79649588 PH |
43 | 'add_ie': ['Vimeo'], |
44 | 'url': 'http://skillsmatter.com/podcast/home/move-semanticsperfect-forwarding-and-rvalue-references', | |
45 | 'file': '22444065.mp4', | |
46 | 'md5': '2903896e23df39722c33f015af0666e2', | |
47 | 'info_dict': { | |
48 | 'title': 'ACCU 2011: Move Semantics,Perfect Forwarding, and Rvalue references- Scott Meyers- 13/04/2011', | |
49 | 'uploader_id': 'skillsmatter', | |
50 | 'uploader': 'Skills Matter', | |
9d4660ca | 51 | } |
c19f7764 JMF |
52 | }, |
53 | # bandcamp page with custom domain | |
54 | { | |
79649588 PH |
55 | 'add_ie': ['Bandcamp'], |
56 | 'url': 'http://bronyrock.com/track/the-pony-mash', | |
57 | 'file': '3235767654.mp3', | |
58 | 'info_dict': { | |
59 | 'title': 'The Pony Mash', | |
60 | 'uploader': 'M_Pallante', | |
c19f7764 | 61 | }, |
79649588 | 62 | 'skip': 'There is a limit of 200 free downloads / month for the test song', |
c19f7764 | 63 | }, |
eeb165e6 | 64 | # embedded brightcove video |
dd5bcdc4 JMF |
65 | # it also tests brightcove videos that need to set the 'Referer' in the |
66 | # http requests | |
eeb165e6 | 67 | { |
79649588 PH |
68 | 'add_ie': ['Brightcove'], |
69 | 'url': 'http://www.bfmtv.com/video/bfmbusiness/cours-bourse/cours-bourse-l-analyse-technique-154522/', | |
70 | 'info_dict': { | |
71 | 'id': '2765128793001', | |
72 | 'ext': 'mp4', | |
73 | 'title': 'Le cours de bourse : l’analyse technique', | |
74 | 'description': 'md5:7e9ad046e968cb2d1114004aba466fd9', | |
75 | 'uploader': 'BFM BUSINESS', | |
eeb165e6 | 76 | }, |
79649588 PH |
77 | 'params': { |
78 | 'skip_download': True, | |
eeb165e6 JMF |
79 | }, |
80 | }, | |
42393ce2 PH |
81 | # Direct link to a video |
82 | { | |
79649588 PH |
83 | 'url': 'http://media.w3.org/2010/05/sintel/trailer.mp4', |
84 | 'file': 'trailer.mp4', | |
85 | 'md5': '67d406c2bcb6af27fa886f31aa934bbe', | |
86 | 'info_dict': { | |
87 | 'id': 'trailer', | |
88 | 'title': 'trailer', | |
89 | 'upload_date': '20100513', | |
42393ce2 | 90 | } |
c0d0b01f JMF |
91 | }, |
92 | # ooyala video | |
93 | { | |
79649588 PH |
94 | 'url': 'http://www.rollingstone.com/music/videos/norwegian-dj-cashmere-cat-goes-spartan-on-with-me-premiere-20131219', |
95 | 'md5': '5644c6ca5d5782c1d0d350dad9bd840c', | |
96 | 'info_dict': { | |
97 | 'id': 'BwY2RxaTrTkslxOfcan0UCf0YqyvWysJ', | |
98 | 'ext': 'mp4', | |
99 | 'title': '2cc213299525360.mov', #that's what we get | |
c0d0b01f JMF |
100 | }, |
101 | }, | |
cfe50f04 | 102 | ] |
9b122384 PH |
103 | |
104 | def report_download_webpage(self, video_id): | |
105 | """Report webpage download.""" | |
106 | if not self._downloader.params.get('test', False): | |
79649588 | 107 | self._downloader.report_warning('Falling back on generic information extractor.') |
9b122384 PH |
108 | super(GenericIE, self).report_download_webpage(video_id) |
109 | ||
110 | def report_following_redirect(self, new_url): | |
111 | """Report information extraction.""" | |
79649588 | 112 | self._downloader.to_screen('[redirect] Following redirect to %s' % new_url) |
9b122384 | 113 | |
42393ce2 | 114 | def _send_head(self, url): |
9b122384 | 115 | """Check if it is a redirect, like url shorteners, in case return the new url.""" |
9b122384 PH |
116 | |
117 | class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler): | |
118 | """ | |
119 | Subclass the HTTPRedirectHandler to make it use our | |
aa94a6d3 | 120 | HEADRequest also on the redirected URL |
9b122384 PH |
121 | """ |
122 | def redirect_request(self, req, fp, code, msg, headers, newurl): | |
123 | if code in (301, 302, 303, 307): | |
124 | newurl = newurl.replace(' ', '%20') | |
125 | newheaders = dict((k,v) for k,v in req.headers.items() | |
126 | if k.lower() not in ("content-length", "content-type")) | |
aa94a6d3 | 127 | return HEADRequest(newurl, |
9b122384 PH |
128 | headers=newheaders, |
129 | origin_req_host=req.get_origin_req_host(), | |
130 | unverifiable=True) | |
131 | else: | |
132 | raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp) | |
133 | ||
134 | class HTTPMethodFallback(compat_urllib_request.BaseHandler): | |
135 | """ | |
136 | Fallback to GET if HEAD is not allowed (405 HTTP error) | |
137 | """ | |
138 | def http_error_405(self, req, fp, code, msg, headers): | |
139 | fp.read() | |
140 | fp.close() | |
141 | ||
142 | newheaders = dict((k,v) for k,v in req.headers.items() | |
143 | if k.lower() not in ("content-length", "content-type")) | |
144 | return self.parent.open(compat_urllib_request.Request(req.get_full_url(), | |
145 | headers=newheaders, | |
146 | origin_req_host=req.get_origin_req_host(), | |
147 | unverifiable=True)) | |
148 | ||
149 | # Build our opener | |
150 | opener = compat_urllib_request.OpenerDirector() | |
151 | for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler, | |
152 | HTTPMethodFallback, HEADRedirectHandler, | |
153 | compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]: | |
154 | opener.add_handler(handler()) | |
155 | ||
aa94a6d3 | 156 | response = opener.open(HEADRequest(url)) |
9b122384 | 157 | if response is None: |
79649588 | 158 | raise ExtractorError('Invalid URL protocol') |
42393ce2 | 159 | return response |
9b122384 PH |
160 | |
161 | def _real_extract(self, url): | |
a7130543 JMF |
162 | parsed_url = compat_urlparse.urlparse(url) |
163 | if not parsed_url.scheme: | |
164 | self._downloader.report_warning('The url doesn\'t specify the protocol, trying with http') | |
165 | return self.url_result('http://' + url) | |
42393ce2 | 166 | video_id = os.path.splitext(url.split('/')[-1])[0] |
a7130543 | 167 | |
79649588 | 168 | self.to_screen('%s: Requesting header' % video_id) |
c1d1facd | 169 | |
30934689 | 170 | try: |
42393ce2 PH |
171 | response = self._send_head(url) |
172 | ||
173 | # Check for redirect | |
174 | new_url = response.geturl() | |
175 | if url != new_url: | |
176 | self.report_following_redirect(new_url) | |
cecaaf3f | 177 | return self.url_result(new_url) |
42393ce2 PH |
178 | |
179 | # Check for direct link to a video | |
180 | content_type = response.headers.get('Content-Type', '') | |
3e785145 | 181 | m = re.match(r'^(?P<type>audio|video|application(?=/ogg$))/(?P<format_id>.+)$', content_type) |
42393ce2 PH |
182 | if m: |
183 | upload_date = response.headers.get('Last-Modified') | |
184 | if upload_date: | |
185 | upload_date = unified_strdate(upload_date) | |
42393ce2 PH |
186 | return { |
187 | 'id': video_id, | |
188 | 'title': os.path.splitext(url_basename(url))[0], | |
189 | 'formats': [{ | |
190 | 'format_id': m.group('format_id'), | |
191 | 'url': url, | |
79649588 | 192 | 'vcodec': 'none' if m.group('type') == 'audio' else None |
42393ce2 PH |
193 | }], |
194 | 'upload_date': upload_date, | |
195 | } | |
196 | ||
30934689 PH |
197 | except compat_urllib_error.HTTPError: |
198 | # This may be a stupid server that doesn't like HEAD, our UA, or so | |
199 | pass | |
9b122384 | 200 | |
9b122384 PH |
201 | try: |
202 | webpage = self._download_webpage(url, video_id) | |
203 | except ValueError: | |
204 | # since this is the last-resort InfoExtractor, if | |
205 | # this error is thrown, it'll be thrown here | |
79649588 | 206 | raise ExtractorError('Failed to download URL: %s' % url) |
9b122384 PH |
207 | |
208 | self.report_extraction(video_id) | |
887c6acd PH |
209 | |
210 | # it's tempting to parse this further, but you would | |
211 | # have to take into account all the variations like | |
212 | # Video Title - Site Name | |
213 | # Site Name | Video Title | |
214 | # Video Title - Tagline | Site Name | |
215 | # and so on and so forth; it's just not practical | |
ef4fd848 | 216 | video_title = self._html_search_regex( |
79649588 PH |
217 | r'(?s)<title>(.*?)</title>', webpage, 'video title', |
218 | default='video') | |
ef4fd848 PH |
219 | |
220 | # video uploader is domain name | |
221 | video_uploader = self._search_regex( | |
79649588 | 222 | r'^(?:https?://)?([^/]*)/.*', url, 'video uploader') |
887c6acd | 223 | |
627a91a9 | 224 | # Look for BrightCove: |
eeb165e6 JMF |
225 | bc_url = BrightcoveIE._extract_brightcove_url(webpage) |
226 | if bc_url is not None: | |
79649588 | 227 | self.to_screen('Brightcove video detected.') |
79f82953 PH |
228 | surl = smuggle_url(bc_url, {'Referer': url}) |
229 | return self.url_result(surl, 'Brightcove') | |
cfe50f04 | 230 | |
7115ca84 | 231 | # Look for embedded (iframe) Vimeo player |
9d4660ca | 232 | mobj = re.search( |
3fabeaa1 | 233 | r'<iframe[^>]+?src="((?:https?:)?//player.vimeo.com/video/.+?)"', webpage) |
9d4660ca PH |
234 | if mobj: |
235 | player_url = unescapeHTML(mobj.group(1)) | |
236 | surl = smuggle_url(player_url, {'Referer': url}) | |
237 | return self.url_result(surl, 'Vimeo') | |
238 | ||
7115ca84 PH |
239 | # Look for embedded (swf embed) Vimeo player |
240 | mobj = re.search( | |
241 | r'<embed[^>]+?src="(https?://(?:www\.)?vimeo.com/moogaloop.swf.+?)"', webpage) | |
242 | if mobj: | |
243 | return self.url_result(mobj.group(1), 'Vimeo') | |
244 | ||
53c1d3ef | 245 | # Look for embedded YouTube player |
1f9da904 PH |
246 | matches = re.findall(r'''(?x) |
247 | (?:<iframe[^>]+?src=|embedSWF\(\s*) | |
248 | (["\'])(?P<url>(?:https?:)?//(?:www\.)?youtube\.com/ | |
249 | (?:embed|v)/.+?) | |
250 | \1''', webpage) | |
887c6acd PH |
251 | if matches: |
252 | urlrs = [self.url_result(unescapeHTML(tuppl[1]), 'Youtube') | |
253 | for tuppl in matches] | |
254 | return self.playlist_result( | |
255 | urlrs, playlist_id=video_id, playlist_title=video_title) | |
53c1d3ef | 256 | |
355e4fd0 PH |
257 | # Look for embedded Dailymotion player |
258 | matches = re.findall( | |
ef4fd848 | 259 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/.+?)\1', webpage) |
355e4fd0 PH |
260 | if matches: |
261 | urlrs = [self.url_result(unescapeHTML(tuppl[1]), 'Dailymotion') | |
262 | for tuppl in matches] | |
263 | return self.playlist_result( | |
264 | urlrs, playlist_id=video_id, playlist_title=video_title) | |
265 | ||
ef4fd848 PH |
266 | # Look for embedded Wistia player |
267 | match = re.search( | |
268 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage) | |
269 | if match: | |
270 | return { | |
271 | '_type': 'url_transparent', | |
272 | 'url': unescapeHTML(match.group('url')), | |
273 | 'ie_key': 'Wistia', | |
274 | 'uploader': video_uploader, | |
275 | 'title': video_title, | |
276 | 'id': video_id, | |
277 | } | |
278 | ||
ee3e63e4 | 279 | # Look for embedded blip.tv player |
19dab5e6 | 280 | mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage) |
ee3e63e4 | 281 | if mobj: |
19dab5e6 | 282 | return self.url_result('http://blip.tv/a/a-'+mobj.group(1), 'BlipTV') |
283 | mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9]+)', webpage) | |
ee3e63e4 | 284 | if mobj: |
19dab5e6 | 285 | return self.url_result(mobj.group(1), 'BlipTV') |
ee3e63e4 | 286 | |
c19f7764 JMF |
287 | # Look for Bandcamp pages with custom domain |
288 | mobj = re.search(r'<meta property="og:url"[^>]*?content="(.*?bandcamp\.com.*?)"', webpage) | |
289 | if mobj is not None: | |
290 | burl = unescapeHTML(mobj.group(1)) | |
09804265 JMF |
291 | # Don't set the extractor because it can be a track url or an album |
292 | return self.url_result(burl) | |
c19f7764 | 293 | |
f25571ff PH |
294 | # Look for embedded Vevo player |
295 | mobj = re.search( | |
296 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:cache\.)?vevo\.com/.+?)\1', webpage) | |
297 | if mobj is not None: | |
298 | return self.url_result(mobj.group('url')) | |
299 | ||
c0d0b01f JMF |
300 | # Look for Ooyala videos |
301 | mobj = re.search(r'player.ooyala.com/[^"?]+\?[^"]*?(?:embedCode|ec)=([^"&]+)', webpage) | |
302 | if mobj is not None: | |
303 | return OoyalaIE._build_url_result(mobj.group(1)) | |
304 | ||
aa94a6d3 | 305 | # Look for Aparat videos |
42e4fcf2 | 306 | mobj = re.search(r'<iframe src="(http://www\.aparat\.com/video/[^"]+)"', webpage) |
aa94a6d3 PH |
307 | if mobj is not None: |
308 | return self.url_result(mobj.group(1), 'Aparat') | |
309 | ||
c93c2ab1 | 310 | # Look for MPORA videos |
42e4fcf2 | 311 | mobj = re.search(r'<iframe .*?src="(http://mpora\.com/videos/[^"]+)"', webpage) |
c93c2ab1 PH |
312 | if mobj is not None: |
313 | return self.url_result(mobj.group(1), 'Mpora') | |
5f59ee79 | 314 | |
8f89e687 | 315 | # Look for embedded Novamov player |
316 | mobj = re.search( | |
317 | r'<iframe[^>]+?src=(["\'])(?P<url>http://(?:(?:embed|www)\.)?novamov\.com/embed\.php.+?)\1', webpage) | |
318 | if mobj is not None: | |
319 | return self.url_result(mobj.group('url'), 'Novamov') | |
c93c2ab1 | 320 | |
9b122384 PH |
321 | # Start with something easy: JW Player in SWFObject |
322 | mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage) | |
d981cef6 PH |
323 | if mobj is None: |
324 | # Look for gorilla-vid style embedding | |
325 | mobj = re.search(r'(?s)jw_plugins.*?file:\s*["\'](.*?)["\']', webpage) | |
9b122384 PH |
326 | if mobj is None: |
327 | # Broaden the search a little bit | |
328 | mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage) | |
329 | if mobj is None: | |
330 | # Broaden the search a little bit: JWPlayer JS loader | |
2f21eb2d | 331 | mobj = re.search(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http(?![^\'"]+\.[0-9]+[\'"])[^\'"]+)["\']', webpage) |
9b122384 PH |
332 | if mobj is None: |
333 | # Try to find twitter cards info | |
334 | mobj = re.search(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage) | |
335 | if mobj is None: | |
336 | # We look for Open Graph info: | |
337 | # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am) | |
338 | m_video_type = re.search(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage) | |
339 | # We only look in og:video if the MIME type is a video, don't try if it's a Flash player: | |
340 | if m_video_type is not None: | |
341 | mobj = re.search(r'<meta.*?property="og:video".*?content="(.*?)"', webpage) | |
7fea7156 PH |
342 | if mobj is None: |
343 | # HTML5 video | |
08e291b5 | 344 | mobj = re.search(r'<video[^<]*(?:>.*?<source.*?)? src="([^"]+)"', webpage, flags=re.DOTALL) |
9b122384 | 345 | if mobj is None: |
79649588 | 346 | raise ExtractorError('Unsupported URL: %s' % url) |
9b122384 PH |
347 | |
348 | # It's possible that one of the regexes | |
349 | # matched, but returned an empty group: | |
350 | if mobj.group(1) is None: | |
79649588 | 351 | raise ExtractorError('Did not find a valid video URL at %s' % url) |
9b122384 | 352 | |
08e291b5 | 353 | video_url = mobj.group(1) |
a5caba1e | 354 | video_url = compat_urlparse.urljoin(url, video_url) |
08e291b5 | 355 | video_id = compat_urllib_parse.unquote(os.path.basename(video_url)) |
9b122384 | 356 | |
fc9713a1 PH |
357 | # Sometimes, jwplayer extraction will result in a YouTube URL |
358 | if YoutubeIE.suitable(video_url): | |
359 | return self.url_result(video_url, 'Youtube') | |
360 | ||
9b122384 | 361 | # here's a fun little line of code for you: |
9b122384 PH |
362 | video_id = os.path.splitext(video_id)[0] |
363 | ||
113577e1 | 364 | return { |
fc9713a1 PH |
365 | 'id': video_id, |
366 | 'url': video_url, | |
9b122384 | 367 | 'uploader': video_uploader, |
fc9713a1 | 368 | 'title': video_title, |
113577e1 | 369 | } |