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.

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