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