generic.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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_parse,
  9. compat_urlparse,
  10. compat_xml_parse_error,
  11. ExtractorError,
  12. float_or_none,
  13. HEADRequest,
  14. orderedSet,
  15. parse_xml,
  16. smuggle_url,
  17. unescapeHTML,
  18. unified_strdate,
  19. unsmuggle_url,
  20. url_basename,
  21. )
  22. from .brightcove import BrightcoveIE
  23. from .ooyala import OoyalaIE
  24. from .rutv import RUTVIE
  25. from .smotri import SmotriIE
  26. class GenericIE(InfoExtractor):
  27. IE_DESC = 'Generic downloader that works on some sites'
  28. _VALID_URL = r'.*'
  29. IE_NAME = 'generic'
  30. _TESTS = [
  31. {
  32. 'url': 'http://www.hodiho.fr/2013/02/regis-plante-sa-jeep.html',
  33. 'md5': '85b90ccc9d73b4acd9138d3af4c27f89',
  34. 'info_dict': {
  35. 'id': '13601338388002',
  36. 'ext': 'mp4',
  37. 'uploader': 'www.hodiho.fr',
  38. 'title': 'R\u00e9gis plante sa Jeep',
  39. }
  40. },
  41. # bandcamp page with custom domain
  42. {
  43. 'add_ie': ['Bandcamp'],
  44. 'url': 'http://bronyrock.com/track/the-pony-mash',
  45. 'info_dict': {
  46. 'id': '3235767654',
  47. 'ext': 'mp3',
  48. 'title': 'The Pony Mash',
  49. 'uploader': 'M_Pallante',
  50. },
  51. 'skip': 'There is a limit of 200 free downloads / month for the test song',
  52. },
  53. # embedded brightcove video
  54. # it also tests brightcove videos that need to set the 'Referer' in the
  55. # http requests
  56. {
  57. 'add_ie': ['Brightcove'],
  58. 'url': 'http://www.bfmtv.com/video/bfmbusiness/cours-bourse/cours-bourse-l-analyse-technique-154522/',
  59. 'info_dict': {
  60. 'id': '2765128793001',
  61. 'ext': 'mp4',
  62. 'title': 'Le cours de bourse : l’analyse technique',
  63. 'description': 'md5:7e9ad046e968cb2d1114004aba466fd9',
  64. 'uploader': 'BFM BUSINESS',
  65. },
  66. 'params': {
  67. 'skip_download': True,
  68. },
  69. },
  70. {
  71. # https://github.com/rg3/youtube-dl/issues/2253
  72. 'url': 'http://bcove.me/i6nfkrc3',
  73. 'md5': '0ba9446db037002366bab3b3eb30c88c',
  74. 'info_dict': {
  75. 'id': '3101154703001',
  76. 'ext': 'mp4',
  77. 'title': 'Still no power',
  78. 'uploader': 'thestar.com',
  79. '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.',
  80. },
  81. 'add_ie': ['Brightcove'],
  82. },
  83. {
  84. 'url': 'http://www.championat.com/video/football/v/87/87499.html',
  85. 'md5': 'fb973ecf6e4a78a67453647444222983',
  86. 'info_dict': {
  87. 'id': '3414141473001',
  88. 'ext': 'mp4',
  89. 'title': 'Видео. Удаление Дзагоева (ЦСКА)',
  90. 'description': 'Онлайн-трансляция матча ЦСКА - "Волга"',
  91. 'uploader': 'Championat',
  92. },
  93. },
  94. # Direct link to a video
  95. {
  96. 'url': 'http://media.w3.org/2010/05/sintel/trailer.mp4',
  97. 'md5': '67d406c2bcb6af27fa886f31aa934bbe',
  98. 'info_dict': {
  99. 'id': 'trailer',
  100. 'ext': 'mp4',
  101. 'title': 'trailer',
  102. 'upload_date': '20100513',
  103. }
  104. },
  105. # ooyala video
  106. {
  107. 'url': 'http://www.rollingstone.com/music/videos/norwegian-dj-cashmere-cat-goes-spartan-on-with-me-premiere-20131219',
  108. 'md5': '5644c6ca5d5782c1d0d350dad9bd840c',
  109. 'info_dict': {
  110. 'id': 'BwY2RxaTrTkslxOfcan0UCf0YqyvWysJ',
  111. 'ext': 'mp4',
  112. 'title': '2cc213299525360.mov', # that's what we get
  113. },
  114. },
  115. # google redirect
  116. {
  117. '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',
  118. 'info_dict': {
  119. 'id': 'cmQHVoWB5FY',
  120. 'ext': 'mp4',
  121. 'upload_date': '20130224',
  122. 'uploader_id': 'TheVerge',
  123. '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.',
  124. 'uploader': 'The Verge',
  125. 'title': 'First Firefox OS phones side-by-side',
  126. },
  127. 'params': {
  128. 'skip_download': False,
  129. }
  130. },
  131. # embed.ly video
  132. {
  133. 'url': 'http://www.tested.com/science/weird/460206-tested-grinding-coffee-2000-frames-second/',
  134. 'info_dict': {
  135. 'id': '9ODmcdjQcHQ',
  136. 'ext': 'mp4',
  137. 'title': 'Tested: Grinding Coffee at 2000 Frames Per Second',
  138. 'upload_date': '20140225',
  139. 'description': 'md5:06a40fbf30b220468f1e0957c0f558ff',
  140. 'uploader': 'Tested',
  141. 'uploader_id': 'testedcom',
  142. },
  143. # No need to test YoutubeIE here
  144. 'params': {
  145. 'skip_download': True,
  146. },
  147. },
  148. # funnyordie embed
  149. {
  150. 'url': 'http://www.theguardian.com/world/2014/mar/11/obama-zach-galifianakis-between-two-ferns',
  151. 'md5': '7cf780be104d40fea7bae52eed4a470e',
  152. 'info_dict': {
  153. 'id': '18e820ec3f',
  154. 'ext': 'mp4',
  155. 'title': 'Between Two Ferns with Zach Galifianakis: President Barack Obama',
  156. 'description': 'Episode 18: President Barack Obama sits down with Zach Galifianakis for his most memorable interview yet.',
  157. },
  158. },
  159. # RUTV embed
  160. {
  161. 'url': 'http://www.rg.ru/2014/03/15/reg-dfo/anklav-anons.html',
  162. 'info_dict': {
  163. 'id': '776940',
  164. 'ext': 'mp4',
  165. 'title': 'Охотское море стало целиком российским',
  166. 'description': 'md5:5ed62483b14663e2a95ebbe115eb8f43',
  167. },
  168. 'params': {
  169. # m3u8 download
  170. 'skip_download': True,
  171. },
  172. },
  173. # Embedded TED video
  174. {
  175. 'url': 'http://en.support.wordpress.com/videos/ted-talks/',
  176. 'md5': 'deeeabcc1085eb2ba205474e7235a3d5',
  177. 'info_dict': {
  178. 'id': '981',
  179. 'ext': 'mp4',
  180. 'title': 'My web playroom',
  181. 'uploader': 'Ze Frank',
  182. 'description': 'md5:ddb2a40ecd6b6a147e400e535874947b',
  183. }
  184. },
  185. # Embeded Ustream video
  186. {
  187. 'url': 'http://www.american.edu/spa/pti/nsa-privacy-janus-2014.cfm',
  188. 'md5': '27b99cdb639c9b12a79bca876a073417',
  189. 'info_dict': {
  190. 'id': '45734260',
  191. 'ext': 'flv',
  192. 'uploader': 'AU SPA: The NSA and Privacy',
  193. 'title': 'NSA and Privacy Forum Debate featuring General Hayden and Barton Gellman'
  194. }
  195. },
  196. # nowvideo embed hidden behind percent encoding
  197. {
  198. 'url': 'http://www.waoanime.tv/the-super-dimension-fortress-macross-episode-1/',
  199. 'md5': '2baf4ddd70f697d94b1c18cf796d5107',
  200. 'info_dict': {
  201. 'id': '06e53103ca9aa',
  202. 'ext': 'flv',
  203. 'title': 'Macross Episode 001 Watch Macross Episode 001 onl',
  204. 'description': 'No description',
  205. },
  206. },
  207. # arte embed
  208. {
  209. 'url': 'http://www.tv-replay.fr/redirection/20-03-14/x-enius-arte-10753389.html',
  210. 'md5': '7653032cbb25bf6c80d80f217055fa43',
  211. 'info_dict': {
  212. 'id': '048195-004_PLUS7-F',
  213. 'ext': 'flv',
  214. 'title': 'X:enius',
  215. 'description': 'md5:d5fdf32ef6613cdbfd516ae658abf168',
  216. 'upload_date': '20140320',
  217. },
  218. 'params': {
  219. 'skip_download': 'Requires rtmpdump'
  220. }
  221. },
  222. # smotri embed
  223. {
  224. 'url': 'http://rbctv.rbc.ru/archive/news/562949990879132.shtml',
  225. 'md5': 'ec40048448e9284c9a1de77bb188108b',
  226. 'info_dict': {
  227. 'id': 'v27008541fad',
  228. 'ext': 'mp4',
  229. 'title': 'Крым и Севастополь вошли в состав России',
  230. 'description': 'md5:fae01b61f68984c7bd2fa741e11c3175',
  231. 'duration': 900,
  232. 'upload_date': '20140318',
  233. 'uploader': 'rbctv_2012_4',
  234. 'uploader_id': 'rbctv_2012_4',
  235. },
  236. },
  237. # Condé Nast embed
  238. {
  239. 'url': 'http://www.wired.com/2014/04/honda-asimo/',
  240. 'md5': 'ba0dfe966fa007657bd1443ee672db0f',
  241. 'info_dict': {
  242. 'id': '53501be369702d3275860000',
  243. 'ext': 'mp4',
  244. 'title': 'Honda’s New Asimo Robot Is More Human Than Ever',
  245. }
  246. },
  247. # Dailymotion embed
  248. {
  249. 'url': 'http://www.spi0n.com/zap-spi0n-com-n216/',
  250. 'md5': '441aeeb82eb72c422c7f14ec533999cd',
  251. 'info_dict': {
  252. 'id': 'k2mm4bCdJ6CQ2i7c8o2',
  253. 'ext': 'mp4',
  254. 'title': 'Le Zap de Spi0n n°216 - Zapping du Web',
  255. 'uploader': 'Spi0n',
  256. },
  257. 'add_ie': ['Dailymotion'],
  258. },
  259. # YouTube embed
  260. {
  261. 'url': 'http://www.badzine.de/ansicht/datum/2014/06/09/so-funktioniert-die-neue-englische-badminton-liga.html',
  262. 'info_dict': {
  263. 'id': 'FXRb4ykk4S0',
  264. 'ext': 'mp4',
  265. 'title': 'The NBL Auction 2014',
  266. 'uploader': 'BADMINTON England',
  267. 'uploader_id': 'BADMINTONEvents',
  268. 'upload_date': '20140603',
  269. 'description': 'md5:9ef128a69f1e262a700ed83edb163a73',
  270. },
  271. 'add_ie': ['Youtube'],
  272. 'params': {
  273. 'skip_download': True,
  274. }
  275. },
  276. # MTVSercices embed
  277. {
  278. 'url': 'http://www.gametrailers.com/news-post/76093/north-america-europe-is-getting-that-mario-kart-8-mercedes-dlc-too',
  279. 'md5': '35727f82f58c76d996fc188f9755b0d5',
  280. 'info_dict': {
  281. 'id': '0306a69b-8adf-4fb5-aace-75f8e8cbfca9',
  282. 'ext': 'mp4',
  283. 'title': 'Review',
  284. 'description': 'Mario\'s life in the fast lane has never looked so good.',
  285. },
  286. },
  287. # YouTube embed via <data-embed-url="">
  288. {
  289. 'url': 'https://play.google.com/store/apps/details?id=com.gameloft.android.ANMP.GloftA8HM',
  290. 'info_dict': {
  291. 'id': 'jpSGZsgga_I',
  292. 'ext': 'mp4',
  293. 'title': 'Asphalt 8: Airborne - Launch Trailer',
  294. 'uploader': 'Gameloft',
  295. 'uploader_id': 'gameloft',
  296. 'upload_date': '20130821',
  297. 'description': 'md5:87bd95f13d8be3e7da87a5f2c443106a',
  298. },
  299. 'params': {
  300. 'skip_download': True,
  301. }
  302. },
  303. # Camtasia studio
  304. {
  305. 'url': 'http://www.ll.mit.edu/workshops/education/videocourses/antennas/lecture1/video/',
  306. 'playlist': [{
  307. 'md5': '0c5e352edabf715d762b0ad4e6d9ee67',
  308. 'info_dict': {
  309. 'id': 'Fenn-AA_PA_Radar_Course_Lecture_1c_Final',
  310. 'title': 'Fenn-AA_PA_Radar_Course_Lecture_1c_Final - video1',
  311. 'ext': 'flv',
  312. 'duration': 2235.90,
  313. }
  314. }, {
  315. 'md5': '10e4bb3aaca9fd630e273ff92d9f3c63',
  316. 'info_dict': {
  317. 'id': 'Fenn-AA_PA_Radar_Course_Lecture_1c_Final_PIP',
  318. 'title': 'Fenn-AA_PA_Radar_Course_Lecture_1c_Final - pip',
  319. 'ext': 'flv',
  320. 'duration': 2235.93,
  321. }
  322. }],
  323. 'info_dict': {
  324. 'title': 'Fenn-AA_PA_Radar_Course_Lecture_1c_Final',
  325. }
  326. },
  327. # Flowplayer
  328. {
  329. 'url': 'http://www.handjobhub.com/video/busty-blonde-siri-tit-fuck-while-wank-6313.html',
  330. 'md5': '9d65602bf31c6e20014319c7d07fba27',
  331. 'info_dict': {
  332. 'id': '5123ea6d5e5a7',
  333. 'ext': 'mp4',
  334. 'age_limit': 18,
  335. 'uploader': 'www.handjobhub.com',
  336. 'title': 'Busty Blonde Siri Tit Fuck While Wank at Handjob Hub',
  337. }
  338. },
  339. # RSS feed
  340. {
  341. 'url': 'http://phihag.de/2014/youtube-dl/rss2.xml',
  342. 'info_dict': {
  343. 'id': 'http://phihag.de/2014/youtube-dl/rss2.xml',
  344. 'title': 'Zero Punctuation',
  345. 'description': 're:'
  346. },
  347. 'playlist_mincount': 11,
  348. },
  349. # Multiple brightcove videos
  350. # https://github.com/rg3/youtube-dl/issues/2283
  351. {
  352. 'url': 'http://www.newyorker.com/online/blogs/newsdesk/2014/01/always-never-nuclear-command-and-control.html',
  353. 'info_dict': {
  354. 'id': 'always-never',
  355. 'title': 'Always / Never - The New Yorker',
  356. },
  357. 'playlist_count': 3,
  358. 'params': {
  359. 'extract_flat': False,
  360. 'skip_download': True,
  361. }
  362. }
  363. ]
  364. def report_download_webpage(self, video_id):
  365. """Report webpage download."""
  366. if not self._downloader.params.get('test', False):
  367. self._downloader.report_warning('Falling back on generic information extractor.')
  368. super(GenericIE, self).report_download_webpage(video_id)
  369. def report_following_redirect(self, new_url):
  370. """Report information extraction."""
  371. self._downloader.to_screen('[redirect] Following redirect to %s' % new_url)
  372. def _extract_rss(self, url, video_id, doc):
  373. playlist_title = doc.find('./channel/title').text
  374. playlist_desc_el = doc.find('./channel/description')
  375. playlist_desc = None if playlist_desc_el is None else playlist_desc_el.text
  376. entries = [{
  377. '_type': 'url',
  378. 'url': e.find('link').text,
  379. 'title': e.find('title').text,
  380. } for e in doc.findall('./channel/item')]
  381. return {
  382. '_type': 'playlist',
  383. 'id': url,
  384. 'title': playlist_title,
  385. 'description': playlist_desc,
  386. 'entries': entries,
  387. }
  388. def _extract_camtasia(self, url, video_id, webpage):
  389. """ Returns None if no camtasia video can be found. """
  390. camtasia_cfg = self._search_regex(
  391. r'fo\.addVariable\(\s*"csConfigFile",\s*"([^"]+)"\s*\);',
  392. webpage, 'camtasia configuration file', default=None)
  393. if camtasia_cfg is None:
  394. return None
  395. title = self._html_search_meta('DC.title', webpage, fatal=True)
  396. camtasia_url = compat_urlparse.urljoin(url, camtasia_cfg)
  397. camtasia_cfg = self._download_xml(
  398. camtasia_url, video_id,
  399. note='Downloading camtasia configuration',
  400. errnote='Failed to download camtasia configuration')
  401. fileset_node = camtasia_cfg.find('./playlist/array/fileset')
  402. entries = []
  403. for n in fileset_node.getchildren():
  404. url_n = n.find('./uri')
  405. if url_n is None:
  406. continue
  407. entries.append({
  408. 'id': os.path.splitext(url_n.text.rpartition('/')[2])[0],
  409. 'title': '%s - %s' % (title, n.tag),
  410. 'url': compat_urlparse.urljoin(url, url_n.text),
  411. 'duration': float_or_none(n.find('./duration').text),
  412. })
  413. return {
  414. '_type': 'playlist',
  415. 'entries': entries,
  416. 'title': title,
  417. }
  418. def _real_extract(self, url):
  419. if url.startswith('//'):
  420. return {
  421. '_type': 'url',
  422. 'url': self.http_scheme() + url,
  423. }
  424. parsed_url = compat_urlparse.urlparse(url)
  425. if not parsed_url.scheme:
  426. default_search = self._downloader.params.get('default_search')
  427. if default_search is None:
  428. default_search = 'fixup_error'
  429. if default_search in ('auto', 'auto_warning', 'fixup_error'):
  430. if '/' in url:
  431. self._downloader.report_warning('The url doesn\'t specify the protocol, trying with http')
  432. return self.url_result('http://' + url)
  433. elif default_search != 'fixup_error':
  434. if default_search == 'auto_warning':
  435. if re.match(r'^(?:url|URL)$', url):
  436. raise ExtractorError(
  437. 'Invalid URL: %r . Call youtube-dl like this: youtube-dl -v "https://www.youtube.com/watch?v=BaW_jenozKc" ' % url,
  438. expected=True)
  439. else:
  440. self._downloader.report_warning(
  441. 'Falling back to youtube search for %s . Set --default-search "auto" to suppress this warning.' % url)
  442. return self.url_result('ytsearch:' + url)
  443. if default_search in ('error', 'fixup_error'):
  444. raise ExtractorError(
  445. ('%r is not a valid URL. '
  446. 'Set --default-search "ytsearch" (or run youtube-dl "ytsearch:%s" ) to search YouTube'
  447. ) % (url, url), expected=True)
  448. else:
  449. assert ':' in default_search
  450. return self.url_result(default_search + url)
  451. url, smuggled_data = unsmuggle_url(url)
  452. force_videoid = None
  453. if smuggled_data and 'force_videoid' in smuggled_data:
  454. force_videoid = smuggled_data['force_videoid']
  455. video_id = force_videoid
  456. else:
  457. video_id = os.path.splitext(url.rstrip('/').split('/')[-1])[0]
  458. self.to_screen('%s: Requesting header' % video_id)
  459. head_req = HEADRequest(url)
  460. response = self._request_webpage(
  461. head_req, video_id,
  462. note=False, errnote='Could not send HEAD request to %s' % url,
  463. fatal=False)
  464. if response is not False:
  465. # Check for redirect
  466. new_url = response.geturl()
  467. if url != new_url:
  468. self.report_following_redirect(new_url)
  469. if force_videoid:
  470. new_url = smuggle_url(
  471. new_url, {'force_videoid': force_videoid})
  472. return self.url_result(new_url)
  473. # Check for direct link to a video
  474. content_type = response.headers.get('Content-Type', '')
  475. m = re.match(r'^(?P<type>audio|video|application(?=/ogg$))/(?P<format_id>.+)$', content_type)
  476. if m:
  477. upload_date = response.headers.get('Last-Modified')
  478. if upload_date:
  479. upload_date = unified_strdate(upload_date)
  480. return {
  481. 'id': video_id,
  482. 'title': os.path.splitext(url_basename(url))[0],
  483. 'formats': [{
  484. 'format_id': m.group('format_id'),
  485. 'url': url,
  486. 'vcodec': 'none' if m.group('type') == 'audio' else None
  487. }],
  488. 'upload_date': upload_date,
  489. }
  490. try:
  491. webpage = self._download_webpage(url, video_id)
  492. except ValueError:
  493. # since this is the last-resort InfoExtractor, if
  494. # this error is thrown, it'll be thrown here
  495. raise ExtractorError('Failed to download URL: %s' % url)
  496. self.report_extraction(video_id)
  497. # Is it an RSS feed?
  498. try:
  499. doc = parse_xml(webpage)
  500. if doc.tag == 'rss':
  501. return self._extract_rss(url, video_id, doc)
  502. except compat_xml_parse_error:
  503. pass
  504. # Is it a Camtasia project?
  505. camtasia_res = self._extract_camtasia(url, video_id, webpage)
  506. if camtasia_res is not None:
  507. return camtasia_res
  508. # Sometimes embedded video player is hidden behind percent encoding
  509. # (e.g. https://github.com/rg3/youtube-dl/issues/2448)
  510. # Unescaping the whole page allows to handle those cases in a generic way
  511. webpage = compat_urllib_parse.unquote(webpage)
  512. # it's tempting to parse this further, but you would
  513. # have to take into account all the variations like
  514. # Video Title - Site Name
  515. # Site Name | Video Title
  516. # Video Title - Tagline | Site Name
  517. # and so on and so forth; it's just not practical
  518. video_title = self._html_search_regex(
  519. r'(?s)<title>(.*?)</title>', webpage, 'video title',
  520. default='video')
  521. # Try to detect age limit automatically
  522. age_limit = self._rta_search(webpage)
  523. # And then there are the jokers who advertise that they use RTA,
  524. # but actually don't.
  525. AGE_LIMIT_MARKERS = [
  526. r'Proudly Labeled <a href="http://www.rtalabel.org/" title="Restricted to Adults">RTA</a>',
  527. ]
  528. if any(re.search(marker, webpage) for marker in AGE_LIMIT_MARKERS):
  529. age_limit = 18
  530. # video uploader is domain name
  531. video_uploader = self._search_regex(
  532. r'^(?:https?://)?([^/]*)/.*', url, 'video uploader')
  533. # Helper method
  534. def _playlist_from_matches(matches, getter, ie=None):
  535. urlrs = orderedSet(self.url_result(getter(m), ie) for m in matches)
  536. return self.playlist_result(
  537. urlrs, playlist_id=video_id, playlist_title=video_title)
  538. # Look for BrightCove:
  539. bc_urls = BrightcoveIE._extract_brightcove_urls(webpage)
  540. if bc_urls:
  541. self.to_screen('Brightcove video detected.')
  542. entries = [{
  543. '_type': 'url',
  544. 'url': smuggle_url(bc_url, {'Referer': url}),
  545. 'ie_key': 'Brightcove'
  546. } for bc_url in bc_urls]
  547. return {
  548. '_type': 'playlist',
  549. 'title': video_title,
  550. 'id': video_id,
  551. 'entries': entries,
  552. }
  553. # Look for embedded (iframe) Vimeo player
  554. mobj = re.search(
  555. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//player\.vimeo\.com/video/.+?)\1', webpage)
  556. if mobj:
  557. player_url = unescapeHTML(mobj.group('url'))
  558. surl = smuggle_url(player_url, {'Referer': url})
  559. return self.url_result(surl, 'Vimeo')
  560. # Look for embedded (swf embed) Vimeo player
  561. mobj = re.search(
  562. r'<embed[^>]+?src="(https?://(?:www\.)?vimeo\.com/moogaloop\.swf.+?)"', webpage)
  563. if mobj:
  564. return self.url_result(mobj.group(1), 'Vimeo')
  565. # Look for embedded YouTube player
  566. matches = re.findall(r'''(?x)
  567. (?:
  568. <iframe[^>]+?src=|
  569. data-video-url=|
  570. <embed[^>]+?src=|
  571. embedSWF\(?:\s*
  572. )
  573. (["\'])
  574. (?P<url>(?:https?:)?//(?:www\.)?youtube\.com/
  575. (?:embed|v)/.+?)
  576. \1''', webpage)
  577. if matches:
  578. return _playlist_from_matches(
  579. matches, lambda m: unescapeHTML(m[1]), ie='Youtube')
  580. # Look for embedded Dailymotion player
  581. matches = re.findall(
  582. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/.+?)\1', webpage)
  583. if matches:
  584. return _playlist_from_matches(
  585. matches, lambda m: unescapeHTML(m[1]))
  586. # Look for embedded Wistia player
  587. match = re.search(
  588. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:fast\.)?wistia\.net/embed/iframe/.+?)\1', webpage)
  589. if match:
  590. return {
  591. '_type': 'url_transparent',
  592. 'url': unescapeHTML(match.group('url')),
  593. 'ie_key': 'Wistia',
  594. 'uploader': video_uploader,
  595. 'title': video_title,
  596. 'id': video_id,
  597. }
  598. # Look for embedded blip.tv player
  599. mobj = re.search(r'<meta\s[^>]*https?://api\.blip\.tv/\w+/redirect/\w+/(\d+)', webpage)
  600. if mobj:
  601. return self.url_result('http://blip.tv/a/a-'+mobj.group(1), 'BlipTV')
  602. mobj = re.search(r'<(?:iframe|embed|object)\s[^>]*(https?://(?:\w+\.)?blip\.tv/(?:play/|api\.swf#)[a-zA-Z0-9_]+)', webpage)
  603. if mobj:
  604. return self.url_result(mobj.group(1), 'BlipTV')
  605. # Look for embedded condenast player
  606. matches = re.findall(
  607. r'<iframe\s+(?:[a-zA-Z-]+="[^"]+"\s+)*?src="(https?://player\.cnevids\.com/embed/[^"]+")',
  608. webpage)
  609. if matches:
  610. return {
  611. '_type': 'playlist',
  612. 'entries': [{
  613. '_type': 'url',
  614. 'ie_key': 'CondeNast',
  615. 'url': ma,
  616. } for ma in matches],
  617. 'title': video_title,
  618. 'id': video_id,
  619. }
  620. # Look for Bandcamp pages with custom domain
  621. mobj = re.search(r'<meta property="og:url"[^>]*?content="(.*?bandcamp\.com.*?)"', webpage)
  622. if mobj is not None:
  623. burl = unescapeHTML(mobj.group(1))
  624. # Don't set the extractor because it can be a track url or an album
  625. return self.url_result(burl)
  626. # Look for embedded Vevo player
  627. mobj = re.search(
  628. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:cache\.)?vevo\.com/.+?)\1', webpage)
  629. if mobj is not None:
  630. return self.url_result(mobj.group('url'))
  631. # Look for Ooyala videos
  632. mobj = (re.search(r'player.ooyala.com/[^"?]+\?[^"]*?(?:embedCode|ec)=(?P<ec>[^"&]+)', webpage) or
  633. re.search(r'OO.Player.create\([\'"].*?[\'"],\s*[\'"](?P<ec>.{32})[\'"]', webpage))
  634. if mobj is not None:
  635. return OoyalaIE._build_url_result(mobj.group('ec'))
  636. # Look for Aparat videos
  637. mobj = re.search(r'<iframe .*?src="(http://www\.aparat\.com/video/[^"]+)"', webpage)
  638. if mobj is not None:
  639. return self.url_result(mobj.group(1), 'Aparat')
  640. # Look for MPORA videos
  641. mobj = re.search(r'<iframe .*?src="(http://mpora\.(?:com|de)/videos/[^"]+)"', webpage)
  642. if mobj is not None:
  643. return self.url_result(mobj.group(1), 'Mpora')
  644. # Look for embedded NovaMov-based player
  645. mobj = re.search(
  646. r'''(?x)<(?:pagespeed_)?iframe[^>]+?src=(["\'])
  647. (?P<url>http://(?:(?:embed|www)\.)?
  648. (?:novamov\.com|
  649. nowvideo\.(?:ch|sx|eu|at|ag|co)|
  650. videoweed\.(?:es|com)|
  651. movshare\.(?:net|sx|ag)|
  652. divxstage\.(?:eu|net|ch|co|at|ag))
  653. /embed\.php.+?)\1''', webpage)
  654. if mobj is not None:
  655. return self.url_result(mobj.group('url'))
  656. # Look for embedded Facebook player
  657. mobj = re.search(
  658. r'<iframe[^>]+?src=(["\'])(?P<url>https://www\.facebook\.com/video/embed.+?)\1', webpage)
  659. if mobj is not None:
  660. return self.url_result(mobj.group('url'), 'Facebook')
  661. # Look for embedded VK player
  662. mobj = re.search(r'<iframe[^>]+?src=(["\'])(?P<url>https?://vk\.com/video_ext\.php.+?)\1', webpage)
  663. if mobj is not None:
  664. return self.url_result(mobj.group('url'), 'VK')
  665. # Look for embedded ivi player
  666. mobj = re.search(r'<embed[^>]+?src=(["\'])(?P<url>https?://(?:www\.)?ivi\.ru/video/player.+?)\1', webpage)
  667. if mobj is not None:
  668. return self.url_result(mobj.group('url'), 'Ivi')
  669. # Look for embedded Huffington Post player
  670. mobj = re.search(
  671. r'<iframe[^>]+?src=(["\'])(?P<url>https?://embed\.live\.huffingtonpost\.com/.+?)\1', webpage)
  672. if mobj is not None:
  673. return self.url_result(mobj.group('url'), 'HuffPost')
  674. # Look for embed.ly
  675. mobj = re.search(r'class=["\']embedly-card["\'][^>]href=["\'](?P<url>[^"\']+)', webpage)
  676. if mobj is not None:
  677. return self.url_result(mobj.group('url'))
  678. mobj = re.search(r'class=["\']embedly-embed["\'][^>]src=["\'][^"\']*url=(?P<url>[^&]+)', webpage)
  679. if mobj is not None:
  680. return self.url_result(compat_urllib_parse.unquote(mobj.group('url')))
  681. # Look for funnyordie embed
  682. matches = re.findall(r'<iframe[^>]+?src="(https?://(?:www\.)?funnyordie\.com/embed/[^"]+)"', webpage)
  683. if matches:
  684. return _playlist_from_matches(
  685. matches, getter=unescapeHTML, ie='FunnyOrDie')
  686. # Look for embedded RUTV player
  687. rutv_url = RUTVIE._extract_url(webpage)
  688. if rutv_url:
  689. return self.url_result(rutv_url, 'RUTV')
  690. # Look for embedded TED player
  691. mobj = re.search(
  692. r'<iframe[^>]+?src=(["\'])(?P<url>http://embed\.ted\.com/.+?)\1', webpage)
  693. if mobj is not None:
  694. return self.url_result(mobj.group('url'), 'TED')
  695. # Look for embedded Ustream videos
  696. mobj = re.search(
  697. r'<iframe[^>]+?src=(["\'])(?P<url>http://www\.ustream\.tv/embed/.+?)\1', webpage)
  698. if mobj is not None:
  699. return self.url_result(mobj.group('url'), 'Ustream')
  700. # Look for embedded arte.tv player
  701. mobj = re.search(
  702. r'<script [^>]*?src="(?P<url>http://www\.arte\.tv/playerv2/embed[^"]+)"',
  703. webpage)
  704. if mobj is not None:
  705. return self.url_result(mobj.group('url'), 'ArteTVEmbed')
  706. # Look for embedded smotri.com player
  707. smotri_url = SmotriIE._extract_url(webpage)
  708. if smotri_url:
  709. return self.url_result(smotri_url, 'Smotri')
  710. # Look for embeded soundcloud player
  711. mobj = re.search(
  712. r'<iframe src="(?P<url>https?://(?:w\.)?soundcloud\.com/player[^"]+)"',
  713. webpage)
  714. if mobj is not None:
  715. url = unescapeHTML(mobj.group('url'))
  716. return self.url_result(url)
  717. # Look for embedded vulture.com player
  718. mobj = re.search(
  719. r'<iframe src="(?P<url>https?://video\.vulture\.com/[^"]+)"',
  720. webpage)
  721. if mobj is not None:
  722. url = unescapeHTML(mobj.group('url'))
  723. return self.url_result(url, ie='Vulture')
  724. # Look for embedded mtvservices player
  725. mobj = re.search(
  726. r'<iframe src="(?P<url>https?://media\.mtvnservices\.com/embed/[^"]+)"',
  727. webpage)
  728. if mobj is not None:
  729. url = unescapeHTML(mobj.group('url'))
  730. return self.url_result(url, ie='MTVServicesEmbedded')
  731. # Look for embedded yahoo player
  732. mobj = re.search(
  733. r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:screen|movies)\.yahoo\.com/.+?\.html\?format=embed)\1',
  734. webpage)
  735. if mobj is not None:
  736. return self.url_result(mobj.group('url'), 'Yahoo')
  737. # Look for embedded sbs.com.au player
  738. mobj = re.search(
  739. r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:www\.)sbs\.com\.au/ondemand/video/single/.+?)\1',
  740. webpage)
  741. if mobj is not None:
  742. return self.url_result(mobj.group('url'), 'SBS')
  743. # Start with something easy: JW Player in SWFObject
  744. found = re.findall(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
  745. if not found:
  746. # Look for gorilla-vid style embedding
  747. found = re.findall(r'''(?sx)
  748. (?:
  749. jw_plugins|
  750. JWPlayerOptions|
  751. jwplayer\s*\(\s*["'][^'"]+["']\s*\)\s*\.setup
  752. )
  753. .*?file\s*:\s*["\'](.*?)["\']''', webpage)
  754. if not found:
  755. # Broaden the search a little bit
  756. found = re.findall(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
  757. if not found:
  758. # Broaden the findall a little bit: JWPlayer JS loader
  759. found = re.findall(r'[^A-Za-z0-9]?file["\']?:\s*["\'](http(?![^\'"]+\.[0-9]+[\'"])[^\'"]+)["\']', webpage)
  760. if not found:
  761. # Flow player
  762. found = re.findall(r'''(?xs)
  763. flowplayer\("[^"]+",\s*
  764. \{[^}]+?\}\s*,
  765. \s*{[^}]+? ["']?clip["']?\s*:\s*\{\s*
  766. ["']?url["']?\s*:\s*["']([^"']+)["']
  767. ''', webpage)
  768. if not found:
  769. # Try to find twitter cards info
  770. found = re.findall(r'<meta (?:property|name)="twitter:player:stream" (?:content|value)="(.+?)"', webpage)
  771. if not found:
  772. # We look for Open Graph info:
  773. # We have to match any number spaces between elements, some sites try to align them (eg.: statigr.am)
  774. m_video_type = re.findall(r'<meta.*?property="og:video:type".*?content="video/(.*?)"', webpage)
  775. # We only look in og:video if the MIME type is a video, don't try if it's a Flash player:
  776. if m_video_type is not None:
  777. def check_video(vurl):
  778. vpath = compat_urlparse.urlparse(vurl).path
  779. vext = determine_ext(vpath)
  780. return '.' in vpath and vext not in ('swf', 'png', 'jpg')
  781. found = list(filter(
  782. check_video,
  783. re.findall(r'<meta.*?property="og:video".*?content="(.*?)"', webpage)))
  784. if not found:
  785. # HTML5 video
  786. found = re.findall(r'(?s)<video[^<]*(?:>.*?<source[^>]+)? src="([^"]+)"', webpage)
  787. if not found:
  788. found = re.search(
  789. r'(?i)<meta\s+(?=(?:[a-z-]+="[^"]+"\s+)*http-equiv="refresh")'
  790. r'(?:[a-z-]+="[^"]+"\s+)*?content="[0-9]{,2};url=\'([^\']+)\'"',
  791. webpage)
  792. if found:
  793. new_url = found.group(1)
  794. self.report_following_redirect(new_url)
  795. return {
  796. '_type': 'url',
  797. 'url': new_url,
  798. }
  799. if not found:
  800. raise ExtractorError('Unsupported URL: %s' % url)
  801. entries = []
  802. for video_url in found:
  803. video_url = compat_urlparse.urljoin(url, video_url)
  804. video_id = compat_urllib_parse.unquote(os.path.basename(video_url))
  805. # Sometimes, jwplayer extraction will result in a YouTube URL
  806. if YoutubeIE.suitable(video_url):
  807. entries.append(self.url_result(video_url, 'Youtube'))
  808. continue
  809. # here's a fun little line of code for you:
  810. video_id = os.path.splitext(video_id)[0]
  811. entries.append({
  812. 'id': video_id,
  813. 'url': video_url,
  814. 'uploader': video_uploader,
  815. 'title': video_title,
  816. 'age_limit': age_limit,
  817. })
  818. if len(entries) == 1:
  819. return entries[0]
  820. else:
  821. for num, e in enumerate(entries, start=1):
  822. e['title'] = '%s (%d)' % (e['title'], num)
  823. return {
  824. '_type': 'playlist',
  825. 'entries': entries,
  826. }