npo.py 7.0 KB

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