npo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_urllib_request,
  6. compat_urllib_parse,
  7. )
  8. from ..utils import (
  9. fix_xml_ampersands,
  10. parse_duration,
  11. qualities,
  12. strip_jsonp,
  13. unified_strdate,
  14. url_basename,
  15. )
  16. class NPOBaseIE(InfoExtractor):
  17. def _get_token(self, video_id):
  18. token_page = self._download_webpage(
  19. 'http://ida.omroep.nl/npoplayer/i.js',
  20. video_id, note='Downloading token')
  21. token = self._search_regex(
  22. r'npoplayer\.token = "(.+?)"', token_page, 'token')
  23. # Decryption algorithm extracted from http://npoplayer.omroep.nl/csjs/npoplayer-min.js
  24. token_l = list(token)
  25. first = second = None
  26. for i in range(5, len(token_l) - 4):
  27. if token_l[i].isdigit():
  28. if first is None:
  29. first = i
  30. elif second is None:
  31. second = i
  32. if first is None or second is None:
  33. first = 12
  34. second = 13
  35. token_l[first], token_l[second] = token_l[second], token_l[first]
  36. return ''.join(token_l)
  37. class NPOIE(NPOBaseIE):
  38. IE_NAME = 'npo'
  39. IE_DESC = 'npo.nl and ntr.nl'
  40. _VALID_URL = r'''(?x)
  41. (?:
  42. npo:|
  43. https?://
  44. (?:www\.)?
  45. (?:
  46. npo\.nl/(?!live|radio)(?:[^/]+/){2}|
  47. ntr\.nl/(?:[^/]+/){2,}|
  48. omroepwnl\.nl/video/fragment/[^/]+__
  49. )
  50. )
  51. (?P<id>[^/?#]+)
  52. '''
  53. _TESTS = [
  54. {
  55. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  56. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  57. 'info_dict': {
  58. 'id': 'VPWON_1220719',
  59. 'ext': 'm4v',
  60. 'title': 'Nieuwsuur',
  61. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  62. 'upload_date': '20140622',
  63. },
  64. },
  65. {
  66. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  67. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  68. 'info_dict': {
  69. 'id': 'VARA_101191800',
  70. 'ext': 'm4v',
  71. 'title': 'De Mega Mike & Mega Thomas show',
  72. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  73. 'upload_date': '20090227',
  74. 'duration': 2400,
  75. },
  76. },
  77. {
  78. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  79. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  80. 'info_dict': {
  81. 'id': 'VPWON_1169289',
  82. 'ext': 'm4v',
  83. 'title': 'Tegenlicht',
  84. 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
  85. 'upload_date': '20130225',
  86. 'duration': 3000,
  87. },
  88. },
  89. {
  90. 'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
  91. 'info_dict': {
  92. 'id': 'WO_VPRO_043706',
  93. 'ext': 'wmv',
  94. 'title': 'De nieuwe mens - Deel 1',
  95. 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
  96. 'duration': 4680,
  97. },
  98. 'params': {
  99. # mplayer mms download
  100. 'skip_download': True,
  101. }
  102. },
  103. # non asf in streams
  104. {
  105. 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
  106. 'md5': 'b3da13de374cbe2d5332a7e910bef97f',
  107. 'info_dict': {
  108. 'id': 'WO_NOS_762771',
  109. 'ext': 'mp4',
  110. 'title': 'Hoe gaat Europa verder na Parijs?',
  111. },
  112. },
  113. {
  114. 'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content',
  115. 'md5': '01c6a2841675995da1f0cf776f03a9c3',
  116. 'info_dict': {
  117. 'id': 'VPWON_1233944',
  118. 'ext': 'm4v',
  119. 'title': 'Aap, poot, pies',
  120. 'description': 'md5:c9c8005d1869ae65b858e82c01a91fde',
  121. 'upload_date': '20150508',
  122. 'duration': 599,
  123. },
  124. },
  125. {
  126. 'url': 'http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698',
  127. 'md5': 'd30cd8417b8b9bca1fdff27428860d08',
  128. 'info_dict': {
  129. 'id': 'POW_00996502',
  130. 'ext': 'm4v',
  131. 'title': '''"Dit is wel een 'landslide'..."''',
  132. 'description': 'md5:f8d66d537dfb641380226e31ca57b8e8',
  133. 'upload_date': '20150508',
  134. 'duration': 462,
  135. },
  136. }
  137. ]
  138. def _real_extract(self, url):
  139. video_id = self._match_id(url)
  140. return self._get_info(video_id)
  141. def _get_info(self, video_id):
  142. metadata = self._download_json(
  143. 'http://e.omroep.nl/metadata/%s' % video_id,
  144. video_id,
  145. # We have to remove the javascript callback
  146. transform_source=strip_jsonp,
  147. )
  148. # For some videos actual video id (prid) is different (e.g. for
  149. # http://www.omroepwnl.nl/video/fragment/vandaag-de-dag-verkiezingen__POMS_WNL_853698
  150. # video id is POMS_WNL_853698 but prid is POW_00996502)
  151. video_id = metadata.get('prid') or video_id
  152. token = self._get_token(video_id)
  153. formats = []
  154. pubopties = metadata.get('pubopties')
  155. if pubopties:
  156. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  157. for format_id in pubopties:
  158. format_info = self._download_json(
  159. 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
  160. % (video_id, format_id, token),
  161. video_id, 'Downloading %s JSON' % format_id)
  162. if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
  163. continue
  164. streams = format_info.get('streams')
  165. if streams:
  166. video_info = self._download_json(
  167. streams[0] + '&type=json',
  168. video_id, 'Downloading %s stream JSON' % format_id)
  169. else:
  170. video_info = format_info
  171. video_url = video_info.get('url')
  172. if not video_url:
  173. continue
  174. if format_id == 'adaptive':
  175. formats.extend(self._extract_m3u8_formats(video_url, video_id))
  176. else:
  177. formats.append({
  178. 'url': video_url,
  179. 'format_id': format_id,
  180. 'quality': quality(format_id),
  181. })
  182. streams = metadata.get('streams')
  183. if streams:
  184. for i, stream in enumerate(streams):
  185. stream_url = stream.get('url')
  186. if not stream_url:
  187. continue
  188. if '.asf' not in stream_url:
  189. formats.append({
  190. 'url': stream_url,
  191. 'quality': stream.get('kwaliteit'),
  192. })
  193. continue
  194. asx = self._download_xml(
  195. stream_url, video_id,
  196. 'Downloading stream %d ASX playlist' % i,
  197. transform_source=fix_xml_ampersands)
  198. ref = asx.find('./ENTRY/Ref')
  199. if ref is None:
  200. continue
  201. video_url = ref.get('href')
  202. if not video_url:
  203. continue
  204. formats.append({
  205. 'url': video_url,
  206. 'ext': stream.get('formaat', 'asf'),
  207. 'quality': stream.get('kwaliteit'),
  208. })
  209. self._sort_formats(formats)
  210. subtitles = {}
  211. if metadata.get('tt888') == 'ja':
  212. subtitles['nl'] = [{
  213. 'ext': 'vtt',
  214. 'url': 'http://e.omroep.nl/tt888/%s' % video_id,
  215. }]
  216. return {
  217. 'id': video_id,
  218. # prefer aflevering_titel if any since titel may be too generic, e.g.
  219. # http://tegenlicht.vpro.nl/afleveringen/2014-2015/access-to-africa.html
  220. 'title': metadata.get('aflevering_titel') or metadata['titel'],
  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 VPROIE(NPOIE):
  359. _VALID_URL = r'https?://(?:www\.)?(?:tegenlicht\.)?vpro\.nl/(?:[^/]+/){2,}(?P<id>[^/]+)\.html'
  360. _TESTS = [
  361. {
  362. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  363. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  364. 'info_dict': {
  365. 'id': 'VPWON_1169289',
  366. 'ext': 'm4v',
  367. 'title': 'De toekomst komt uit Afrika',
  368. 'description': 'md5:52cf4eefbc96fffcbdc06d024147abea',
  369. 'upload_date': '20130225',
  370. },
  371. },
  372. {
  373. 'url': 'http://www.vpro.nl/programmas/2doc/2015/sergio-herman.html',
  374. 'info_dict': {
  375. 'id': 'sergio-herman',
  376. 'title': 'Sergio Herman: Fucking perfect',
  377. },
  378. 'playlist_count': 2,
  379. },
  380. {
  381. # playlist with youtube embed
  382. 'url': 'http://www.vpro.nl/programmas/2doc/2015/education-education.html',
  383. 'info_dict': {
  384. 'id': 'education-education',
  385. 'title': '2Doc',
  386. },
  387. 'playlist_count': 2,
  388. }
  389. ]
  390. def _real_extract(self, url):
  391. playlist_id = self._match_id(url)
  392. webpage = self._download_webpage(url, playlist_id)
  393. entries = [
  394. self.url_result('npo:%s' % video_id if not video_id.startswith('http') else video_id)
  395. for video_id in re.findall(r'data-media-id="([^"]+)"', webpage)
  396. ]
  397. playlist_title = self._search_regex(
  398. r'<title>\s*([^>]+?)\s*-\s*Teledoc\s*-\s*VPRO\s*</title>',
  399. webpage, 'playlist title', default=None) or self._og_search_title(webpage)
  400. return self.playlist_result(entries, playlist_id, playlist_title)
  401. class WNLIE(InfoExtractor):
  402. _VALID_URL = r'https?://(?:www\.)?omroepwnl\.nl/video/detail/(?P<id>[^/]+)__\d+'
  403. _TEST = {
  404. 'url': 'http://www.omroepwnl.nl/video/detail/vandaag-de-dag-6-mei__060515',
  405. 'info_dict': {
  406. 'id': 'vandaag-de-dag-6-mei',
  407. 'title': 'Vandaag de Dag 6 mei',
  408. },
  409. 'playlist_count': 4,
  410. }
  411. def _real_extract(self, url):
  412. playlist_id = self._match_id(url)
  413. webpage = self._download_webpage(url, playlist_id)
  414. entries = [
  415. self.url_result('npo:%s' % video_id, 'NPO')
  416. for video_id, part in re.findall(
  417. r'<a[^>]+href="([^"]+)"[^>]+class="js-mid"[^>]*>(Deel \d+)', webpage)
  418. ]
  419. playlist_title = self._html_search_regex(
  420. r'(?s)<h1[^>]+class="subject"[^>]*>(.+?)</h1>',
  421. webpage, 'playlist title')
  422. return self.playlist_result(entries, playlist_id, playlist_title)