You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

650 lines
27 KiB

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