]>
Commit | Line | Data |
---|---|---|
cfe50f04 JMF |
1 | # encoding: utf-8 |
2 | ||
9b122384 PH |
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, | |
a5caba1e | 11 | compat_urlparse, |
9b122384 PH |
12 | |
13 | ExtractorError, | |
9d4660ca PH |
14 | smuggle_url, |
15 | unescapeHTML, | |
9b122384 | 16 | ) |
cfe50f04 | 17 | from .brightcove import BrightcoveIE |
9b122384 | 18 | |
0838239e | 19 | |
9b122384 | 20 | class GenericIE(InfoExtractor): |
0f818663 | 21 | IE_DESC = u'Generic downloader that works on some sites' |
9b122384 PH |
22 | _VALID_URL = r'.*' |
23 | IE_NAME = u'generic' | |
cfe50f04 JMF |
24 | _TESTS = [ |
25 | { | |
26 | u'url': u'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html', | |
27 | u'file': u'13601338388002.mp4', | |
aa929c37 | 28 | u'md5': u'6e15c93721d7ec9e9ca3fdbf07982cfd', |
cfe50f04 | 29 | u'info_dict': { |
0838239e | 30 | u"uploader": u"www.hodiho.fr", |
cfe50f04 JMF |
31 | u"title": u"R\u00e9gis plante sa Jeep" |
32 | } | |
33 | }, | |
9d4660ca PH |
34 | # embedded vimeo video |
35 | { | |
36 | u'url': u'http://skillsmatter.com/podcast/home/move-semanticsperfect-forwarding-and-rvalue-references', | |
37 | u'file': u'22444065.mp4', | |
38 | u'md5': u'2903896e23df39722c33f015af0666e2', | |
39 | u'info_dict': { | |
40 | u'title': u'ACCU 2011: Move Semantics,Perfect Forwarding, and Rvalue references- Scott Meyers- 13/04/2011', | |
41 | u"uploader_id": u"skillsmatter", | |
42 | u"uploader": u"Skills Matter", | |
43 | } | |
c19f7764 JMF |
44 | }, |
45 | # bandcamp page with custom domain | |
46 | { | |
47 | u'url': u'http://bronyrock.com/track/the-pony-mash', | |
48 | u'file': u'3235767654.mp3', | |
49 | u'info_dict': { | |
50 | u'title': u'The Pony Mash', | |
51 | u'uploader': u'M_Pallante', | |
52 | }, | |
53 | u'skip': u'There is a limit of 200 free downloads / month for the test song', | |
54 | }, | |
cfe50f04 | 55 | ] |
9b122384 PH |
56 | |
57 | def report_download_webpage(self, video_id): | |
58 | """Report webpage download.""" | |
59 | if not self._downloader.params.get('test', False): | |
60 | self._downloader.report_warning(u'Falling back on generic information extractor.') | |
61 | super(GenericIE, self).report_download_webpage(video_id) | |
62 | ||
63 | def report_following_redirect(self, new_url): | |
64 | """Report information extraction.""" | |
65 | self._downloader.to_screen(u'[redirect] Following redirect to %s' % new_url) | |
66 | ||
67 | def _test_redirect(self, url): | |
68 | """Check if it is a redirect, like url shorteners, in case return the new url.""" | |
69 | class HeadRequest(compat_urllib_request.Request): | |
70 | def get_method(self): | |
71 | return "HEAD" | |
72 | ||
73 | class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler): | |
74 | """ | |
75 | Subclass the HTTPRedirectHandler to make it use our | |
76 | HeadRequest also on the redirected URL | |
77 | """ | |
78 | def redirect_request(self, req, fp, code, msg, headers, newurl): | |
79 | if code in (301, 302, 303, 307): | |
80 | newurl = newurl.replace(' ', '%20') | |
81 | newheaders = dict((k,v) for k,v in req.headers.items() | |
82 | if k.lower() not in ("content-length", "content-type")) | |
83 | return HeadRequest(newurl, | |
84 | headers=newheaders, | |
85 | origin_req_host=req.get_origin_req_host(), | |
86 | unverifiable=True) | |
87 | else: | |
88 | raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp) | |
89 | ||
90 | class HTTPMethodFallback(compat_urllib_request.BaseHandler): | |
91 | """ | |
92 | Fallback to GET if HEAD is not allowed (405 HTTP error) | |
93 | """ | |
94 | def http_error_405(self, req, fp, code, msg, headers): | |
95 | fp.read() | |
96 | fp.close() | |
97 | ||
98 | newheaders = dict((k,v) for k,v in req.headers.items() | |
99 | if k.lower() not in ("content-length", "content-type")) | |
100 | return self.parent.open(compat_urllib_request.Request(req.get_full_url(), | |
101 | headers=newheaders, | |
102 | origin_req_host=req.get_origin_req_host(), | |
103 | unverifiable=True)) | |
104 | ||
105 | # Build our opener | |
106 | opener = compat_urllib_request.OpenerDirector() | |
107 | for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler, | |
108 | HTTPMethodFallback, HEADRedirectHandler, | |
109 | compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]: | |
110 | opener.add_handler(handler()) | |
111 | ||
112 | response = opener.open(HeadRequest(url)) | |
113 | if response is None: | |
114 | raise ExtractorError(u'Invalid URL protocol') | |
115 | new_url = response.geturl() | |
116 | ||
117 | if url == new_url: | |
118 | return False | |
119 | ||
120 | self.report_following_redirect(new_url) | |
121 | return new_url | |
122 | ||
123 | def _real_extract(self, url): | |
a7130543 JMF |
124 | parsed_url = compat_urlparse.urlparse(url) |
125 | if not parsed_url.scheme: | |
126 | self._downloader.report_warning('The url doesn\'t specify the protocol, trying with http') | |
127 | return self.url_result('http://' + url) | |
128 | ||
30934689 PH |
129 | try: |
130 | new_url = self._test_redirect(url) | |
131 | if new_url: | |
132 | return [self.url_result(new_url)] | |
133 | except compat_urllib_error.HTTPError: | |
134 | # This may be a stupid server that doesn't like HEAD, our UA, or so | |
135 | pass | |
9b122384 PH |
136 | |
137 | video_id = url.split('/')[-1] | |
138 | try: | |
139 | webpage = self._download_webpage(url, video_id) | |
140 | except ValueError: | |
141 | # since this is the last-resort InfoExtractor, if | |
142 | # this error is thrown, it'll be thrown here | |
e484c81f | 143 | raise ExtractorError(u'Failed to download URL: %s' % url) |
9b122384 PH |
144 | |
145 | self.report_extraction(video_id) | |
627a91a9 | 146 | # Look for BrightCove: |
32835331 | 147 | m_brightcove = re.search(r'<object[^>]+?class=([\'"])[^>]*?BrightcoveExperience.*?\1.+?</object>', webpage, re.DOTALL) |
cfe50f04 JMF |
148 | if m_brightcove is not None: |
149 | self.to_screen(u'Brightcove video detected.') | |
150 | bc_url = BrightcoveIE._build_brighcove_url(m_brightcove.group()) | |
151 | return self.url_result(bc_url, 'Brightcove') | |
152 | ||
9d4660ca PH |
153 | # Look for embedded Vimeo player |
154 | mobj = re.search( | |
53c1d3ef | 155 | r'<iframe[^>]+?src="(https?://player.vimeo.com/video/.+?)"', webpage) |
9d4660ca PH |
156 | if mobj: |
157 | player_url = unescapeHTML(mobj.group(1)) | |
158 | surl = smuggle_url(player_url, {'Referer': url}) | |
159 | return self.url_result(surl, 'Vimeo') | |
160 | ||
53c1d3ef PH |
161 | # Look for embedded YouTube player |
162 | mobj = re.search( | |
163 | r'<iframe[^>]+?src="(https?://(?:www\.)?youtube.com/embed/.+?)"', webpage) | |
164 | if mobj: | |
165 | surl = unescapeHTML(mobj.group(1)) | |
166 | return self.url_result(surl, 'Youtube') | |
167 | ||
c19f7764 JMF |
168 | # Look for Bandcamp pages with custom domain |
169 | mobj = re.search(r'<meta property="og:url"[^>]*?content="(.*?bandcamp\.com.*?)"', webpage) | |
170 | if mobj is not None: | |
171 | burl = unescapeHTML(mobj.group(1)) | |
172 | return self.url_result(burl, 'Bandcamp') | |
173 | ||
9b122384 PH |
174 | # Start with something easy: JW Player in SWFObject |
175 | mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage) | |
176 | if mobj is None: | |
177 | # Broaden the search a little bit | |
178 | mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage) | |
179 | if mobj is None: | |
180 | # Broaden the search a little bit: JWPlayer JS loader | |
52c8ade4 | 181 | mobj = re.search(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http[^\'"&]*)', webpage) |
9b122384 PH |
182 | if mobj is None: |
183 | # Try to find twitter cards info | |
184 | mobj = re.search(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage) | |
185 | if mobj is None: | |
186 | # We look for Open Graph info: | |
187 | # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am) | |
188 | m_video_type = re.search(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage) | |
189 | # We only look in og:video if the MIME type is a video, don't try if it's a Flash player: | |
190 | if m_video_type is not None: | |
191 | mobj = re.search(r'<meta.*?property="og:video".*?content="(.*?)"', webpage) | |
7fea7156 PH |
192 | if mobj is None: |
193 | # HTML5 video | |
08e291b5 | 194 | mobj = re.search(r'<video[^<]*(?:>.*?<source.*?)? src="([^"]+)"', webpage, flags=re.DOTALL) |
9b122384 | 195 | if mobj is None: |
e484c81f | 196 | raise ExtractorError(u'Unsupported URL: %s' % url) |
9b122384 PH |
197 | |
198 | # It's possible that one of the regexes | |
199 | # matched, but returned an empty group: | |
200 | if mobj.group(1) is None: | |
e484c81f | 201 | raise ExtractorError(u'Did not find a valid video URL at %s' % url) |
9b122384 | 202 | |
08e291b5 | 203 | video_url = mobj.group(1) |
a5caba1e | 204 | video_url = compat_urlparse.urljoin(url, video_url) |
08e291b5 | 205 | video_id = compat_urllib_parse.unquote(os.path.basename(video_url)) |
9b122384 PH |
206 | |
207 | # here's a fun little line of code for you: | |
208 | video_extension = os.path.splitext(video_id)[1][1:] | |
209 | video_id = os.path.splitext(video_id)[0] | |
210 | ||
211 | # it's tempting to parse this further, but you would | |
212 | # have to take into account all the variations like | |
213 | # Video Title - Site Name | |
214 | # Site Name | Video Title | |
215 | # Video Title - Tagline | Site Name | |
216 | # and so on and so forth; it's just not practical | |
217 | video_title = self._html_search_regex(r'<title>(.*)</title>', | |
5c44c154 | 218 | webpage, u'video title', default=u'video', flags=re.DOTALL) |
9b122384 PH |
219 | |
220 | # video uploader is domain name | |
221 | video_uploader = self._search_regex(r'(?:https?://)?([^/]*)/.*', | |
222 | url, u'video uploader') | |
223 | ||
224 | return [{ | |
225 | 'id': video_id, | |
226 | 'url': video_url, | |
227 | 'uploader': video_uploader, | |
228 | 'upload_date': None, | |
229 | 'title': video_title, | |
230 | 'ext': video_extension, | |
231 | }] |