generic.py 26 KB

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