generic.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import os
  4. import re
  5. from .common import InfoExtractor
  6. from .youtube import YoutubeIE
  7. from ..utils import (
  8. compat_urllib_error,
  9. compat_urllib_parse,
  10. compat_urllib_request,
  11. compat_urlparse,
  12. compat_xml_parse_error,
  13. ExtractorError,
  14. HEADRequest,
  15. parse_xml,
  16. smuggle_url,
  17. unescapeHTML,
  18. unified_strdate,
  19. url_basename,
  20. )
  21. from .brightcove import BrightcoveIE
  22. from .ooyala import OoyalaIE
  23. from .rutv import RUTVIE
  24. class GenericIE(InfoExtractor):
  25. IE_DESC = 'Generic downloader that works on some sites'
  26. _VALID_URL = r'.*'
  27. IE_NAME = 'generic'
  28. _TESTS = [
  29. {
  30. 'url': 'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html',
  31. 'file': '13601338388002.mp4',
  32. 'md5': '6e15c93721d7ec9e9ca3fdbf07982cfd',
  33. 'info_dict': {
  34. 'uploader': 'www.hodiho.fr',
  35. 'title': 'R\u00e9gis plante sa Jeep',
  36. }
  37. },
  38. # bandcamp page with custom domain
  39. {
  40. 'add_ie': ['Bandcamp'],
  41. 'url': 'http://bronyrock.com/track/the-pony-mash',
  42. 'file': '3235767654.mp3',
  43. 'info_dict': {
  44. 'title': 'The Pony Mash',
  45. 'uploader': 'M_Pallante',
  46. },
  47. 'skip': 'There is a limit of 200 free downloads / month for the test song',
  48. },
  49. # embedded brightcove video
  50. # it also tests brightcove videos that need to set the 'Referer' in the
  51. # http requests
  52. {
  53. 'add_ie': ['Brightcove'],
  54. 'url': 'http://www.bfmtv.com/video/bfmbusiness/cours-bourse/cours-bourse-l-analyse-technique-154522/',
  55. 'info_dict': {
  56. 'id': '2765128793001',
  57. 'ext': 'mp4',
  58. 'title': 'Le cours de bourse : l’analyse technique',
  59. 'description': 'md5:7e9ad046e968cb2d1114004aba466fd9',
  60. 'uploader': 'BFM BUSINESS',
  61. },
  62. 'params': {
  63. 'skip_download': True,
  64. },
  65. },
  66. {
  67. # https://github.com/rg3/youtube-dl/issues/2253
  68. 'url': 'http://bcove.me/i6nfkrc3',
  69. 'file': '3101154703001.mp4',
  70. 'md5': '0ba9446db037002366bab3b3eb30c88c',
  71. 'info_dict': {
  72. 'title': 'Still no power',
  73. 'uploader': 'thestar.com',
  74. '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.',
  75. },
  76. 'add_ie': ['Brightcove'],
  77. },
  78. # Direct link to a video
  79. {
  80. 'url': 'http://media.w3.org/2010/05/sintel/trailer.mp4',
  81. 'md5': '67d406c2bcb6af27fa886f31aa934bbe',
  82. 'info_dict': {
  83. 'id': 'trailer',
  84. 'ext': 'mp4',
  85. 'title': 'trailer',
  86. 'upload_date': '20100513',
  87. }
  88. },
  89. # ooyala video
  90. {
  91. 'url': 'http://www.rollingstone.com/music/videos/norwegian-dj-cashmere-cat-goes-spartan-on-with-me-premiere-20131219',
  92. 'md5': '5644c6ca5d5782c1d0d350dad9bd840c',
  93. 'info_dict': {
  94. 'id': 'BwY2RxaTrTkslxOfcan0UCf0YqyvWysJ',
  95. 'ext': 'mp4',
  96. 'title': '2cc213299525360.mov', # that's what we get
  97. },
  98. },
  99. # second style of embedded ooyala videos
  100. {
  101. 'url': 'http://www.smh.com.au/tv/business/show/financial-review-sunday/behind-the-scenes-financial-review-sunday--4350201.html',
  102. 'info_dict': {
  103. 'id': '13djJjYjptA1XpPx8r9kuzPyj3UZH0Uk',
  104. 'ext': 'mp4',
  105. 'title': 'Behind-the-scenes: Financial Review Sunday ',
  106. 'description': 'Step inside Channel Nine studios for an exclusive tour of its upcoming financial business show.',
  107. },
  108. 'params': {
  109. # m3u8 download
  110. 'skip_download': True,
  111. },
  112. },
  113. # google redirect
  114. {
  115. 'url': 'http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCUQtwIwAA&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DcmQHVoWB5FY&ei=F-sNU-LLCaXk4QT52ICQBQ&usg=AFQjCNEw4hL29zgOohLXvpJ-Bdh2bils1Q&bvm=bv.61965928,d.bGE',
  116. 'info_dict': {
  117. 'id': 'cmQHVoWB5FY',
  118. 'ext': 'mp4',
  119. 'upload_date': '20130224',
  120. 'uploader_id': 'TheVerge',
  121. 'description': 'Chris Ziegler takes a look at the Alcatel OneTouch Fire and the ZTE Open; two of the first Firefox OS handsets to be officially announced.',
  122. 'uploader': 'The Verge',
  123. 'title': 'First Firefox OS phones side-by-side',
  124. },
  125. 'params': {
  126. 'skip_download': False,
  127. }
  128. },
  129. # embed.ly video
  130. {
  131. 'url': 'http://www.tested.com/science/weird/460206-tested-grinding-coffee-2000-frames-second/',
  132. 'info_dict': {
  133. 'id': '9ODmcdjQcHQ',
  134. 'ext': 'mp4',
  135. 'title': 'Tested: Grinding Coffee at 2000 Frames Per Second',
  136. 'upload_date': '20140225',
  137. 'description': 'md5:06a40fbf30b220468f1e0957c0f558ff',
  138. 'uploader': 'Tested',
  139. 'uploader_id': 'testedcom',
  140. },
  141. # No need to test YoutubeIE here
  142. 'params': {
  143. 'skip_download': True,
  144. },
  145. },
  146. # funnyordie embed
  147. {
  148. 'url': 'http://www.theguardian.com/world/2014/mar/11/obama-zach-galifianakis-between-two-ferns',
  149. 'md5': '7cf780be104d40fea7bae52eed4a470e',
  150. 'info_dict': {
  151. 'id': '18e820ec3f',
  152. 'ext': 'mp4',
  153. 'title': 'Between Two Ferns with Zach Galifianakis: President Barack Obama',
  154. 'description': 'Episode 18: President Barack Obama sits down with Zach Galifianakis for his most memorable interview yet.',
  155. },
  156. },
  157. # RUTV embed
  158. {
  159. 'url': 'http://www.rg.ru/2014/03/15/reg-dfo/anklav-anons.html',
  160. 'info_dict': {
  161. 'id': '776940',
  162. 'ext': 'mp4',
  163. 'title': 'Охотское море стало целиком российским',
  164. 'description': 'md5:5ed62483b14663e2a95ebbe115eb8f43',
  165. },
  166. 'params': {
  167. # m3u8 download
  168. 'skip_download': True,
  169. },
  170. },
  171. # Embedded TED video
  172. {
  173. 'url': 'http://en.support.wordpress.com/videos/ted-talks/',
  174. 'md5': 'deeeabcc1085eb2ba205474e7235a3d5',
  175. 'info_dict': {
  176. 'id': '981',
  177. 'ext': 'mp4',
  178. 'title': 'My web playroom',
  179. 'uploader': 'Ze Frank',
  180. 'description': 'md5:ddb2a40ecd6b6a147e400e535874947b',
  181. }
  182. }
  183. ]
  184. def report_download_webpage(self, video_id):
  185. """Report webpage download."""
  186. if not self._downloader.params.get('test', False):
  187. self._downloader.report_warning('Falling back on generic information extractor.')
  188. super(GenericIE, self).report_download_webpage(video_id)
  189. def report_following_redirect(self, new_url):
  190. """Report information extraction."""
  191. self._downloader.to_screen('[redirect] Following redirect to %s' % new_url)
  192. def _send_head(self, url):
  193. """Check if it is a redirect, like url shorteners, in case return the new url."""
  194. class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
  195. """
  196. Subclass the HTTPRedirectHandler to make it use our
  197. HEADRequest also on the redirected URL
  198. """
  199. def redirect_request(self, req, fp, code, msg, headers, newurl):
  200. if code in (301, 302, 303, 307):
  201. newurl = newurl.replace(' ', '%20')
  202. newheaders = dict((k,v) for k,v in req.headers.items()
  203. if k.lower() not in ("content-length", "content-type"))
  204. try:
  205. # This function was deprecated in python 3.3 and removed in 3.4
  206. origin_req_host = req.get_origin_req_host()
  207. except AttributeError:
  208. origin_req_host = req.origin_req_host
  209. return HEADRequest(newurl,
  210. headers=newheaders,
  211. origin_req_host=origin_req_host,
  212. unverifiable=True)
  213. else:
  214. raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp)
  215. class HTTPMethodFallback(compat_urllib_request.BaseHandler):
  216. """
  217. Fallback to GET if HEAD is not allowed (405 HTTP error)
  218. """
  219. def http_error_405(self, req, fp, code, msg, headers):
  220. fp.read()
  221. fp.close()
  222. newheaders = dict((k,v) for k,v in req.headers.items()
  223. if k.lower() not in ("content-length", "content-type"))
  224. return self.parent.open(compat_urllib_request.Request(req.get_full_url(),
  225. headers=newheaders,
  226. origin_req_host=req.get_origin_req_host(),
  227. unverifiable=True))
  228. # Build our opener
  229. opener = compat_urllib_request.OpenerDirector()
  230. for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler,
  231. HTTPMethodFallback, HEADRedirectHandler,
  232. compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]:
  233. opener.add_handler(handler())
  234. response = opener.open(HEADRequest(url))
  235. if response is None:
  236. raise ExtractorError('Invalid URL protocol')
  237. return response
  238. def _extract_rss(self, url, video_id, doc):
  239. playlist_title = doc.find('./channel/title').text
  240. playlist_desc_el = doc.find('./channel/description')
  241. playlist_desc = None if playlist_desc_el is None else playlist_desc_el.text
  242. entries = [{
  243. '_type': 'url',
  244. 'url': e.find('link').text,
  245. 'title': e.find('title').text,
  246. } for e in doc.findall('./channel/item')]
  247. return {
  248. '_type': 'playlist',
  249. 'id': url,
  250. 'title': playlist_title,
  251. 'description': playlist_desc,
  252. 'entries': entries,
  253. }
  254. def _real_extract(self, url):
  255. parsed_url = compat_urlparse.urlparse(url)
  256. if not parsed_url.scheme:
  257. default_search = self._downloader.params.get('default_search')
  258. if default_search is None:
  259. default_search = 'auto'
  260. if default_search == 'auto':
  261. if '/' in url:
  262. self._downloader.report_warning('The url doesn\'t specify the protocol, trying with http')
  263. return self.url_result('http://' + url)
  264. else:
  265. return self.url_result('ytsearch:' + url)
  266. else:
  267. assert ':' in default_search
  268. return self.url_result(default_search + url)
  269. video_id = os.path.splitext(url.rstrip('/').split('/')[-1])[0]
  270. self.to_screen('%s: Requesting header' % video_id)
  271. try:
  272. response = self._send_head(url)
  273. # Check for redirect
  274. new_url = response.geturl()
  275. if url != new_url:
  276. self.report_following_redirect(new_url)
  277. return self.url_result(new_url)
  278. # Check for direct link to a video
  279. content_type = response.headers.get('Content-Type', '')
  280. m = re.match(r'^(?P<type>audio|video|application(?=/ogg$))/(?P<format_id>.+)$', content_type)
  281. if m:
  282. upload_date = response.headers.get('Last-Modified')
  283. if upload_date:
  284. upload_date = unified_strdate(upload_date)
  285. return {
  286. 'id': video_id,
  287. 'title': os.path.splitext(url_basename(url))[0],
  288. 'formats': [{
  289. 'format_id': m.group('format_id'),
  290. 'url': url,
  291. 'vcodec': 'none' if m.group('type') == 'audio' else None
  292. }],
  293. 'upload_date': upload_date,
  294. }
  295. except compat_urllib_error.HTTPError:
  296. # This may be a stupid server that doesn't like HEAD, our UA, or so
  297. pass
  298. try:
  299. webpage = self._download_webpage(url, video_id)
  300. except ValueError:
  301. # since this is the last-resort InfoExtractor, if
  302. # this error is thrown, it'll be thrown here
  303. raise ExtractorError('Failed to download URL: %s' % url)
  304. self.report_extraction(video_id)
  305. # Is it an RSS feed?
  306. try:
  307. doc = parse_xml(webpage)
  308. if doc.tag == 'rss':
  309. return self._extract_rss(url, video_id, doc)
  310. except compat_xml_parse_error:
  311. pass
  312. # it's tempting to parse this further, but you would
  313. # have to take into account all the variations like
  314. # Video Title - Site Name
  315. # Site Name | Video Title
  316. # Video Title - Tagline | Site Name
  317. # and so on and so forth; it's just not practical
  318. video_title = self._html_search_regex(
  319. r'(?s)<title>(.*?)</title>', webpage, 'video title',
  320. default='video')
  321. # video uploader is domain name
  322. video_uploader = self._search_regex(
  323. r'^(?:https?://)?([^/]*)/.*', url, 'video uploader')
  324. # Look for BrightCove:
  325. bc_urls = BrightcoveIE._extract_brightcove_urls(webpage)
  326. if bc_urls:
  327. self.to_screen('Brightcove video detected.')
  328. entries = [{
  329. '_type': 'url',
  330. 'url': smuggle_url(bc_url, {'Referer': url}),
  331. 'ie_key': 'Brightcove'
  332. } for bc_url in bc_urls]
  333. return {
  334. '_type': 'playlist',
  335. 'title': video_title,
  336. 'id': video_id,
  337. 'entries': entries,
  338. }
  339. # Look for embedded (iframe) Vimeo player
  340. mobj = re.search(
  341. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.vimeo\.com/video/.+?)\1', webpage)
  342. if mobj:
  343. player_url = unescapeHTML(mobj.group('url'))
  344. surl = smuggle_url(player_url, {'Referer': url})
  345. return self.url_result(surl, 'Vimeo')
  346. # Look for embedded (swf embed) Vimeo player
  347. mobj = re.search(
  348. r'<embed[^>]+?src="(https?://(?:www\.)?vimeo\.com/moogaloop\.swf.+?)"', webpage)
  349. if mobj:
  350. return self.url_result(mobj.group(1), 'Vimeo')
  351. # Look for embedded YouTube player
  352. matches = re.findall(r'''(?x)
  353. (?:<iframe[^>]+?src=|embedSWF\(\s*)
  354. (["\'])(?P<url>(?:https?:)?//(?:www\.)?youtube\.com/
  355. (?:embed|v)/.+?)
  356. \1''', webpage)
  357. if matches:
  358. urlrs = [self.url_result(unescapeHTML(tuppl[1]), 'Youtube')
  359. for tuppl in matches]
  360. return self.playlist_result(
  361. urlrs, playlist_id=video_id, playlist_title=video_title)
  362. # Look for embedded Dailymotion player
  363. matches = re.findall(
  364. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/.+?)\1', webpage)
  365. if matches:
  366. urlrs = [self.url_result(unescapeHTML(tuppl[1]), 'Dailymotion')
  367. for tuppl in matches]
  368. return self.playlist_result(
  369. urlrs, playlist_id=video_id, playlist_title=video_title)
  370. # Look for embedded Wistia player
  371. match = re.search(
  372. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage)
  373. if match:
  374. return {
  375. '_type': 'url_transparent',
  376. 'url': unescapeHTML(match.group('url')),
  377. 'ie_key': 'Wistia',
  378. 'uploader': video_uploader,
  379. 'title': video_title,
  380. 'id': video_id,
  381. }
  382. # Look for embedded blip.tv player
  383. mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
  384. if mobj:
  385. return self.url_result('http://blip.tv/a/a-'+mobj.group(1), 'BlipTV')
  386. mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9]+)', webpage)
  387. if mobj:
  388. return self.url_result(mobj.group(1), 'BlipTV')
  389. # Look for Bandcamp pages with custom domain
  390. mobj = re.search(r'<meta property="og:url"[^>]*?content="(.*?bandcamp\.com.*?)"', webpage)
  391. if mobj is not None:
  392. burl = unescapeHTML(mobj.group(1))
  393. # Don't set the extractor because it can be a track url or an album
  394. return self.url_result(burl)
  395. # Look for embedded Vevo player
  396. mobj = re.search(
  397. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:cache\.)?vevo\.com/.+?)\1', webpage)
  398. if mobj is not None:
  399. return self.url_result(mobj.group('url'))
  400. # Look for Ooyala videos
  401. mobj = (re.search(r'player.ooyala.com/[^"?]+\?[^"]*?(?:embedCode|ec)=(?P<ec>[^"&]+)', webpage) or
  402. re.search(r'OO.Player.create\([\'"].*?[\'"],\s*[\'"](?P<ec>.{32})[\'"]', webpage))
  403. if mobj is not None:
  404. return OoyalaIE._build_url_result(mobj.group('ec'))
  405. # Look for Aparat videos
  406. mobj = re.search(r'<iframe src="(http://www\.aparat\.com/video/[^"]+)"', webpage)
  407. if mobj is not None:
  408. return self.url_result(mobj.group(1), 'Aparat')
  409. # Look for MPORA videos
  410. mobj = re.search(r'<iframe .*?src="(http://mpora\.(?:com|de)/videos/[^"]+)"', webpage)
  411. if mobj is not None:
  412. return self.url_result(mobj.group(1), 'Mpora')
  413. # Look for embedded NovaMov player
  414. mobj = re.search(
  415. r'<iframe[^>]+?src=(["\'])(?P<url>http://(?:(?:embed|www)\.)?novamov\.com/embed\.php.+?)\1', webpage)
  416. if mobj is not None:
  417. return self.url_result(mobj.group('url'), 'NovaMov')
  418. # Look for embedded NowVideo player
  419. mobj = re.search(
  420. r'<iframe[^>]+?src=(["\'])(?P<url>http://(?:(?:embed|www)\.)?nowvideo\.(?:ch|sx|eu)/embed\.php.+?)\1', webpage)
  421. if mobj is not None:
  422. return self.url_result(mobj.group('url'), 'NowVideo')
  423. # Look for embedded Facebook player
  424. mobj = re.search(
  425. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  426. if mobj is not None:
  427. return self.url_result(mobj.group('url'), 'Facebook')
  428. # Look for embedded VK player
  429. mobj = re.search(r'<iframe[^>]+?src=(["\'])(?P<url>https?://vk\.com/video_ext\.php.+?)\1', webpage)
  430. if mobj is not None:
  431. return self.url_result(mobj.group('url'), 'VK')
  432. # Look for embedded Huffington Post player
  433. mobj = re.search(
  434. r'<iframe[^>]+?src=(["\'])(?P<url>https?://embed\.live\.huffingtonpost\.com/.+?)\1', webpage)
  435. if mobj is not None:
  436. return self.url_result(mobj.group('url'), 'HuffPost')
  437. # Look for embed.ly
  438. mobj = re.search(r'class=["\']embedly-card["\'][^>]href=["\'](?P<url>[^"\']+)', webpage)
  439. if mobj is not None:
  440. return self.url_result(mobj.group('url'))
  441. mobj = re.search(r'class=["\']embedly-embed["\'][^>]src=["\'][^"\']*url=(?P<url>[^&]+)', webpage)
  442. if mobj is not None:
  443. return self.url_result(compat_urllib_parse.unquote(mobj.group('url')))
  444. # Look for funnyordie embed
  445. matches = re.findall(r'<iframe[^>]+?src="(https?://(?:www\.)?funnyordie\.com/embed/[^"]+)"', webpage)
  446. if matches:
  447. urlrs = [self.url_result(unescapeHTML(eurl), 'FunnyOrDie')
  448. for eurl in matches]
  449. return self.playlist_result(
  450. urlrs, playlist_id=video_id, playlist_title=video_title)
  451. # Look for embedded RUTV player
  452. rutv_url = RUTVIE._extract_url(webpage)
  453. if rutv_url:
  454. return self.url_result(rutv_url, 'RUTV')
  455. # Start with something easy: JW Player in SWFObject
  456. mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
  457. if mobj is None:
  458. # Look for gorilla-vid style embedding
  459. mobj = re.search(r'(?s)(?:jw_plugins|JWPlayerOptions).*?file\s*:\s*["\'](.*?)["\']', webpage)
  460. if mobj is None:
  461. # Broaden the search a little bit
  462. mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
  463. if mobj is None:
  464. # Broaden the search a little bit: JWPlayer JS loader
  465. mobj = re.search(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http(?![^\'"]+\.[0-9]+[\'"])[^\'"]+)["\']', webpage)
  466. # Look for embedded TED player
  467. mobj = re.search(
  468. r'<iframe[^>]+?src=(["\'])(?P<url>http://embed\.ted\.com/.+?)\1', webpage)
  469. if mobj is not None:
  470. return self.url_result(mobj.group('url'), 'TED')
  471. if mobj is None:
  472. # Try to find twitter cards info
  473. mobj = re.search(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage)
  474. if mobj is None:
  475. # We look for Open Graph info:
  476. # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am)
  477. m_video_type = re.search(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage)
  478. # We only look in og:video if the MIME type is a video, don't try if it's a Flash player:
  479. if m_video_type is not None:
  480. mobj = re.search(r'<meta.*?property="og:video".*?content="(.*?)"', webpage)
  481. if mobj is None:
  482. # HTML5 video
  483. mobj = re.search(r'<video[^<]*(?:>.*?<source.*?)? src="([^"]+)"', webpage, flags=re.DOTALL)
  484. if mobj is None:
  485. mobj = re.search(
  486. r'(?i)<meta\s+(?=(?:[a-z-]+="[^"]+"\s+)*http-equiv="refresh")'
  487. r'(?:[a-z-]+="[^"]+"\s+)*?content="[0-9]{,2};url=\'([^\']+)\'"',
  488. webpage)
  489. if mobj:
  490. new_url = mobj.group(1)
  491. self.report_following_redirect(new_url)
  492. return {
  493. '_type': 'url',
  494. 'url': new_url,
  495. }
  496. if mobj is None:
  497. raise ExtractorError('Unsupported URL: %s' % url)
  498. # It's possible that one of the regexes
  499. # matched, but returned an empty group:
  500. if mobj.group(1) is None:
  501. raise ExtractorError('Did not find a valid video URL at %s' % url)
  502. video_url = mobj.group(1)
  503. video_url = compat_urlparse.urljoin(url, video_url)
  504. video_id = compat_urllib_parse.unquote(os.path.basename(video_url))
  505. # Sometimes, jwplayer extraction will result in a YouTube URL
  506. if YoutubeIE.suitable(video_url):
  507. return self.url_result(video_url, 'Youtube')
  508. # here's a fun little line of code for you:
  509. video_id = os.path.splitext(video_id)[0]
  510. return {
  511. 'id': video_id,
  512. 'url': video_url,
  513. 'uploader': video_uploader,
  514. 'title': video_title,
  515. }