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.

383 lines
13 KiB

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