2
0

npo.py 15 KB

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