npo.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. ]
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. video_id = mobj.group('id')
  70. return self._get_info(video_id)
  71. def _get_info(self, video_id):
  72. metadata = self._download_json(
  73. 'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
  74. video_id,
  75. # We have to remove the javascript callback
  76. transform_source=strip_jsonp,
  77. )
  78. token_page = self._download_webpage(
  79. 'http://ida.omroep.nl/npoplayer/i.js',
  80. video_id,
  81. note='Downloading token'
  82. )
  83. token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
  84. formats = []
  85. pubopties = metadata.get('pubopties')
  86. if pubopties:
  87. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  88. for format_id in pubopties:
  89. format_info = self._download_json(
  90. 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s'
  91. % (video_id, format_id, token),
  92. video_id, 'Downloading %s JSON' % format_id)
  93. if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
  94. continue
  95. streams = format_info.get('streams')
  96. if streams:
  97. video_info = self._download_json(
  98. streams[0] + '&type=json',
  99. video_id, 'Downloading %s stream JSON' % format_id)
  100. else:
  101. video_info = format_info
  102. video_url = video_info.get('url')
  103. if not video_url:
  104. continue
  105. if format_id == 'adaptive':
  106. formats.extend(self._extract_m3u8_formats(video_url, video_id))
  107. else:
  108. formats.append({
  109. 'url': video_url,
  110. 'format_id': format_id,
  111. 'quality': quality(format_id),
  112. })
  113. streams = metadata.get('streams')
  114. if streams:
  115. for i, stream in enumerate(streams):
  116. stream_url = stream.get('url')
  117. if not stream_url:
  118. continue
  119. if determine_ext(stream_url).lower() != 'asf':
  120. formats.append({
  121. 'url': stream_url,
  122. 'quality': stream.get('kwaliteit'),
  123. })
  124. continue
  125. asx = self._download_xml(
  126. stream_url, video_id,
  127. 'Downloading stream %d ASX playlist' % i,
  128. transform_source=fix_xml_ampersands)
  129. ref = asx.find('./ENTRY/Ref')
  130. if ref is None:
  131. continue
  132. video_url = ref.get('href')
  133. if not video_url:
  134. continue
  135. formats.append({
  136. 'url': video_url,
  137. 'ext': stream.get('formaat', 'asf'),
  138. 'quality': stream.get('kwaliteit'),
  139. })
  140. self._sort_formats(formats)
  141. return {
  142. 'id': video_id,
  143. 'title': metadata['titel'],
  144. 'description': metadata['info'],
  145. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  146. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  147. 'duration': parse_duration(metadata.get('tijdsduur')),
  148. 'formats': formats,
  149. }
  150. class TegenlichtVproIE(NPOIE):
  151. IE_NAME = 'tegenlicht.vpro.nl'
  152. _VALID_URL = r'https?://tegenlicht\.vpro\.nl/afleveringen/.*?'
  153. _TESTS = [
  154. {
  155. 'url': 'http://tegenlicht.vpro.nl/afleveringen/2012-2013/de-toekomst-komt-uit-afrika.html',
  156. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  157. 'info_dict': {
  158. 'id': 'VPWON_1169289',
  159. 'ext': 'm4v',
  160. 'title': 'Tegenlicht',
  161. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  162. 'upload_date': '20130225',
  163. },
  164. },
  165. ]
  166. def _real_extract(self, url):
  167. name = url_basename(url)
  168. webpage = self._download_webpage(url, name)
  169. urn = self._html_search_meta('mediaurn', webpage)
  170. info_page = self._download_json(
  171. 'http://rs.vpro.nl/v2/api/media/%s.json' % urn, name)
  172. return self._get_info(info_page['mid'])