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.

559 lines
20 KiB

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