npo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. fix_xml_ampersands,
  5. parse_duration,
  6. qualities,
  7. strip_jsonp,
  8. unified_strdate,
  9. url_basename,
  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'https?://(?:www\.)?(?:npo|ntr)\.nl/(?!live|radio)(?:[^/]+/){2,}(?P<id>[^/?#]+)'
  36. _TESTS = [
  37. {
  38. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  39. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  40. 'info_dict': {
  41. 'id': 'VPWON_1220719',
  42. 'ext': 'm4v',
  43. 'title': 'Nieuwsuur',
  44. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  45. 'upload_date': '20140622',
  46. },
  47. },
  48. {
  49. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  50. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  51. 'info_dict': {
  52. 'id': 'VARA_101191800',
  53. 'ext': 'm4v',
  54. 'title': 'De Mega Mike & Mega Thomas show',
  55. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  56. 'upload_date': '20090227',
  57. 'duration': 2400,
  58. },
  59. },
  60. {
  61. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  62. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  63. 'info_dict': {
  64. 'id': 'VPWON_1169289',
  65. 'ext': 'm4v',
  66. 'title': 'Tegenlicht',
  67. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  68. 'upload_date': '20130225',
  69. 'duration': 3000,
  70. },
  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': 'wmv',
  77. 'title': 'De nieuwe mens - Deel 1',
  78. 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
  79. 'duration': 4680,
  80. },
  81. 'params': {
  82. # mplayer mms download
  83. 'skip_download': True,
  84. }
  85. },
  86. # non asf in streams
  87. {
  88. 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
  89. 'md5': 'b3da13de374cbe2d5332a7e910bef97f',
  90. 'info_dict': {
  91. 'id': 'WO_NOS_762771',
  92. 'ext': 'mp4',
  93. 'title': 'Hoe gaat Europa verder na Parijs?',
  94. },
  95. },
  96. {
  97. 'url': 'http://www.ntr.nl/Aap-Poot-Pies/27/detail/Aap-poot-pies/VPWON_1233944#content',
  98. 'md5': '01c6a2841675995da1f0cf776f03a9c3',
  99. 'info_dict': {
  100. 'id': 'VPWON_1233944',
  101. 'ext': 'm4v',
  102. 'title': 'Aap, poot, pies',
  103. 'description': 'md5:c9c8005d1869ae65b858e82c01a91fde',
  104. 'upload_date': '20150508',
  105. 'duration': 599,
  106. },
  107. }
  108. ]
  109. def _real_extract(self, url):
  110. video_id = self._match_id(url)
  111. return self._get_info(video_id)
  112. def _get_info(self, video_id):
  113. metadata = self._download_json(
  114. 'http://e.omroep.nl/metadata/%s' % video_id,
  115. video_id,
  116. # We have to remove the javascript callback
  117. transform_source=strip_jsonp,
  118. )
  119. token = self._get_token(video_id)
  120. formats = []
  121. pubopties = metadata.get('pubopties')
  122. if pubopties:
  123. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  124. for format_id in pubopties:
  125. format_info = self._download_json(
  126. 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
  127. % (video_id, format_id, token),
  128. video_id, 'Downloading %s JSON' % format_id)
  129. if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
  130. continue
  131. streams = format_info.get('streams')
  132. if streams:
  133. video_info = self._download_json(
  134. streams[0] + '&type=json',
  135. video_id, 'Downloading %s stream JSON' % format_id)
  136. else:
  137. video_info = format_info
  138. video_url = video_info.get('url')
  139. if not video_url:
  140. continue
  141. if format_id == 'adaptive':
  142. formats.extend(self._extract_m3u8_formats(video_url, video_id))
  143. else:
  144. formats.append({
  145. 'url': video_url,
  146. 'format_id': format_id,
  147. 'quality': quality(format_id),
  148. })
  149. streams = metadata.get('streams')
  150. if streams:
  151. for i, stream in enumerate(streams):
  152. stream_url = stream.get('url')
  153. if not stream_url:
  154. continue
  155. if '.asf' not in stream_url:
  156. formats.append({
  157. 'url': stream_url,
  158. 'quality': stream.get('kwaliteit'),
  159. })
  160. continue
  161. asx = self._download_xml(
  162. stream_url, video_id,
  163. 'Downloading stream %d ASX playlist' % i,
  164. transform_source=fix_xml_ampersands)
  165. ref = asx.find('./ENTRY/Ref')
  166. if ref is None:
  167. continue
  168. video_url = ref.get('href')
  169. if not video_url:
  170. continue
  171. formats.append({
  172. 'url': video_url,
  173. 'ext': stream.get('formaat', 'asf'),
  174. 'quality': stream.get('kwaliteit'),
  175. })
  176. self._sort_formats(formats)
  177. subtitles = {}
  178. if metadata.get('tt888') == 'ja':
  179. subtitles['nl'] = [{
  180. 'ext': 'vtt',
  181. 'url': 'http://e.omroep.nl/tt888/%s' % video_id,
  182. }]
  183. return {
  184. 'id': video_id,
  185. 'title': metadata['titel'],
  186. 'description': metadata['info'],
  187. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  188. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  189. 'duration': parse_duration(metadata.get('tijdsduur')),
  190. 'formats': formats,
  191. 'subtitles': subtitles,
  192. }
  193. class NPOLiveIE(NPOBaseIE):
  194. IE_NAME = 'npo.nl:live'
  195. _VALID_URL = r'https?://(?:www\.)?npo\.nl/live/(?P<id>.+)'
  196. _TEST = {
  197. 'url': 'http://www.npo.nl/live/npo-1',
  198. 'info_dict': {
  199. 'id': 'LI_NEDERLAND1_136692',
  200. 'display_id': 'npo-1',
  201. 'ext': 'mp4',
  202. 'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  203. 'description': 'Livestream',
  204. 'is_live': True,
  205. },
  206. 'params': {
  207. 'skip_download': True,
  208. }
  209. }
  210. def _real_extract(self, url):
  211. display_id = self._match_id(url)
  212. webpage = self._download_webpage(url, display_id)
  213. live_id = self._search_regex(
  214. r'data-prid="([^"]+)"', webpage, 'live id')
  215. metadata = self._download_json(
  216. 'http://e.omroep.nl/metadata/%s' % live_id,
  217. display_id, transform_source=strip_jsonp)
  218. token = self._get_token(display_id)
  219. formats = []
  220. streams = metadata.get('streams')
  221. if streams:
  222. for stream in streams:
  223. stream_type = stream.get('type').lower()
  224. # smooth streaming is not supported
  225. if stream_type in ['ss', 'ms']:
  226. continue
  227. stream_info = self._download_json(
  228. 'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp'
  229. % (stream.get('url'), token),
  230. display_id, 'Downloading %s JSON' % stream_type)
  231. if stream_info.get('error_code', 0) or stream_info.get('errorcode', 0):
  232. continue
  233. stream_url = self._download_json(
  234. stream_info['stream'], display_id,
  235. 'Downloading %s URL' % stream_type,
  236. 'Unable to download %s URL' % stream_type,
  237. transform_source=strip_jsonp, fatal=False)
  238. if not stream_url:
  239. continue
  240. if stream_type == 'hds':
  241. f4m_formats = self._extract_f4m_formats(stream_url, display_id)
  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(stream_url, display_id, 'mp4'))
  248. else:
  249. formats.append({
  250. 'url': stream_url,
  251. 'preference': -10,
  252. })
  253. self._sort_formats(formats)
  254. return {
  255. 'id': live_id,
  256. 'display_id': display_id,
  257. 'title': self._live_title(metadata['titel']),
  258. 'description': metadata['info'],
  259. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  260. 'formats': formats,
  261. 'is_live': True,
  262. }
  263. class NPORadioIE(InfoExtractor):
  264. IE_NAME = 'npo.nl:radio'
  265. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/(?P<id>[^/]+)/?$'
  266. _TEST = {
  267. 'url': 'http://www.npo.nl/radio/radio-1',
  268. 'info_dict': {
  269. 'id': 'radio-1',
  270. 'ext': 'mp3',
  271. 'title': 're:^NPO Radio 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  272. 'is_live': True,
  273. },
  274. 'params': {
  275. 'skip_download': True,
  276. }
  277. }
  278. @staticmethod
  279. def _html_get_attribute_regex(attribute):
  280. return r'{0}\s*=\s*\'([^\']+)\''.format(attribute)
  281. def _real_extract(self, url):
  282. video_id = self._match_id(url)
  283. webpage = self._download_webpage(url, video_id)
  284. title = self._html_search_regex(
  285. self._html_get_attribute_regex('data-channel'), webpage, 'title')
  286. stream = self._parse_json(
  287. self._html_search_regex(self._html_get_attribute_regex('data-streams'), webpage, 'data-streams'),
  288. video_id)
  289. codec = stream.get('codec')
  290. return {
  291. 'id': video_id,
  292. 'url': stream['url'],
  293. 'title': self._live_title(title),
  294. 'acodec': codec,
  295. 'ext': codec,
  296. 'is_live': True,
  297. }
  298. class NPORadioFragmentIE(InfoExtractor):
  299. IE_NAME = 'npo.nl:radio:fragment'
  300. _VALID_URL = r'https?://(?:www\.)?npo\.nl/radio/[^/]+/fragment/(?P<id>\d+)'
  301. _TEST = {
  302. 'url': 'http://www.npo.nl/radio/radio-5/fragment/174356',
  303. 'md5': 'dd8cc470dad764d0fdc70a9a1e2d18c2',
  304. 'info_dict': {
  305. 'id': '174356',
  306. 'ext': 'mp3',
  307. 'title': 'Jubileumconcert Willeke Alberti',
  308. },
  309. }
  310. def _real_extract(self, url):
  311. audio_id = self._match_id(url)
  312. webpage = self._download_webpage(url, audio_id)
  313. title = self._html_search_regex(
  314. r'href="/radio/[^/]+/fragment/%s" title="([^"]+)"' % audio_id,
  315. webpage, 'title')
  316. audio_url = self._search_regex(
  317. r"data-streams='([^']+)'", webpage, 'audio url')
  318. return {
  319. 'id': audio_id,
  320. 'url': audio_url,
  321. 'title': title,
  322. }
  323. class TegenlichtVproIE(NPOIE):
  324. IE_NAME = 'tegenlicht.vpro.nl'
  325. _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
  326. _TESTS = [
  327. {
  328. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  329. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  330. 'info_dict': {
  331. 'id': 'VPWON_1169289',
  332. 'ext': 'm4v',
  333. 'title': 'Tegenlicht',
  334. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  335. 'upload_date': '20130225',
  336. },
  337. },
  338. ]
  339. def _real_extract(self, url):
  340. name = url_basename(url)
  341. webpage = self._download_webpage(url, name)
  342. urn = self._html_search_meta('mediaurn', webpage)
  343. info_page = self._download_json(
  344. 'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
  345. return self._get_info(info_page['mid'])