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.

278 lines
9.8 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. fix_xml_ampersands,
  6. parse_duration,
  7. qualities,
  8. strip_jsonp,
  9. unified_strdate,
  10. url_basename,
  11. )
  12. class NPOBaseIE(InfoExtractor):
  13. def _get_token(self, video_id):
  14. token_page = self._download_webpage(
  15. 'http://ida.omroep.nl/npoplayer/i.js',
  16. video_id, note='Downloading token')
  17. return self._search_regex(
  18. r'npoplayer\.token = "(.+?)"', token_page, 'token')
  19. class NPOIE(NPOBaseIE):
  20. IE_NAME = 'npo.nl'
  21. _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
  22. _TESTS = [
  23. {
  24. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  25. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  26. 'info_dict': {
  27. 'id': 'VPWON_1220719',
  28. 'ext': 'm4v',
  29. 'title': 'Nieuwsuur',
  30. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  31. 'upload_date': '20140622',
  32. },
  33. },
  34. {
  35. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  36. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  37. 'info_dict': {
  38. 'id': 'VARA_101191800',
  39. 'ext': 'm4v',
  40. 'title': 'De Mega Mike & Mega Thomas show',
  41. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  42. 'upload_date': '20090227',
  43. 'duration': 2400,
  44. },
  45. },
  46. {
  47. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  48. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  49. 'info_dict': {
  50. 'id': 'VPWON_1169289',
  51. 'ext': 'm4v',
  52. 'title': 'Tegenlicht',
  53. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  54. 'upload_date': '20130225',
  55. 'duration': 3000,
  56. },
  57. },
  58. {
  59. 'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
  60. 'info_dict': {
  61. 'id': 'WO_VPRO_043706',
  62. 'ext': 'wmv',
  63. 'title': 'De nieuwe mens - Deel 1',
  64. 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
  65. 'duration': 4680,
  66. },
  67. 'params': {
  68. # mplayer mms download
  69. 'skip_download': True,
  70. }
  71. },
  72. # non asf in streams
  73. {
  74. 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
  75. 'md5': 'b3da13de374cbe2d5332a7e910bef97f',
  76. 'info_dict': {
  77. 'id': 'WO_NOS_762771',
  78. 'ext': 'mp4',
  79. 'title': 'Hoe gaat Europa verder na Parijs?',
  80. },
  81. },
  82. ]
  83. def _real_extract(self, url):
  84. video_id = self._match_id(url)
  85. return self._get_info(video_id)
  86. def _get_info(self, video_id):
  87. metadata = self._download_json(
  88. 'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
  89. video_id,
  90. # We have to remove the javascript callback
  91. transform_source=strip_jsonp,
  92. )
  93. token = self._get_token(video_id)
  94. formats = []
  95. pubopties = metadata.get('pubopties')
  96. if pubopties:
  97. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  98. for format_id in pubopties:
  99. format_info = self._download_json(
  100. 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
  101. % (video_id, format_id, token),
  102. video_id, 'Downloading %s JSON' % format_id)
  103. if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
  104. continue
  105. streams = format_info.get('streams')
  106. if streams:
  107. video_info = self._download_json(
  108. streams[0] + '&type=json',
  109. video_id, 'Downloading %s stream JSON' % format_id)
  110. else:
  111. video_info = format_info
  112. video_url = video_info.get('url')
  113. if not video_url:
  114. continue
  115. if format_id == 'adaptive':
  116. formats.extend(self._extract_m3u8_formats(video_url, video_id))
  117. else:
  118. formats.append({
  119. 'url': video_url,
  120. 'format_id': format_id,
  121. 'quality': quality(format_id),
  122. })
  123. streams = metadata.get('streams')
  124. if streams:
  125. for i, stream in enumerate(streams):
  126. stream_url = stream.get('url')
  127. if not stream_url:
  128. continue
  129. if '.asf' not in stream_url:
  130. formats.append({
  131. 'url': stream_url,
  132. 'quality': stream.get('kwaliteit'),
  133. })
  134. continue
  135. asx = self._download_xml(
  136. stream_url, video_id,
  137. 'Downloading stream %d ASX playlist' % i,
  138. transform_source=fix_xml_ampersands)
  139. ref = asx.find('./ENTRY/Ref')
  140. if ref is None:
  141. continue
  142. video_url = ref.get('href')
  143. if not video_url:
  144. continue
  145. formats.append({
  146. 'url': video_url,
  147. 'ext': stream.get('formaat', 'asf'),
  148. 'quality': stream.get('kwaliteit'),
  149. })
  150. self._sort_formats(formats)
  151. return {
  152. 'id': video_id,
  153. 'title': metadata['titel'],
  154. 'description': metadata['info'],
  155. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  156. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  157. 'duration': parse_duration(metadata.get('tijdsduur')),
  158. 'formats': formats,
  159. }
  160. class NPOLiveIE(NPOBaseIE):
  161. IE_NAME = 'npo.nl:live'
  162. _VALID_URL = r'https?://www\.npo\.nl/live/(?P<id>.+)'
  163. _TEST = {
  164. 'url': 'http://www.npo.nl/live/npo-1',
  165. 'info_dict': {
  166. 'id': 'LI_NEDERLAND1_136692',
  167. 'display_id': 'npo-1',
  168. 'ext': 'mp4',
  169. 'title': 're:^Nederland 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  170. 'description': 'Livestream',
  171. 'is_live': True,
  172. },
  173. 'params': {
  174. 'skip_download': True,
  175. }
  176. }
  177. def _real_extract(self, url):
  178. display_id = self._match_id(url)
  179. webpage = self._download_webpage(url, display_id)
  180. live_id = self._search_regex(
  181. r'data-prid="([^"]+)"', webpage, 'live id')
  182. metadata = self._download_json(
  183. 'http://e.omroep.nl/metadata/%s' % live_id,
  184. display_id, transform_source=strip_jsonp)
  185. token = self._get_token(display_id)
  186. formats = []
  187. streams = metadata.get('streams')
  188. if streams:
  189. for stream in streams:
  190. stream_type = stream.get('type').lower()
  191. if stream_type == 'ss':
  192. continue
  193. stream_info = self._download_json(
  194. 'http://ida.omroep.nl/aapi/?stream=%s&token=%s&type=jsonp'
  195. % (stream.get('url'), token),
  196. display_id, 'Downloading %s JSON' % stream_type)
  197. if stream_info.get('error_code', 0) or stream_info.get('errorcode', 0):
  198. continue
  199. stream_url = self._download_json(
  200. stream_info['stream'], display_id,
  201. 'Downloading %s URL' % stream_type,
  202. transform_source=strip_jsonp)
  203. if stream_type == 'hds':
  204. f4m_formats = self._extract_f4m_formats(stream_url, display_id)
  205. # f4m downloader downloads only piece of live stream
  206. for f4m_format in f4m_formats:
  207. f4m_format['preference'] = -1
  208. formats.extend(f4m_formats)
  209. elif stream_type == 'hls':
  210. formats.extend(self._extract_m3u8_formats(stream_url, display_id, 'mp4'))
  211. else:
  212. formats.append({
  213. 'url': stream_url,
  214. })
  215. self._sort_formats(formats)
  216. return {
  217. 'id': live_id,
  218. 'display_id': display_id,
  219. 'title': self._live_title(metadata['titel']),
  220. 'description': metadata['info'],
  221. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  222. 'formats': formats,
  223. 'is_live': True,
  224. }
  225. class TegenlichtVproIE(NPOIE):
  226. IE_NAME = 'tegenlicht.vpro.nl'
  227. _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
  228. _TESTS = [
  229. {
  230. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  231. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  232. 'info_dict': {
  233. 'id': 'VPWON_1169289',
  234. 'ext': 'm4v',
  235. 'title': 'Tegenlicht',
  236. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  237. 'upload_date': '20130225',
  238. },
  239. },
  240. ]
  241. def _real_extract(self, url):
  242. name = url_basename(url)
  243. webpage = self._download_webpage(url, name)
  244. urn = self._html_search_meta('mediaurn', webpage)
  245. info_page = self._download_json(
  246. 'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
  247. return self._get_info(info_page['mid'])