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.

595 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 and ntr.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\.nl/[^/]+/[^/]+/
  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. # live stream
  144. 'url': 'npo:LI_NL1_4188102',
  145. 'only_matching': True,
  146. }, {
  147. 'url': 'http://www.npo.nl/radio-gaga/13-06-2017/BNN_101383373',
  148. 'only_matching': True,
  149. }]
  150. def _real_extract(self, url):
  151. video_id = self._match_id(url)
  152. return self._get_info(video_id)
  153. def _get_info(self, video_id):
  154. metadata = self._download_json(
  155. 'http://e.omroep.nl/metadata/%s' % video_id,
  156. video_id,
  157. # We have to remove the javascript callback
  158. transform_source=strip_jsonp,
  159. )
  160. # For some videos actual video id (prid) is different (e.g. for
  161. # http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698
  162. # video id is POMS_WNL_853698 but prid is POW_00996502)
  163. video_id = metadata.get('prid') or video_id
  164. # titel is too generic in some cases so utilize aflevering_titel as well
  165. # when available (e.g. http://tegenlicht.vpro.nl/afleveringen/2014-2015/access-to-africa.html)
  166. title = metadata['titel']
  167. sub_title = metadata.get('aflevering_titel')
  168. if sub_title and sub_title != title:
  169. title += ': %s' % sub_title
  170. token = self._get_token(video_id)
  171. formats = []
  172. urls = set()
  173. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  174. items = self._download_json(
  175. 'http://ida.omroep.nl/app.php/%s' % video_id, video_id,
  176. 'Downloading formats JSON', query={
  177. 'adaptive': 'yes',
  178. 'token': token,
  179. })['items'][0]
  180. for num, item in enumerate(items):
  181. item_url = item.get('url')
  182. if not item_url or item_url in urls:
  183. continue
  184. urls.add(item_url)
  185. format_id = self._search_regex(
  186. r'video/ida/([^/]+)', item_url, 'format id',
  187. default=None)
  188. def add_format_url(format_url):
  189. formats.append({
  190. 'url': format_url,
  191. 'format_id': format_id,
  192. 'quality': quality(format_id),
  193. })
  194. # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
  195. if item.get('contentType') in ('url', 'audio'):
  196. add_format_url(item_url)
  197. continue
  198. try:
  199. stream_info = self._download_json(
  200. item_url + '&type=json', video_id,
  201. 'Downloading %s stream JSON'
  202. % item.get('label') or item.get('format') or format_id or num)
  203. except ExtractorError as ee:
  204. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404:
  205. error = (self._parse_json(
  206. ee.cause.read().decode(), video_id,
  207. fatal=False) or {}).get('errorstring')
  208. if error:
  209. raise ExtractorError(error, expected=True)
  210. raise
  211. # Stream URL instead of JSON, example: npo:LI_NL1_4188102
  212. if isinstance(stream_info, compat_str):
  213. if not stream_info.startswith('http'):
  214. continue
  215. video_url = stream_info
  216. # JSON
  217. else:
  218. video_url = stream_info.get('url')
  219. if not video_url or video_url in urls:
  220. continue
  221. urls.add(item_url)
  222. if determine_ext(video_url) == 'm3u8':
  223. formats.extend(self._extract_m3u8_formats(
  224. video_url, video_id, ext='mp4',
  225. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
  226. else:
  227. add_format_url(video_url)
  228. is_live = metadata.get('medium') == 'live'
  229. if not is_live:
  230. for num, stream in enumerate(metadata.get('streams', [])):
  231. stream_url = stream.get('url')
  232. if not stream_url or stream_url in urls:
  233. continue
  234. urls.add(stream_url)
  235. # smooth streaming is not supported
  236. stream_type = stream.get('type', '').lower()
  237. if stream_type in ['ss', 'ms']:
  238. continue
  239. if stream_type == 'hds':
  240. f4m_formats = self._extract_f4m_formats(
  241. stream_url, video_id, fatal=False)
  242. # f4m downloader downloads only piece of live stream
  243. for f4m_format in f4m_formats:
  244. f4m_format['preference'] = -1
  245. formats.extend(f4m_formats)
  246. elif stream_type == 'hls':
  247. formats.extend(self._extract_m3u8_formats(
  248. stream_url, video_id, ext='mp4', fatal=False))
  249. # Example: http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706
  250. elif '.asf' in stream_url:
  251. asx = self._download_xml(
  252. stream_url, video_id,
  253. 'Downloading stream %d ASX playlist' % num,
  254. transform_source=fix_xml_ampersands, fatal=False)
  255. if not asx:
  256. continue
  257. ref = asx.find('./ENTRY/Ref')
  258. if ref is None:
  259. continue
  260. video_url = ref.get('href')
  261. if not video_url or video_url in urls:
  262. continue
  263. urls.add(video_url)
  264. formats.append({
  265. 'url': video_url,
  266. 'ext': stream.get('formaat', 'asf'),
  267. 'quality': stream.get('kwaliteit'),
  268. 'preference': -10,
  269. })
  270. else:
  271. formats.append({
  272. 'url': stream_url,
  273. 'quality': stream.get('kwaliteit'),
  274. })
  275. self._sort_formats(formats)
  276. subtitles = {}
  277. if metadata.get('tt888') == 'ja':
  278. subtitles['nl'] = [{
  279. 'ext': 'vtt',
  280. 'url': 'http://tt888.omroep.nl/tt888/%s' % video_id,
  281. }]
  282. return {
  283. 'id': video_id,
  284. 'title': self._live_title(title) if is_live else title,
  285. 'description': metadata.get('info'),
  286. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  287. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  288. 'duration': parse_duration(metadata.get('tijdsduur')),
  289. 'formats': formats,
  290. 'subtitles': subtitles,
  291. 'is_live': is_live,
  292. }
  293. class NPOLiveIE(NPOBaseIE):
  294. IE_NAME = 'npo.nl:live'
  295. _VALID_URL = r'https?://(?:www\.)?npo\.nl/live(?:/(?P<id>[^/?#&]+))?'
  296. _TESTS = [{
  297. 'url': 'http://www.npo.nl/live/npo-1',
  298. 'info_dict': {
  299. 'id': 'LI_NL1_4188102',
  300. 'display_id': 'npo-1',
  301. 'ext': 'mp4',
  302. 'title': 're:^NPO 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  303. 'is_live': True,
  304. },
  305. 'params': {
  306. 'skip_download': True,
  307. }
  308. }, {
  309. 'url': 'http://www.npo.nl/live',
  310. 'only_matching': True,
  311. }]
  312. def _real_extract(self, url):
  313. display_id = self._match_id(url) or 'npo-1'
  314. webpage = self._download_webpage(url, display_id)
  315. live_id = self._search_regex(
  316. [r'media-id="([^"]+)"', r'data-prid="([^"]+)"'], webpage, 'live id')
  317. return {
  318. '_type': 'url_transparent',
  319. 'url': 'npo:%s' % live_id,
  320. 'ie_key': NPOIE.ie_key(),
  321. 'id': live_id,
  322. 'display_id': display_id,
  323. }
  324. class NPORadioIE(InfoExtractor):
  325. IE_NAME = 'npo.nl:radio'
  326. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
  327. _TEST = {
  328. 'url': 'http://www.npo.nl/radio/radio-1',
  329. 'info_dict': {
  330. 'id': 'radio-1',
  331. 'ext': 'mp3',
  332. 'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  333. 'is_live': True,
  334. },
  335. 'params': {
  336. 'skip_download': True,
  337. }
  338. }
  339. @staticmethod
  340. def _html_get_attribute_regex(attribute):
  341. return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
  342. def _real_extract(self, url):
  343. video_id = self._match_id(url)
  344. webpage = self._download_webpage(url, video_id)
  345. title = self._html_search_regex(
  346. self._html_get_attribute_regex('data-channel'), webpage, 'title')
  347. stream = self._parse_json(
  348. self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
  349. video_id)
  350. codec = stream.get('codec')
  351. return {
  352. 'id': video_id,
  353. 'url': stream['url'],
  354. 'title': self._live_title(title),
  355. 'acodec': codec,
  356. 'ext': codec,
  357. 'is_live': True,
  358. }
  359. class NPORadioFragmentIE(InfoExtractor):
  360. IE_NAME = 'npo.nl:radio:fragment'
  361. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
  362. _TEST = {
  363. 'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
  364. 'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
  365. 'info_dict': {
  366. 'id': '174356',
  367. 'ext': 'mp3',
  368. 'title': 'Jubileumconcert Willeke Alberti',
  369. },
  370. }
  371. def _real_extract(self, url):
  372. audio_id = self._match_id(url)
  373. webpage = self._download_webpage(url, audio_id)
  374. title = self._html_search_regex(
  375. r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
  376. webpage, 'title')
  377. audio_url = self._search_regex(
  378. r"data-streams='([^']+)'", webpage, 'audio url')
  379. return {
  380. 'id': audio_id,
  381. 'url': audio_url,
  382. 'title': title,
  383. }
  384. class NPODataMidEmbedIE(InfoExtractor):
  385. def _real_extract(self, url):
  386. display_id = self._match_id(url)
  387. webpage = self._download_webpage(url, display_id)
  388. video_id = self._search_regex(
  389. r'data-mid=(["\'])(?P<id>(?:(?!\1).)+)\1', webpage, 'video_id', group='id')
  390. return {
  391. '_type': 'url_transparent',
  392. 'ie_key': 'NPO',
  393. 'url': 'npo:%s' % video_id,
  394. 'display_id': display_id
  395. }
  396. class SchoolTVIE(NPODataMidEmbedIE):
  397. IE_NAME = 'schooltv'
  398. _VALID_URL = r'https?://(?:www\.)?schooltv\.nl/video/(?P<id>[^/?#&]+)'
  399. _TEST = {
  400. 'url': 'http://www.schooltv.nl/video/ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam/',
  401. 'info_dict': {
  402. 'id': 'WO_NTR_429477',
  403. 'display_id': 'ademhaling-de-hele-dag-haal-je-adem-maar-wat-gebeurt-er-dan-eigenlijk-in-je-lichaam',
  404. 'title': 'Ademhaling: De hele dag haal je adem. Maar wat gebeurt er dan eigenlijk in je lichaam?',
  405. 'ext': 'mp4',
  406. 'description': 'md5:abfa0ff690adb73fd0297fd033aaa631'
  407. },
  408. 'params': {
  409. # Skip because of m3u8 download
  410. 'skip_download': True
  411. }
  412. }
  413. class HetKlokhuisIE(NPODataMidEmbedIE):
  414. IE_NAME = 'hetklokhuis'
  415. _VALID_URL = r'https?://(?:www\.)?hetklokhuis.nl/[^/]+/\d+/(?P<id>[^/?#&]+)'
  416. _TEST = {
  417. 'url': 'http://hetklokhuis.nl/tv-uitzending/3471/Zwaartekrachtsgolven',
  418. 'info_dict': {
  419. 'id': 'VPWON_1260528',
  420. 'display_id': 'Zwaartekrachtsgolven',
  421. 'ext': 'm4v',
  422. 'title': 'Het Klokhuis: Zwaartekrachtsgolven',
  423. 'description': 'md5:c94f31fb930d76c2efa4a4a71651dd48',
  424. 'upload_date': '20170223',
  425. },
  426. 'params': {
  427. 'skip_download': True
  428. }
  429. }
  430. class NPOPlaylistBaseIE(NPOIE):
  431. def _real_extract(self, url):
  432. playlist_id = self._match_id(url)
  433. webpage = self._download_webpage(url, playlist_id)
  434. entries = [
  435. self.url_result('npo:%s' % video_id if not video_id.startswith('http') else video_id)
  436. for video_id in orderedSet(re.findall(self._PLAYLIST_ENTRY_RE, webpage))
  437. ]
  438. playlist_title = self._html_search_regex(
  439. self._PLAYLIST_TITLE_RE, webpage, 'playlist title',
  440. default=None) or self._og_search_title(webpage)
  441. return self.playlist_result(entries, playlist_id, playlist_title)
  442. class VPROIE(NPOPlaylistBaseIE):
  443. IE_NAME = 'vpro'
  444. _VALID_URL = r'https?://(?:www\.)?(?:(?:tegenlicht\.)?vpro|2doc)\.nl/(?:[^/]+/)*(?P<id>[^/]+)\.html'
  445. _PLAYLIST_TITLE_RE = (r'<h1[^>]+class=["\'].*?\bmedia-platform-title\b.*?["\'][^>]*>([^<]+)',
  446. r'<h5[^>]+class=["\'].*?\bmedia-platform-subtitle\b.*?["\'][^>]*>([^<]+)')
  447. _PLAYLIST_ENTRY_RE = r'data-media-id="([^"]+)"'
  448. _TESTS = [
  449. {
  450. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  451. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  452. 'info_dict': {
  453. 'id': 'VPWON_1169289',
  454. 'ext': 'm4v',
  455. 'title': 'De toekomst komt uit Afrika',
  456. 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
  457. 'upload_date': '20130225',
  458. },
  459. 'skip': 'Video gone',
  460. },
  461. {
  462. 'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html',
  463. 'info_dict': {
  464. 'id': 'sergio-herman',
  465. 'title': 'sergio herman: fucking perfect',
  466. },
  467. 'playlist_count': 2,
  468. },
  469. {
  470. # playlist with youtube embed
  471. 'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html',
  472. 'info_dict': {
  473. 'id': 'education-education',
  474. 'title': 'education education',
  475. },
  476. 'playlist_count': 2,
  477. },
  478. {
  479. 'url': 'http://www.2doc.nl/documentaires/series/2doc/2015/oktober/de-tegenprestatie.html',
  480. 'info_dict': {
  481. 'id': 'de-tegenprestatie',
  482. 'title': 'De Tegenprestatie',
  483. },
  484. 'playlist_count': 2,
  485. }, {
  486. 'url': 'http://www.2doc.nl/speel~VARA_101375237~mh17-het-verdriet-van-nederland~.html',
  487. 'info_dict': {
  488. 'id': 'VARA_101375237',
  489. 'ext': 'm4v',
  490. 'title': 'MH17: Het verdriet van Nederland',
  491. 'description': 'md5:09e1a37c1fdb144621e22479691a9f18',
  492. 'upload_date': '20150716',
  493. },
  494. 'params': {
  495. # Skip because of m3u8 download
  496. 'skip_download': True
  497. },
  498. }
  499. ]
  500. class WNLIE(NPOPlaylistBaseIE):
  501. IE_NAME = 'wnl'
  502. _VALID_URL = r'https?://(?:www\.)?omroepwnl\.nl/video/detail/(?P<id>[^/]+)__\d+'
  503. _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class="subject"[^>]*>(.+?)</h1>'
  504. _PLAYLIST_ENTRY_RE = r'<a[^>]+href="([^"]+)"[^>]+class="js-mid"[^>]*>Deel \d+'
  505. _TESTS = [{
  506. 'url': 'http://www.omroepwnl.nl/video/detail/vandaag-de-dag-6-mei__060515',
  507. 'info_dict': {
  508. 'id': 'vandaag-de-dag-6-mei',
  509. 'title': 'Vandaag de Dag 6 mei',
  510. },
  511. 'playlist_count': 4,
  512. }]
  513. class AndereTijdenIE(NPOPlaylistBaseIE):
  514. IE_NAME = 'anderetijden'
  515. _VALID_URL = r'https?://(?:www\.)?anderetijden\.nl/programma/(?:[^/]+/)+(?P<id>[^/?#&]+)'
  516. _PLAYLIST_TITLE_RE = r'(?s)<h1[^>]+class=["\'].*?\bpage-title\b.*?["\'][^>]*>(.+?)</h1>'
  517. _PLAYLIST_ENTRY_RE = r'<figure[^>]+class=["\']episode-container episode-page["\'][^>]+data-prid=["\'](.+?)["\']'
  518. _TESTS = [{
  519. 'url': 'http://anderetijden.nl/programma/1/Andere-Tijden/aflevering/676/Duitse-soldaten-over-de-Slag-bij-Arnhem',
  520. 'info_dict': {
  521. 'id': 'Duitse-soldaten-over-de-Slag-bij-Arnhem',
  522. 'title': 'Duitse soldaten over de Slag bij Arnhem',
  523. },
  524. 'playlist_count': 3,
  525. }]