generic.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. 'md5': '85b90ccc9d73b4acd9138d3af4c27f89',
  33. 'info_dict': {
  34. 'id': '13601338388002',
  35. 'ext': 'mp4',
  36. 'uploader': 'www.hodiho.fr',
  37. 'title': 'R\u00e9gis plante sa Jeep',
  38. }
  39. },
  40. # bandcamp page with custom domain
  41. {
  42. 'add_ie': ['Bandcamp'],
  43. 'url': 'http://bronyrock.com/track/the-pony-mash',
  44. 'info_dict': {
  45. 'id': '3235767654',
  46. 'ext': 'mp3',
  47. 'title': 'The Pony Mash',
  48. 'uploader': 'M_Pallante',
  49. },
  50. 'skip': 'There is a limit of 200 free downloads / month for the test song',
  51. },
  52. # embedded brightcove video
  53. # it also tests brightcove videos that need to set the 'Referer' in the
  54. # http requests
  55. {
  56. 'add_ie': ['Brightcove'],
  57. 'url': 'http://www.bfmtv.com/video/bfmbusiness/cours-bourse/cours-bourse-l-analyse-technique-154522/',
  58. 'info_dict': {
  59. 'id': '2765128793001',
  60. 'ext': 'mp4',
  61. 'title': 'Le cours de bourse : l’analyse technique',
  62. 'description': 'md5:7e9ad046e968cb2d1114004aba466fd9',
  63. 'uploader': 'BFM BUSINESS',
  64. },
  65. 'params': {
  66. 'skip_download': True,
  67. },
  68. },
  69. {
  70. # https://github.com/rg3/youtube-dl/issues/2253
  71. 'url': 'http://bcove.me/i6nfkrc3',
  72. 'md5': '0ba9446db037002366bab3b3eb30c88c',
  73. 'info_dict': {
  74. 'id': '3101154703001',
  75. 'ext': 'mp4',
  76. 'title': 'Still no power',
  77. 'uploader': 'thestar.com',
  78. '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.',
  79. },
  80. 'add_ie': ['Brightcove'],
  81. },
  82. {
  83. 'url': 'http://www.championat.com/video/football/v/87/87499.html',
  84. 'md5': 'fb973ecf6e4a78a67453647444222983',
  85. 'info_dict': {
  86. 'id': '3414141473001',
  87. 'ext': 'mp4',
  88. 'title': 'Видео. Удаление Дзагоева (ЦСКА)',
  89. 'description': 'Онлайн-трансляция матча ЦСКА - "Волга"',
  90. 'uploader': 'Championat',
  91. },
  92. },
  93. # Direct link to a video
  94. {
  95. 'url': 'http://media.w3.org/2010/05/sintel/trailer.mp4',
  96. 'md5': '67d406c2bcb6af27fa886f31aa934bbe',
  97. 'info_dict': {
  98. 'id': 'trailer',
  99. 'ext': 'mp4',
  100. 'title': 'trailer',
  101. 'upload_date': '20100513',
  102. }
  103. },
  104. # ooyala video
  105. {
  106. 'url': 'http://www.rollingstone.com/music/videos/norwegian-dj-cashmere-cat-goes-spartan-on-with-me-premiere-20131219',
  107. 'md5': '5644c6ca5d5782c1d0d350dad9bd840c',
  108. 'info_dict': {
  109. 'id': 'BwY2RxaTrTkslxOfcan0UCf0YqyvWysJ',
  110. 'ext': 'mp4',
  111. 'title': '2cc213299525360.mov', # that's what we get
  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. # Embeded Ustream video
  185. {
  186. 'url': 'http://www.american.edu/spa/pti/nsa-privacy-janus-2014.cfm',
  187. 'md5': '27b99cdb639c9b12a79bca876a073417',
  188. 'info_dict': {
  189. 'id': '45734260',
  190. 'ext': 'flv',
  191. 'uploader': 'AU SPA: The NSA and Privacy',
  192. 'title': 'NSA and Privacy Forum Debate featuring General Hayden and Barton Gellman'
  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. # Condé Nast embed
  237. {
  238. 'url': 'http://www.wired.com/2014/04/honda-asimo/',
  239. 'md5': 'ba0dfe966fa007657bd1443ee672db0f',
  240. 'info_dict': {
  241. 'id': '53501be369702d3275860000',
  242. 'ext': 'mp4',
  243. 'title': 'Honda’s New Asimo Robot Is More Human Than Ever',
  244. }
  245. },
  246. # Dailymotion embed
  247. {
  248. 'url': 'http://www.spi0n.com/zap-spi0n-com-n216/',
  249. 'md5': '441aeeb82eb72c422c7f14ec533999cd',
  250. 'info_dict': {
  251. 'id': 'k2mm4bCdJ6CQ2i7c8o2',
  252. 'ext': 'mp4',
  253. 'title': 'Le Zap de Spi0n n°216 - Zapping du Web',
  254. 'uploader': 'Spi0n',
  255. },
  256. 'add_ie': ['Dailymotion'],
  257. },
  258. # YouTube embed via <data-embed-url="">
  259. {
  260. 'url': 'https://play.google.com/store/apps/details?id=com.gameloft.android.ANMP.GloftA8HM',
  261. 'md5': 'c267b1ab6d736057d64babaa37e07a66',
  262. 'info_dict': {
  263. 'id': 'Ybd-qmqYYpA',
  264. 'ext': 'mp4',
  265. 'title': 'Asphalt 8: Airborne - Chinese Great Wall - Android Game Trailer',
  266. 'uploader': 'gameloftandroid',
  267. 'uploader_id': 'gameloftandroid',
  268. 'upload_date': '20140321',
  269. 'description': 'md5:9c6dca5dd75b7131ce482ccf080749d6'
  270. }
  271. }
  272. ]
  273. def report_download_webpage(self, video_id):
  274. """Report webpage download."""
  275. if not self._downloader.params.get('test', False):
  276. self._downloader.report_warning('Falling back on generic information extractor.')
  277. super(GenericIE, self).report_download_webpage(video_id)
  278. def report_following_redirect(self, new_url):
  279. """Report information extraction."""
  280. self._downloader.to_screen('[redirect] Following redirect to %s' % new_url)
  281. def _send_head(self, url):
  282. """Check if it is a redirect, like url shorteners, in case return the new url."""
  283. class HEADRedirectHandler(compat_urllib_request.HTTPRedirectHandler):
  284. """
  285. Subclass the HTTPRedirectHandler to make it use our
  286. HEADRequest also on the redirected URL
  287. """
  288. def redirect_request(self, req, fp, code, msg, headers, newurl):
  289. if code in (301, 302, 303, 307):
  290. newurl = newurl.replace(' ', '%20')
  291. newheaders = dict((k,v) for k,v in req.headers.items()
  292. if k.lower() not in ("content-length", "content-type"))
  293. try:
  294. # This function was deprecated in python 3.3 and removed in 3.4
  295. origin_req_host = req.get_origin_req_host()
  296. except AttributeError:
  297. origin_req_host = req.origin_req_host
  298. return HEADRequest(newurl,
  299. headers=newheaders,
  300. origin_req_host=origin_req_host,
  301. unverifiable=True)
  302. else:
  303. raise compat_urllib_error.HTTPError(req.get_full_url(), code, msg, headers, fp)
  304. class HTTPMethodFallback(compat_urllib_request.BaseHandler):
  305. """
  306. Fallback to GET if HEAD is not allowed (405 HTTP error)
  307. """
  308. def http_error_405(self, req, fp, code, msg, headers):
  309. fp.read()
  310. fp.close()
  311. newheaders = dict((k,v) for k,v in req.headers.items()
  312. if k.lower() not in ("content-length", "content-type"))
  313. return self.parent.open(compat_urllib_request.Request(req.get_full_url(),
  314. headers=newheaders,
  315. origin_req_host=req.get_origin_req_host(),
  316. unverifiable=True))
  317. # Build our opener
  318. opener = compat_urllib_request.OpenerDirector()
  319. for handler in [compat_urllib_request.HTTPHandler, compat_urllib_request.HTTPDefaultErrorHandler,
  320. HTTPMethodFallback, HEADRedirectHandler,
  321. compat_urllib_request.HTTPErrorProcessor, compat_urllib_request.HTTPSHandler]:
  322. opener.add_handler(handler())
  323. response = opener.open(HEADRequest(url))
  324. if response is None:
  325. raise ExtractorError('Invalid URL protocol')
  326. return response
  327. def _extract_rss(self, url, video_id, doc):
  328. playlist_title = doc.find('./channel/title').text
  329. playlist_desc_el = doc.find('./channel/description')
  330. playlist_desc = None if playlist_desc_el is None else playlist_desc_el.text
  331. entries = [{
  332. '_type': 'url',
  333. 'url': e.find('link').text,
  334. 'title': e.find('title').text,
  335. } for e in doc.findall('./channel/item')]
  336. return {
  337. '_type': 'playlist',
  338. 'id': url,
  339. 'title': playlist_title,
  340. 'description': playlist_desc,
  341. 'entries': entries,
  342. }
  343. def _real_extract(self, url):
  344. if url.startswith('//'):
  345. return {
  346. '_type': 'url',
  347. 'url': self.http_scheme() + url,
  348. }
  349. parsed_url = compat_urlparse.urlparse(url)
  350. if not parsed_url.scheme:
  351. default_search = self._downloader.params.get('default_search')
  352. if default_search is None:
  353. default_search = 'auto_warning'
  354. if default_search in ('auto', 'auto_warning'):
  355. if '/' in url:
  356. self._downloader.report_warning('The url doesn\'t specify the protocol, trying with http')
  357. return self.url_result('http://' + url)
  358. else:
  359. if default_search == 'auto_warning':
  360. self._downloader.report_warning(
  361. 'Falling back to youtube search for %s . Set --default-search to "auto" to suppress this warning.' % url)
  362. return self.url_result('ytsearch:' + url)
  363. else:
  364. assert ':' in default_search
  365. return self.url_result(default_search + url)
  366. video_id = os.path.splitext(url.rstrip('/').split('/')[-1])[0]
  367. self.to_screen('%s: Requesting header' % video_id)
  368. try:
  369. response = self._send_head(url)
  370. # Check for redirect
  371. new_url = response.geturl()
  372. if url != new_url:
  373. self.report_following_redirect(new_url)
  374. return self.url_result(new_url)
  375. # Check for direct link to a video
  376. content_type = response.headers.get('Content-Type', '')
  377. m = re.match(r'^(?P<type>audio|video|application(?=/ogg$))/(?P<format_id>.+)$', content_type)
  378. if m:
  379. upload_date = response.headers.get('Last-Modified')
  380. if upload_date:
  381. upload_date = unified_strdate(upload_date)
  382. return {
  383. 'id': video_id,
  384. 'title': os.path.splitext(url_basename(url))[0],
  385. 'formats': [{
  386. 'format_id': m.group('format_id'),
  387. 'url': url,
  388. 'vcodec': 'none' if m.group('type') == 'audio' else None
  389. }],
  390. 'upload_date': upload_date,
  391. }
  392. except compat_urllib_error.HTTPError:
  393. # This may be a stupid server that doesn't like HEAD, our UA, or so
  394. pass
  395. try:
  396. webpage = self._download_webpage(url, video_id)
  397. except ValueError:
  398. # since this is the last-resort InfoExtractor, if
  399. # this error is thrown, it'll be thrown here
  400. raise ExtractorError('Failed to download URL: %s' % url)
  401. self.report_extraction(video_id)
  402. # Is it an RSS feed?
  403. try:
  404. doc = parse_xml(webpage)
  405. if doc.tag == 'rss':
  406. return self._extract_rss(url, video_id, doc)
  407. except compat_xml_parse_error:
  408. pass
  409. # Sometimes embedded video player is hidden behind percent encoding
  410. # (e.g. https://github.com/rg3/youtube-dl/issues/2448)
  411. # Unescaping the whole page allows to handle those cases in a generic way
  412. webpage = compat_urllib_parse.unquote(webpage)
  413. # it's tempting to parse this further, but you would
  414. # have to take into account all the variations like
  415. # Video Title - Site Name
  416. # Site Name | Video Title
  417. # Video Title - Tagline | Site Name
  418. # and so on and so forth; it's just not practical
  419. video_title = self._html_search_regex(
  420. r'(?s)<title>(.*?)</title>', webpage, 'video title',
  421. default='video')
  422. # video uploader is domain name
  423. video_uploader = self._search_regex(
  424. r'^(?:https?://)?([^/]*)/.*', url, 'video uploader')
  425. # Look for BrightCove:
  426. bc_urls = BrightcoveIE._extract_brightcove_urls(webpage)
  427. if bc_urls:
  428. self.to_screen('Brightcove video detected.')
  429. entries = [{
  430. '_type': 'url',
  431. 'url': smuggle_url(bc_url, {'Referer': url}),
  432. 'ie_key': 'Brightcove'
  433. } for bc_url in bc_urls]
  434. return {
  435. '_type': 'playlist',
  436. 'title': video_title,
  437. 'id': video_id,
  438. 'entries': entries,
  439. }
  440. # Look for embedded (iframe) Vimeo player
  441. mobj = re.search(
  442. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.vimeo\.com/video/.+?)\1', webpage)
  443. if mobj:
  444. player_url = unescapeHTML(mobj.group('url'))
  445. surl = smuggle_url(player_url, {'Referer': url})
  446. return self.url_result(surl, 'Vimeo')
  447. # Look for embedded (swf embed) Vimeo player
  448. mobj = re.search(
  449. r'<embed[^>]+?src="(https?://(?:www\.)?vimeo\.com/moogaloop\.swf.+?)"', webpage)
  450. if mobj:
  451. return self.url_result(mobj.group(1), 'Vimeo')
  452. # Look for embedded YouTube player
  453. matches = re.findall(r'''(?x)
  454. (?:<iframe[^>]+?src=|data-video-url=|embedSWF\(\s*)
  455. (["\'])(?P<url>(?:https?:)?//(?:www\.)?youtube\.com/
  456. (?:embed|v)/.+?)
  457. \1''', webpage)
  458. if matches:
  459. urlrs = [self.url_result(unescapeHTML(tuppl[1]), 'Youtube')
  460. for tuppl in matches]
  461. # First, ensure we have a duplicate free list of entries
  462. seen = set()
  463. new_list = []
  464. theurl = tuple(url.items())
  465. if theurl not in seen:
  466. seen.add(theurl)
  467. new_list.append(url)
  468. urlrs = new_list
  469. return self.playlist_result(
  470. urlrs, playlist_id=video_id, playlist_title=video_title)
  471. # Look for embedded Dailymotion player
  472. matches = re.findall(
  473. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/.+?)\1', webpage)
  474. if matches:
  475. urlrs = [self.url_result(unescapeHTML(tuppl[1]))
  476. for tuppl in matches]
  477. # First, ensure we have a duplicate free list of entries
  478. seen = set()
  479. new_list = []
  480. theurl = tuple(url.items())
  481. if theurl not in seen:
  482. seen.add(theurl)
  483. new_list.append(url)
  484. urlrs = new_list
  485. return self.playlist_result(
  486. urlrs, playlist_id=video_id, playlist_title=video_title)
  487. # Look for embedded Wistia player
  488. match = re.search(
  489. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage)
  490. if match:
  491. return {
  492. '_type': 'url_transparent',
  493. 'url': unescapeHTML(match.group('url')),
  494. 'ie_key': 'Wistia',
  495. 'uploader': video_uploader,
  496. 'title': video_title,
  497. 'id': video_id,
  498. }
  499. # Look for embedded blip.tv player
  500. mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
  501. if mobj:
  502. return self.url_result('http://blip.tv/a/a-'+mobj.group(1), 'BlipTV')
  503. mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9]+)', webpage)
  504. if mobj:
  505. return self.url_result(mobj.group(1), 'BlipTV')
  506. # Look for embedded condenast player
  507. matches = re.findall(
  508. r'<iframe\s+(?:[a-zA-Z-]+="[^"]+"\s+)*?src="(https?://player\.cnevids\.com/embed/[^"]+")',
  509. webpage)
  510. if matches:
  511. return {
  512. '_type': 'playlist',
  513. 'entries': [{
  514. '_type': 'url',
  515. 'ie_key': 'CondeNast',
  516. 'url': ma,
  517. } for ma in matches],
  518. 'title': video_title,
  519. 'id': video_id,
  520. }
  521. # Look for Bandcamp pages with custom domain
  522. mobj = re.search(r'<meta property="og:url"[^>]*?content="(.*?bandcamp\.com.*?)"', webpage)
  523. if mobj is not None:
  524. burl = unescapeHTML(mobj.group(1))
  525. # Don't set the extractor because it can be a track url or an album
  526. return self.url_result(burl)
  527. # Look for embedded Vevo player
  528. mobj = re.search(
  529. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:cache\.)?vevo\.com/.+?)\1', webpage)
  530. if mobj is not None:
  531. return self.url_result(mobj.group('url'))
  532. # Look for Ooyala videos
  533. mobj = (re.search(r'player.ooyala.com/[^"?]+\?[^"]*?(?:embedCode|ec)=(?P<ec>[^"&]+)', webpage) or
  534. re.search(r'OO.Player.create\([\'"].*?[\'"],\s*[\'"](?P<ec>.{32})[\'"]', webpage))
  535. if mobj is not None:
  536. return OoyalaIE._build_url_result(mobj.group('ec'))
  537. # Look for Aparat videos
  538. mobj = re.search(r'<iframe .*?src="(http://www\.aparat\.com/video/[^"]+)"', webpage)
  539. if mobj is not None:
  540. return self.url_result(mobj.group(1), 'Aparat')
  541. # Look for MPORA videos
  542. mobj = re.search(r'<iframe .*?src="(http://mpora\.(?:com|de)/videos/[^"]+)"', webpage)
  543. if mobj is not None:
  544. return self.url_result(mobj.group(1), 'Mpora')
  545. # Look for embedded NovaMov-based player
  546. mobj = re.search(
  547. r'''(?x)<(?:pagespeed_)?iframe[^>]+?src=(["\'])
  548. (?P<url>http://(?:(?:embed|www)\.)?
  549. (?:novamov\.com|
  550. nowvideo\.(?:ch|sx|eu|at|ag|co)|
  551. videoweed\.(?:es|com)|
  552. movshare\.(?:net|sx|ag)|
  553. divxstage\.(?:eu|net|ch|co|at|ag))
  554. /embed\.php.+?)\1''', webpage)
  555. if mobj is not None:
  556. return self.url_result(mobj.group('url'))
  557. # Look for embedded Facebook player
  558. mobj = re.search(
  559. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  560. if mobj is not None:
  561. return self.url_result(mobj.group('url'), 'Facebook')
  562. # Look for embedded VK player
  563. mobj = re.search(r'<iframe[^>]+?src=(["\'])(?P<url>https?://vk\.com/video_ext\.php.+?)\1', webpage)
  564. if mobj is not None:
  565. return self.url_result(mobj.group('url'), 'VK')
  566. # Look for embedded Huffington Post player
  567. mobj = re.search(
  568. r'<iframe[^>]+?src=(["\'])(?P<url>https?://embed\.live\.huffingtonpost\.com/.+?)\1', webpage)
  569. if mobj is not None:
  570. return self.url_result(mobj.group('url'), 'HuffPost')
  571. # Look for embed.ly
  572. mobj = re.search(r'class=["\']embedly-card["\'][^>]href=["\'](?P<url>[^"\']+)', webpage)
  573. if mobj is not None:
  574. return self.url_result(mobj.group('url'))
  575. mobj = re.search(r'class=["\']embedly-embed["\'][^>]src=["\'][^"\']*url=(?P<url>[^&]+)', webpage)
  576. if mobj is not None:
  577. return self.url_result(compat_urllib_parse.unquote(mobj.group('url')))
  578. # Look for funnyordie embed
  579. matches = re.findall(r'<iframe[^>]+?src="(https?://(?:www\.)?funnyordie\.com/embed/[^"]+)"', webpage)
  580. if matches:
  581. urlrs = [self.url_result(unescapeHTML(eurl), 'FunnyOrDie')
  582. for eurl in matches]
  583. # First, ensure we have a duplicate free list of entries
  584. seen = set()
  585. new_list = []
  586. theurl = tuple(url.items())
  587. if theurl not in seen:
  588. seen.add(theurl)
  589. new_list.append(url)
  590. urlrs = new_list
  591. return self.playlist_result(
  592. urlrs, playlist_id=video_id, playlist_title=video_title)
  593. # Look for embedded RUTV player
  594. rutv_url = RUTVIE._extract_url(webpage)
  595. if rutv_url:
  596. return self.url_result(rutv_url, 'RUTV')
  597. # Look for embedded TED player
  598. mobj = re.search(
  599. r'<iframe[^>]+?src=(["\'])(?P<url>http://embed\.ted\.com/.+?)\1', webpage)
  600. if mobj is not None:
  601. return self.url_result(mobj.group('url'), 'TED')
  602. # Look for embedded Ustream videos
  603. mobj = re.search(
  604. r'<iframe[^>]+?src=(["\'])(?P<url>http://www\.ustream\.tv/embed/.+?)\1', webpage)
  605. if mobj is not None:
  606. return self.url_result(mobj.group('url'), 'Ustream')
  607. # Look for embedded arte.tv player
  608. mobj = re.search(
  609. r'<script [^>]*?src="(?P<url>http://www\.arte\.tv/playerv2/embed[^"]+)"',
  610. webpage)
  611. if mobj is not None:
  612. return self.url_result(mobj.group('url'), 'ArteTVEmbed')
  613. # Look for embedded smotri.com player
  614. smotri_url = SmotriIE._extract_url(webpage)
  615. if smotri_url:
  616. return self.url_result(smotri_url, 'Smotri')
  617. # Look for embeded soundcloud player
  618. mobj = re.search(
  619. r'<iframe src="(?P<url>https?://(?:w\.)?soundcloud\.com/player[^"]+)"',
  620. webpage)
  621. if mobj is not None:
  622. url = unescapeHTML(mobj.group('url'))
  623. return self.url_result(url)
  624. # Start with something easy: JW Player in SWFObject
  625. found = re.findall(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
  626. if not found:
  627. # Look for gorilla-vid style embedding
  628. found = re.findall(r'''(?sx)
  629. (?:
  630. jw_plugins|
  631. JWPlayerOptions|
  632. jwplayer\s*\(\s*["'][^'"]+["']\s*\)\s*\.setup
  633. )
  634. .*?file\s*:\s*["\'](.*?)["\']''', webpage)
  635. if not found:
  636. # Broaden the search a little bit
  637. found = re.findall(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
  638. if not found:
  639. # Broaden the findall a little bit: JWPlayer JS loader
  640. found = re.findall(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http(?![^\'"]+\.[0-9]+[\'"])[^\'"]+)["\']', webpage)
  641. if not found:
  642. # Try to find twitter cards info
  643. found = re.findall(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage)
  644. if not found:
  645. # We look for Open Graph info:
  646. # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am)
  647. m_video_type = re.findall(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage)
  648. # We only look in og:video if the MIME type is a video, don't try if it's a Flash player:
  649. if m_video_type is not None:
  650. found = re.findall(r'<meta.*?property="og:video".*?content="(.*?)"', webpage)
  651. if not found:
  652. # HTML5 video
  653. found = re.findall(r'(?s)<video[^<]*(?:>.*?<source.*?)? src="([^"]+)"', webpage)
  654. if not found:
  655. found = re.search(
  656. r'(?i)<meta\s+(?=(?:[a-z-]+="[^"]+"\s+)*http-equiv="refresh")'
  657. r'(?:[a-z-]+="[^"]+"\s+)*?content="[0-9]{,2};url=\'([^\']+)\'"',
  658. webpage)
  659. if found:
  660. new_url = found.group(1)
  661. self.report_following_redirect(new_url)
  662. return {
  663. '_type': 'url',
  664. 'url': new_url,
  665. }
  666. if not found:
  667. raise ExtractorError('Unsupported URL: %s' % url)
  668. entries = []
  669. for video_url in found:
  670. video_url = compat_urlparse.urljoin(url, video_url)
  671. video_id = compat_urllib_parse.unquote(os.path.basename(video_url))
  672. # Sometimes, jwplayer extraction will result in a YouTube URL
  673. if YoutubeIE.suitable(video_url):
  674. entries.append(self.url_result(video_url, 'Youtube'))
  675. continue
  676. # here's a fun little line of code for you:
  677. video_id = os.path.splitext(video_id)[0]
  678. entries.append({
  679. 'id': video_id,
  680. 'url': video_url,
  681. 'uploader': video_uploader,
  682. 'title': video_title,
  683. })
  684. if len(entries) == 1:
  685. return entries[0]
  686. else:
  687. for num, e in enumerate(entries, start=1):
  688. e['title'] = '%s (%d)' % (e['title'], num)
  689. return {
  690. '_type': 'playlist',
  691. 'entries': entries,
  692. }