4 from .common import InfoExtractor
13 class GenericIE(InfoExtractor):
14 """Generic last-resort information extractor."""
19 u'url': u'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html',
20 u'file': u'13601338388002.mp4',
21 u'md5': u'85b90ccc9d73b4acd9138d3af4c27f89',
23 u"uploader": u"www.hodiho.fr",
24 u"title": u"R\u00e9gis plante sa Jeep"
28 def report_download_webpage(self, video_id):
29 """Report webpage download."""
30 if not self._downloader.params.get('test', False):
31 self._downloader.report_warning(u'Falling back on generic information extractor.')
32 super(GenericIE, self).report_download_webpage(video_id)
34 def report_following_redirect(self, new_url):
35 """Report information extraction."""
36 self._downloader.to_screen(u'[redirect] Following redirect to %s' % new_url)
38 def _test_redirect(self, url):
39 """Check if it is a redirect, like url shorteners, in case return the new url."""
40 class HeadRequest(compat_urllib_request.Request):
44 class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
46 Subclass the HTTPRedirectHandler to make it use our
47 HeadRequest also on the redirected URL
49 def redirect_request(self, req, fp, code, msg, headers, newurl):
50 if code in (301, 302, 303, 307):
51 newurl = newurl.replace(' ', '%20')
52 newheaders = dict((k,v) for k,v in req.headers.items()
53 if k.lower() not in ("content-length", "content-type"))
54 return HeadRequest(newurl,
56 origin_req_host=req.get_origin_req_host(),
59 raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp)
61 class HTTPMethodFallback(compat_urllib_request.BaseHandler):
63 Fallback to GET if HEAD is not allowed (405 HTTP error)
65 def http_error_405(self, req, fp, code, msg, headers):
69 newheaders = dict((k,v) for k,v in req.headers.items()
70 if k.lower() not in ("content-length", "content-type"))
71 return self.parent.open(compat_urllib_request.Request(req.get_full_url(),
73 origin_req_host=req.get_origin_req_host(),
77 opener = compat_urllib_request.OpenerDirector()
78 for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler,
79 HTTPMethodFallback, HEADRedirectHandler,
80 compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]:
81 opener.add_handler(handler())
83 response = opener.open(HeadRequest(url))
85 raise ExtractorError(u'Invalid URL protocol')
86 new_url = response.geturl()
91 self.report_following_redirect(new_url)
94 def _real_extract(self, url):
95 new_url = self._test_redirect(url)
96 if new_url: return [self.url_result(new_url)]
98 video_id = url.split('/')[-1]
100 webpage = self._download_webpage(url, video_id)
102 # since this is the last-resort InfoExtractor, if
103 # this error is thrown, it'll be thrown here
104 raise ExtractorError(u'Invalid URL: %s' % url)
106 self.report_extraction(video_id)
107 # Start with something easy: JW Player in SWFObject
108 mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
110 # Broaden the search a little bit
111 mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
113 # Broaden the search a little bit: JWPlayer JS loader
114 mobj = re.search(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http[^\'"&]*)', webpage)
116 # Try to find twitter cards info
117 mobj = re.search(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage)
119 # We look for Open Graph info:
120 # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am)
121 m_video_type = re.search(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage)
122 # We only look in og:video if the MIME type is a video, don't try if it's a Flash player:
123 if m_video_type is not None:
124 mobj = re.search(r'<meta.*?property="og:video".*?content="(.*?)"', webpage)
126 raise ExtractorError(u'Invalid URL: %s' % url)
128 # It's possible that one of the regexes
129 # matched, but returned an empty group:
130 if mobj.group(1) is None:
131 raise ExtractorError(u'Invalid URL: %s' % url)
133 video_url = compat_urllib_parse.unquote(mobj.group(1))
134 video_id = os.path.basename(video_url)
136 # here's a fun little line of code for you:
137 video_extension = os.path.splitext(video_id)[1][1:]
138 video_id = os.path.splitext(video_id)[0]
140 # it's tempting to parse this further, but you would
141 # have to take into account all the variations like
142 # Video Title - Site Name
143 # Site Name | Video Title
144 # Video Title - Tagline | Site Name
145 # and so on and so forth; it's just not practical
146 video_title = self._html_search_regex(r'<title>(.*)</title>',
147 webpage, u'video title', default=u'video')
149 # video uploader is domain name
150 video_uploader = self._search_regex(r'(?:https?://)?([^/]*)/.*',
151 url, u'video uploader')
156 'uploader': video_uploader,
158 'title': video_title,
159 'ext': video_extension,