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