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.

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