6 from .common
import InfoExtractor
10 compat_urllib_request
,
15 from .brightcove
import BrightcoveIE
18 class GenericIE(InfoExtractor
):
19 IE_DESC
= u
'Generic downloader that works on some sites'
24 u
'url': u
'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html',
25 u
'file': u
'13601338388002.mp4',
26 u
'md5': u
'85b90ccc9d73b4acd9138d3af4c27f89',
28 u
"uploader": u
"www.hodiho.fr",
29 u
"title": u
"R\u00e9gis plante sa Jeep"
34 def report_download_webpage(self
, video_id
):
35 """Report webpage download."""
36 if not self
._downloader
.params
.get('test', False):
37 self
._downloader
.report_warning(u
'Falling back on generic information extractor.')
38 super(GenericIE
, self
).report_download_webpage(video_id
)
40 def report_following_redirect(self
, new_url
):
41 """Report information extraction."""
42 self
._downloader
.to_screen(u
'[redirect] Following redirect to %s' % new_url
)
44 def _test_redirect(self
, url
):
45 """Check if it is a redirect, like url shorteners, in case return the new url."""
46 class HeadRequest(compat_urllib_request
.Request
):
50 class HEADRedirectHandler(compat_urllib_request
.HTTPRedirectHandler
):
52 Subclass the HTTPRedirectHandler to make it use our
53 HeadRequest also on the redirected URL
55 def redirect_request(self
, req
, fp
, code
, msg
, headers
, newurl
):
56 if code
in (301, 302, 303, 307):
57 newurl
= newurl
.replace(' ', '%20')
58 newheaders
= dict((k
,v
) for k
,v
in req
.headers
.items()
59 if k
.lower() not in ("content-length", "content-type"))
60 return HeadRequest(newurl
,
62 origin_req_host
=req
.get_origin_req_host(),
65 raise compat_urllib_error
.HTTPError(req
.get_full_url(), code
, msg
, headers
, fp
)
67 class HTTPMethodFallback(compat_urllib_request
.BaseHandler
):
69 Fallback to GET if HEAD is not allowed (405 HTTP error)
71 def http_error_405(self
, req
, fp
, code
, msg
, headers
):
75 newheaders
= dict((k
,v
) for k
,v
in req
.headers
.items()
76 if k
.lower() not in ("content-length", "content-type"))
77 return self
.parent
.open(compat_urllib_request
.Request(req
.get_full_url(),
79 origin_req_host
=req
.get_origin_req_host(),
83 opener
= compat_urllib_request
.OpenerDirector()
84 for handler
in [compat_urllib_request
.HTTPHandler
, compat_urllib_request
.HTTPDefaultErrorHandler
,
85 HTTPMethodFallback
, HEADRedirectHandler
,
86 compat_urllib_request
.HTTPErrorProcessor
, compat_urllib_request
.HTTPSHandler
]:
87 opener
.add_handler(handler())
89 response
= opener
.open(HeadRequest(url
))
91 raise ExtractorError(u
'Invalid URL protocol')
92 new_url
= response
.geturl()
97 self
.report_following_redirect(new_url
)
100 def _real_extract(self
, url
):
101 parsed_url
= compat_urlparse
.urlparse(url
)
102 if not parsed_url
.scheme
:
103 self
._downloader
.report_warning('The url doesn\'t specify the protocol, trying with http')
104 return self
.url_result('http://' + url
)
107 new_url
= self
._test
_redirect
(url
)
109 return [self
.url_result(new_url
)]
110 except compat_urllib_error
.HTTPError
:
111 # This may be a stupid server that doesn't like HEAD, our UA, or so
114 video_id
= url
.split('/')[-1]
116 webpage
= self
._download
_webpage
(url
, video_id
)
118 # since this is the last-resort InfoExtractor, if
119 # this error is thrown, it'll be thrown here
120 raise ExtractorError(u
'Invalid URL: %s' % url
)
122 self
.report_extraction(video_id
)
123 # Look for BrightCove:
124 m_brightcove
= re
.search(r
'<object.+?class=([\'"]).*?BrightcoveExperience.*?\1.+?</object>', webpage, re.DOTALL)
125 if m_brightcove is not None:
126 self.to_screen(u'Brightcove video detected.')
127 bc_url = BrightcoveIE._build_brighcove_url(m_brightcove.group())
128 return self.url_result(bc_url, 'Brightcove')
130 # Start with something easy: JW Player in SWFObject
131 mobj = re.search(r'flashvars: [\'"](?
:.*&)?
file=(http
[^
\'"&]*)', webpage)
133 # Broaden the search a little bit
134 mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
136 # Broaden the search a little bit: JWPlayer JS loader
137 mobj = re.search(r'[^A
-Za
-z0
-9]?
file["\']?:\s*["\'](http
[^
\'"&]*)', webpage)
139 # Try to find twitter cards info
140 mobj = re.search(r'<meta (?:property|name)="twitter
:player
:stream
" (?:content|value)="(.+?
)"', webpage)
142 # We look for Open Graph info:
143 # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am)
144 m_video_type = re.search(r'<meta.*?property="og
:video
:type".*?content="video
/(.*?
)"', webpage)
145 # We only look in og:video if the MIME type is a video, don't try if it's a Flash player:
146 if m_video_type is not None:
147 mobj = re.search(r'<meta.*?property="og
:video
".*?content="(.*?
)"', webpage)
150 mobj = re.search(r'<video[^<]*(?:>.*?<source.*?)? src="([^
"]+)"', webpage, flags=re.DOTALL)
152 raise ExtractorError(u'Invalid URL
: %s' % url)
154 # It's possible that one of the regexes
155 # matched, but returned an empty group:
156 if mobj
.group(1) is None:
157 raise ExtractorError(u
'Invalid URL: %s' % url
)
159 video_url
= mobj
.group(1)
160 video_url
= compat_urlparse
.urljoin(url
, video_url
)
161 video_id
= compat_urllib_parse
.unquote(os
.path
.basename(video_url
))
163 # here's a fun little line of code for you:
164 video_extension
= os
.path
.splitext(video_id
)[1][1:]
165 video_id
= os
.path
.splitext(video_id
)[0]
167 # it's tempting to parse this further, but you would
168 # have to take into account all the variations like
169 # Video Title - Site Name
170 # Site Name | Video Title
171 # Video Title - Tagline | Site Name
172 # and so on and so forth; it's just not practical
173 video_title
= self
._html
_search
_regex
(r
'<title>(.*)</title>',
174 webpage
, u
'video title', default
=u
'video', flags
=re
.DOTALL
)
176 # video uploader is domain name
177 video_uploader
= self
._search
_regex
(r
'(?:https?://)?([^/]*)/.*',
178 url
, u
'video uploader')
183 'uploader': video_uploader
,
185 'title': video_title
,
186 'ext': video_extension
,