npo.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. unified_strdate,
  6. parse_duration,
  7. qualities,
  8. strip_jsonp,
  9. url_basename,
  10. fix_xml_ampersands,
  11. determine_ext,
  12. )
  13. class NPOIE(InfoExtractor):
  14. IE_NAME = 'npo.nl'
  15. _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
  16. _TESTS = [
  17. {
  18. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  19. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  20. 'info_dict': {
  21. 'id': 'VPWON_1220719',
  22. 'ext': 'm4v',
  23. 'title': 'Nieuwsuur',
  24. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  25. 'upload_date': '20140622',
  26. },
  27. },
  28. {
  29. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  30. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  31. 'info_dict': {
  32. 'id': 'VARA_101191800',
  33. 'ext': 'm4v',
  34. 'title': 'De Mega Mike & Mega Thomas show',
  35. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  36. 'upload_date': '20090227',
  37. 'duration': 2400,
  38. },
  39. },
  40. {
  41. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  42. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  43. 'info_dict': {
  44. 'id': 'VPWON_1169289',
  45. 'ext': 'm4v',
  46. 'title': 'Tegenlicht',
  47. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  48. 'upload_date': '20130225',
  49. 'duration': 3000,
  50. },
  51. },
  52. {
  53. 'url': 'http://www.npo.nl/de-nieuwe-mens-deel-1/21-07-2010/WO_VPRO_043706',
  54. 'info_dict': {
  55. 'id': 'WO_VPRO_043706',
  56. 'ext': 'wmv',
  57. 'title': 'De nieuwe mens - Deel 1',
  58. 'description': 'md5:518ae51ba1293ffb80d8d8ce90b74e4b',
  59. 'duration': 4680,
  60. },
  61. 'params': {
  62. # mplayer mms download
  63. 'skip_download': True,
  64. }
  65. },
  66. # non asf in streams
  67. {
  68. 'url': 'http://www.npo.nl/hoe-gaat-europa-verder-na-parijs/10-01-2015/WO_NOS_762771',
  69. 'md5': 'b3da13de374cbe2d5332a7e910bef97f',
  70. 'info_dict': {
  71. 'id': 'WO_NOS_762771',
  72. 'ext': 'mp4',
  73. 'title': 'Hoe gaat Europa verder na Parijs?',
  74. },
  75. },
  76. ]
  77. def _real_extract(self, url):
  78. mobj = re.match(self._VALID_URL, url)
  79. video_id = mobj.group('id')
  80. return self._get_info(video_id)
  81. def _get_info(self, video_id):
  82. metadata = self._download_json(
  83. 'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
  84. video_id,
  85. # We have to remove the javascript callback
  86. transform_source=strip_jsonp,
  87. )
  88. token_page = self._download_webpage(
  89. 'http://ida.omroep.nl/npoplayer/i.js',
  90. video_id,
  91. note='Downloading token'
  92. )
  93. token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
  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 not '.asf' 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 TegenlichtVproIE(NPOIE):
  161. IE_NAME = 'tegenlicht.vpro.nl'
  162. _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
  163. _TESTS = [
  164. {
  165. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  166. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  167. 'info_dict': {
  168. 'id': 'VPWON_1169289',
  169. 'ext': 'm4v',
  170. 'title': 'Tegenlicht',
  171. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  172. 'upload_date': '20130225',
  173. },
  174. },
  175. ]
  176. def _real_extract(self, url):
  177. name = url_basename(url)
  178. webpage = self._download_webpage(url, name)
  179. urn = self._html_search_meta('mediaurn', webpage)
  180. info_page = self._download_json(
  181. 'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
  182. return self._get_info(info_page['mid'])