You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 lines
21 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_HTTPError,
  6. compat_str,
  7. )
  8. from ..utils import (
  9. determine_ext,
  10. ExtractorError,
  11. fix_xml_ampersands,
  12. orderedSet,
  13. parse_duration,
  14. qualities,
  15. strip_jsonp,
  16. unified_strdate,
  17. )
  18. class NPOBaseIE(InfoExtractor):
  19. def _get_token(self, video_id):
  20. return self._download_json(
  21. 'http://ida.omroep.nl/app.php/auth', video_id,
  22. note='Downloading token')['token']
  23. class NPOIE(NPOBaseIE):
  24. IE_NAME = 'npo'
  25. IE_DESC = 'npo.nl, ntr.nl, omroepwnl.nl, zapp.nl and npo3.nl'
  26. _VALID_URL = r'''(?x)
  27. (?:
  28. npo:|
  29. https?://
  30. (?:www\.)?
  31. (?:
  32. npo\.nl/(?!(?:live|radio)/)(?:[^/]+/){2}|
  33. ntr\.nl/(?:[^/]+/){2,}|
  34. omroepwnl\.nl/video/fragment/[^/]+__|
  35. (?:zapp|npo3)\.nl/(?:[^/]+/){2}
  36. )
  37. )
  38. (?P<id>[^/?#]+)
  39. '''
  40. _TESTS = [{
  41. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  42. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  43. 'info_dict': {
  44. 'id': 'VPWON_1220719',
  45. 'ext': 'm4v',
  46. 'title': 'Nieuwsuur',
  47. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  48. 'upload_date': '20140622',
  49. },
  50. }, {
  51. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  52. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  53. 'info_dict': {
  54. 'id': 'VARA_101191800',
  55. 'ext': 'm4v',
  56. 'title': 'De Mega Mike & Mega Thomas show: The best of.',
  57. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  58. 'upload_date': '20090227',
  59. 'duration': 2400,
  60. },
  61. }, {
  62. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  63. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  64. 'info_dict': {
  65. 'id': 'VPWON_1169289',
  66. 'ext': 'm4v',
  67. 'title': 'Tegenlicht: Zwart geld. De toekomst komt uit Afrika',
  68. 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
  69. 'upload_date': '20130225',
  70. 'duration': 3000,
  71. },
  72. }, {
  73. 'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
  74. 'info_dict': {
  75. 'id': 'WO_VPRO_043706',
  76. 'ext': 'm4v',
  77. 'title': 'De nieuwe mens - Deel 1',
  78. 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
  79. 'duration': 4680,
  80. },
  81. 'params': {
  82. 'skip_download': True,
  83. }
  84. }, {
  85. # non asf in streams
  86. 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
  87. 'info_dict': {
  88. 'id': 'WO_NOS_762771',
  89. 'ext': 'mp4',
  90. 'title': 'Hoe gaat Europa verder na Parijs?',
  91. },
  92. 'params': {
  93. 'skip_download': True,
  94. }
  95. }, {
  96. 'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content',
  97. 'info_dict': {
  98. 'id': 'VPWON_1233944',
  99. 'ext': 'm4v',
  100. 'title': 'Aap, poot, pies',
  101. 'description': 'md5:c9c8005d1869ae65b858e82c01a91fde',
  102. 'upload_date': '20150508',
  103. 'duration': 599,
  104. },
  105. 'params': {
  106. 'skip_download': True,
  107. }
  108. }, {
  109. 'url': 'http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698',
  110. 'info_dict': {
  111. 'id': 'POW_00996502',
  112. 'ext': 'm4v',
  113. 'title': '''"Dit is wel een 'landslide'..."''',
  114. 'description': 'md5:f8d66d537dfb641380226e31ca57b8e8',
  115. 'upload_date': '20150508',
  116. 'duration': 462,
  117. },
  118. 'params': {
  119. 'skip_download': True,
  120. }
  121. }, {
  122. # audio
  123. 'url': 'http://www.npo.nl/jouw-stad-rotterdam/29-01-2017/RBX_FUNX_6683215/RBX_FUNX_7601437',
  124. 'info_dict': {
  125. 'id': 'RBX_FUNX_6683215',
  126. 'ext': 'mp3',
  127. 'title': 'Jouw Stad Rotterdam',
  128. 'description': 'md5:db251505244f097717ec59fabc372d9f',
  129. },
  130. 'params': {
  131. 'skip_download': True,
  132. }
  133. }, {
  134. 'url': 'http://www.zapp.nl/de-bzt-show/gemist/KN_1687547',
  135. 'only_matching': True,
  136. }, {
  137. 'url': 'http://www.zapp.nl/de-bzt-show/filmpjes/POMS_KN_7315118',
  138. 'only_matching': True,
  139. }, {
  140. 'url': 'http://www.zapp.nl/beste-vrienden-quiz/extra-video-s/WO_NTR_1067990',
  141. 'only_matching': True,
  142. }, {
  143. 'url': 'https://www.npo3.nl/3onderzoekt/16-09-2015/VPWON_1239870',
  144. 'only_matching': True,
  145. }, {
  146. # live stream
  147. 'url': 'npo:LI_NL1_4188102',
  148. 'only_matching': True,
  149. }, {
  150. 'url': 'http://www.npo.nl/radio-gaga/13-06-2017/BNN_101383373',
  151. 'only_matching': True,
  152. }]
  153. def _real_extract(self, url):
  154. video_id = self._match_id(url)
  155. return self._get_info(video_id)
  156. def _get_info(self, video_id):
  157. metadata = self._download_json(
  158. 'http://e.omroep.nl/metadata/%s' % video_id,
  159. video_id,
  160. # We have to remove the javascript callback
  161. transform_source=strip_jsonp,
  162. )
  163. # For some videos actual video id (prid) is different (e.g. for
  164. # http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698
  165. # video id is POMS_WNL_853698 but prid is POW_00996502)
  166. video_id = metadata.get('prid') or video_id
  167. # titel is too generic in some cases so utilize aflevering_titel as well
  168. # when available (e.g. http://tegenlicht.vpro.nl/afleveringen/2014-2015/access-to-africa.html)
  169. title = metadata['titel']
  170. sub_title = metadata.get('aflevering_titel')
  171. if sub_title and sub_title != title:
  172. title += ': %s' % sub_title
  173. token = self._get_token(video_id)
  174. formats = []
  175. urls = set()
  176. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  177. items = self._download_json(
  178. 'http://ida.omroep.nl/app.php/%s' % video_id, video_id,
  179. 'Downloading formats JSON', query={
  180. 'adaptive': 'yes',
  181. 'token': token,
  182. })['items'][0]
  183. for num, item in enumerate(items):
  184. item_url = item.get('url')
  185. if not item_url or item_url in urls:
  186. continue
  187. urls.add(item_url)
  188. format_id = self._search_regex(
  189. r'video/ida/([^/]+)', item_url, 'format id',
  190. default=None)
  191. def add_format_url(format_url):
  192. formats.append({
  193. 'url': format_url,
  194. 'format_id': format_id,
  195. 'quality': quality(format_id),
  196. })
  197. # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
  198. if item.get('contentType') in ('url', 'audio'):
  199. add_format_url(item_url)
  200. continue
  201. try:
  202. stream_info = self._download_json(
  203. item_url + '&type=json', video_id,
  204. 'Downloading %s stream JSON'
  205. % item.get('label') or item.get('format') or format_id or num)
  206. except ExtractorError as ee:
  207. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404:
  208. error = (self._parse_json(
  209. ee.cause.read().decode(), video_id,
  210. fatal=False) or {}).get('errorstring')
  211. if error:
  212. raise ExtractorError(error, expected=True)
  213. raise
  214. # Stream URL instead of JSON, example: npo:LI_NL1_4188102
  215. if isinstance(stream_info, compat_str):
  216. if not stream_info.startswith('http'):
  217. continue
  218. video_url = stream_info
  219. # JSON
  220. else:
  221. video_url = stream_info.get('url')
  222. if not video_url or video_url in urls:
  223. continue
  224. urls.add(item_url)
  225. if determine_ext(video_url) == 'm3u8':
  226. formats.extend(self._extract_m3u8_formats(
  227. video_url, video_id, ext='mp4',
  228. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
  229. else:
  230. add_format_url(video_url)
  231. is_live = metadata.get('medium') == 'live'
  232. if not is_live:
  233. for num, stream in enumerate(metadata.get('streams', [])):
  234. stream_url = stream.get('url')
  235. if not stream_url or stream_url in urls:
  236. continue
  237. urls.add(stream_url)
  238. # smooth streaming is not supported
  239. stream_type = stream.get('type', '').lower()
  240. if stream_type in ['ss', 'ms']:
  241. continue
  242. if stream_type == 'hds':
  243. f4m_formats = self._extract_f4m_formats(
  244. stream_url, video_id, fatal=False)
  245. # f4m downloader downloads only piece of live stream
  246. for f4m_format in f4m_formats:
  247. f4m_format['preference'] = -1
  248. formats.extend(f4m_formats)
  249. elif stream_type == 'hls':
  250. formats.extend(self._extract_m3u8_formats(
  251. stream_url, video_id, ext='mp4', fatal=False))
  252. # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
  253. elif '.asf' in stream_url:
  254. asx = self._download_xml(
  255. stream_url, video_id,
  256. 'Downloading stream %d ASX playlist' % num,
  257. transform_source=fix_xml_ampersands, fatal=False)
  258. if not asx:
  259. continue
  260. ref = asx.find('./ENTRY/Ref')
  261. if ref is None:
  262. continue
  263. video_url = ref.get('href')
  264. if not video_url or video_url in urls:
  265. continue
  266. urls.add(video_url)
  267. formats.append({
  268. 'url': video_url,
  269. 'ext': stream.get('formaat', 'asf'),
  270. 'quality': stream.get('kwaliteit'),
  271. 'preference': -10,
  272. })
  273. else:
  274. formats.append({
  275. 'url': stream_url,
  276. 'quality': stream.get('kwaliteit'),
  277. })
  278. self._sort_formats(formats)
  279. subtitles = {}
  280. if metadata.get('tt888') == 'ja':
  281. subtitles['nl'] = [{
  282. 'ext': 'vtt',
  283. 'url': 'http://tt888.omroep.nl/tt888/%s' % video_id,
  284. }]
  285. return {
  286. 'id': video_id,
  287. 'title': self._live_title(title) if is_live else title,
  288. 'description': metadata.get('info'),
  289. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  290. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  291. 'duration': parse_duration(metadata.get('tijdsduur')),
  292. 'formats': formats,
  293. 'subtitles': subtitles,
  294. 'is_live': is_live,
  295. }
  296. class NPOLiveIE(NPOBaseIE):
  297. IE_NAME = 'npo.nl:live'
  298. _VALID_URL = r'https?://(?:www\.)?npo\.nl/live(?:/(?P<id>[^/?#&]+))?'
  299. _TESTS = [{
  300. 'url': 'http://www.npo.nl/live/npo-1',
  301. 'info_dict': {
  302. 'id': 'LI_NL1_4188102',
  303. 'display_id': 'npo-1',
  304. 'ext': 'mp4',
  305. 'title': 're:^NPO 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  306. 'is_live': True,
  307. },
  308. 'params': {
  309. 'skip_download': True,
  310. }
  311. }, {
  312. 'url': 'http://www.npo.nl/live',
  313. 'only_matching': True,
  314. }]
  315. def _real_extract(self, url):
  316. display_id = self._match_id(url) or 'npo-1'
  317. webpage = self._download_webpage(url, display_id)
  318. live_id = self._search_regex(
  319. [r'media-id="([^"]+)"', r'data-prid="([^"]+)"'], webpage, 'live id')
  320. return {
  321. '_type': 'url_transparent',
  322. 'url': 'npo:%s' % live_id,
  323. 'ie_key': NPOIE.ie_key(),
  324. 'id': live_id,
  325. 'display_id': display_id,
  326. }
  327. class NPORadioIE(InfoExtractor):
  328. IE_NAME = 'npo.nl:radio'
  329. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
  330. _TEST = {
  331. 'url': 'http://www.npo.nl/radio/radio-1',
  332. 'info_dict': {
  333. 'id': 'radio-1',
  334. 'ext': 'mp3',
  335. 'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  336. 'is_live': True,
  337. },
  338. 'params': {
  339. 'skip_download': True,
  340. }
  341. }
  342. @staticmethod
  343. def _html_get_attribute_regex(attribute):
  344. return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
  345. def _real_extract(self, url):
  346. video_id = self._match_id(url)
  347. webpage = self._download_webpage(url, video_id)
  348. title = self._html_search_regex(
  349. self._html_get_attribute_regex('data-channel'), webpage, 'title')
  350. stream = self._parse_json(
  351. self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
  352. video_id)
  353. codec = stream.get('codec')
  354. return {
  355. 'id': video_id,
  356. 'url': stream['url'],
  357. 'title': self._live_title(title),
  358. 'acodec': codec,
  359. 'ext': codec,
  360. 'is_live': True,
  361. }
  362. class NPORadioFragmentIE(InfoExtractor):
  363. IE_NAME = 'npo.nl:radio:fragment'
  364. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
  365. _TEST = {
  366. 'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
  367. 'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
  368. 'info_dict': {
  369. 'id': '174356',
  370. 'ext': 'mp3',
  371. 'title': 'Jubileumconcert Willeke Alberti',
  372. },
  373. }
  374. def _real_extract(self, url):
  375. audio_id = self._match_id(url)
  376. webpage = self._download_webpage(url, audio_id)
  377. title = self._html_search_regex(
  378. r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
  379. webpage, 'title')
  380. audio_url = self._search_regex(
  381. r"data-streams='([^']+)'", webpage, 'audio url')
  382. return {
  383. 'id': audio_id,
  384. 'url': audio_url,
  385. 'title': title,
  386. }
  387. class NPODataMidEmbedIE(InfoExtractor):
  388. def _real_extract(self, url):
  389. display_id = self._match_id(url)
  390. webpage = self._download_webpage(url, display_id)
  391. video_id = self._search_regex(
  392. r'data-mid=(["\'])(?P<id>(?:(?!\1).)+)\1', webpage, 'video_id', group='id')
  393. return {
  394. '_type': 'url_transparent',
  395. 'ie_key': 'NPO',
  396. 'url': 'npo:%s' % video_id,
  397. 'display_id': display_id
  398. }
  399. class SchoolTVIE(NPODataMidEmbedIE):
  400. IE_NAME = 'schooltv'
  401. _VALID_URL = r'https?://(?:www\.)?schooltv\.nl/video/(?P<id>[^/?#&]+)'
  402. _TEST = {
  403. 'url': 'http://www.schooltv.nl/video/ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam/',
  404. 'info_dict': {
  405. 'id': 'WO_NTR_429477',
  406. 'display_id': 'ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam',
  407. 'title': 'Ademhaling: De hele dag haal je adem. Maar wat gebeurt er dan eigenlijk in je lichaam?',
  408. 'ext': 'mp4',
  409. 'description': 'md5:abfa0ff690adb73fd0297fd033aaa631'
  410. },
  411. 'params': {
  412. # Skip because of m3u8 download
  413. 'skip_download': True
  414. }
  415. }
  416. class HetKlokhuisIE(NPODataMidEmbedIE):
  417. IE_NAME = 'hetklokhuis'
  418. _VALID_URL = r'https?://(?:www\.)?hetklokhuis\.nl/[^/]+/\d+/(?P<id>[^/?#&]+)'
  419. _TEST = {
  420. 'url': 'http://hetklokhuis.nl/tv-uitzending/3471/Zwaartekrachtsgolven',
  421. 'info_dict': {
  422. 'id': 'VPWON_1260528',
  423. 'display_id': 'Zwaartekrachtsgolven',
  424. 'ext': 'm4v',
  425. 'title': 'Het Klokhuis: Zwaartekrachtsgolven',
  426. 'description': 'md5:c94f31fb930d76c2efa4a4a71651dd48',
  427. 'upload_date': '20170223',
  428. },
  429. 'params': {
  430. 'skip_download': True
  431. }
  432. }
  433. class NPOPlaylistBaseIE(NPOIE):
  434. def _real_extract(self, url):
  435. playlist_id = self._match_id(url)
  436. webpage = self._download_webpage(url, playlist_id)
  437. entries = [
  438. self.url_result('npo:%s' % video_id if not video_id.startswith('http') else video_id)
  439. for video_id in orderedSet(re.findall(self._PLAYLIST_ENTRY_RE, webpage))
  440. ]
  441. playlist_title = self._html_search_regex(
  442. self._PLAYLIST_TITLE_RE, webpage, 'playlist title',
  443. default=None) or self._og_search_title(webpage)
  444. return self.playlist_result(entries, playlist_id, playlist_title)
  445. class VPROIE(NPOPlaylistBaseIE):
  446. IE_NAME = 'vpro'
  447. _VALID_URL = r'https?://(?:www\.)?(?:(?:tegenlicht\.)?vpro|2doc)\.nl/(?:[^/]+/)*(?P<id>[^/]+)\.html'
  448. _PLAYLIST_TITLE_RE = (r'<h1[^>]+class=["\'].*?\bmedia-platform-title\b.*?["\'][^>]*>([^<]+)',
  449. r'<h5[^>]+class=["\'].*?\bmedia-platform-subtitle\b.*?["\'][^>]*>([^<]+)')
  450. _PLAYLIST_ENTRY_RE = r'data-media-id="([^"]+)"'
  451. _TESTS = [
  452. {
  453. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  454. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  455. 'info_dict': {
  456. 'id': 'VPWON_1169289',
  457. 'ext': 'm4v',
  458. 'title': 'De toekomst komt uit Afrika',
  459. 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
  460. 'upload_date': '20130225',
  461. },
  462. 'skip': 'Video gone',
  463. },
  464. {
  465. 'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html',
  466. 'info_dict': {
  467. 'id': 'sergio-herman',
  468. 'title': 'sergio herman: fucking perfect',
  469. },
  470. 'playlist_count': 2,
  471. },
  472. {
  473. # playlist with youtube embed
  474. 'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html',
  475. 'info_dict': {
  476. 'id': 'education-education',
  477. 'title': 'education education',
  478. },
  479. 'playlist_count': 2,
  480. },
  481. {
  482. 'url': 'http://www.2doc.nl/documentaires/series/2doc/2015/oktober/de-tegenprestatie.html',
  483. 'info_dict': {
  484. 'id': 'de-tegenprestatie',
  485. 'title': 'De Tegenprestatie',
  486. },
  487. 'playlist_count': 2,
  488. }, {
  489. 'url': 'http://www.2doc.nl/speel~VARA_101375237~mh17-het-verdriet-van-nederland~.html',
  490. 'info_dict': {
  491. 'id': 'VARA_101375237',
  492. 'ext': 'm4v',
  493. 'title': 'MH17: Het verdriet van Nederland',
  494. 'description': 'md5:09e1a37c1fdb144621e22479691a9f18',
  495. 'upload_date': '20150716',
  496. },
  497. 'params': {
  498. # Skip because of m3u8 download
  499. 'skip_download': True
  500. },
  501. }
  502. ]
  503. class WNLIE(NPOPlaylistBaseIE):
  504. IE_NAME = 'wnl'
  505. _VALID_URL = r'https?://(?:www\.)?omroepwnl\.nl/video/detail/(?P<id>[^/]+)__\d+'
  506. _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class="subject"[^>]*>(.+?)</h1>'
  507. _PLAYLIST_ENTRY_RE = r'<a[^>]+href="([^"]+)"[^>]+class="js-mid"[^>]*>Deel \d+'
  508. _TESTS = [{
  509. 'url': 'http://www.omroepwnl.nl/video/detail/vandaag-de-dag-6-mei__060515',
  510. 'info_dict': {
  511. 'id': 'vandaag-de-dag-6-mei',
  512. 'title': 'Vandaag de Dag 6 mei',
  513. },
  514. 'playlist_count': 4,
  515. }]
  516. class AndereTijdenIE(NPOPlaylistBaseIE):
  517. IE_NAME = 'anderetijden'
  518. _VALID_URL = r'https?://(?:www\.)?anderetijden\.nl/programma/(?:[^/]+/)+(?P<id>[^/?#&]+)'
  519. _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class=["\'].*?\bpage-title\b.*?["\'][^>]*>(.+?)</h1>'
  520. _PLAYLIST_ENTRY_RE = r'<figure[^>]+class=["\']episode-container episode-page["\'][^>]+data-prid=["\'](.+?)["\']'
  521. _TESTS = [{
  522. 'url': 'http://anderetijden.nl/programma/1/Andere-Tijden/aflevering/676/Duitse-soldaten-over-de-Slag-bij-Arnhem',
  523. 'info_dict': {
  524. 'id': 'Duitse-soldaten-over-de-Slag-bij-Arnhem',
  525. 'title': 'Duitse soldaten over de Slag bij Arnhem',
  526. },
  527. 'playlist_count': 3,
  528. }]