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.

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