generic.py 38 KB

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