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.

528 lines
18 KiB

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