npo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. )
  9. class NPOIE(InfoExtractor):
  10. IE_NAME = 'npo.nl'
  11. _VALID_URL = r'https?://www\.npo\.nl/[^/]+/[^/]+/(?P<id>[^/?]+)'
  12. _TESTS = [
  13. {
  14. 'url': 'http://www.npo.nl/nieuwsuur/22-06-2014/VPWON_1220719',
  15. 'md5': '4b3f9c429157ec4775f2c9cb7b911016',
  16. 'info_dict': {
  17. 'id': 'VPWON_1220719',
  18. 'ext': 'm4v',
  19. 'title': 'Nieuwsuur',
  20. 'description': 'Dagelijks tussen tien en elf: nieuws, sport en achtergronden.',
  21. 'upload_date': '20140622',
  22. },
  23. },
  24. {
  25. 'url': 'http://www.npo.nl/de-mega-mike-mega-thomas-show/27-02-2009/VARA_101191800',
  26. 'md5': 'da50a5787dbfc1603c4ad80f31c5120b',
  27. 'info_dict': {
  28. 'id': 'VARA_101191800',
  29. 'ext': 'm4v',
  30. 'title': 'De Mega Mike & Mega Thomas show',
  31. 'description': 'md5:3b74c97fc9d6901d5a665aac0e5400f4',
  32. 'upload_date': '20090227',
  33. 'duration': 2400,
  34. },
  35. },
  36. {
  37. 'url': 'http://www.npo.nl/tegenlicht/25-02-2013/VPWON_1169289',
  38. 'md5': 'f8065e4e5a7824068ed3c7e783178f2c',
  39. 'info_dict': {
  40. 'id': 'VPWON_1169289',
  41. 'ext': 'm4v',
  42. 'title': 'Tegenlicht',
  43. 'description': 'md5:d6476bceb17a8c103c76c3b708f05dd1',
  44. 'upload_date': '20130225',
  45. 'duration': 3000,
  46. },
  47. }
  48. ]
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VALID_URL, url)
  51. video_id = mobj.group('id')
  52. metadata = self._download_json(
  53. 'http://e.omroep.nl/metadata/aflevering/%s' % video_id,
  54. video_id,
  55. # We have to remove the javascript callback
  56. transform_source=lambda j: re.sub(r'parseMetadata\((.*?)\);\n//.*$', r'\1', j)
  57. )
  58. token_page = self._download_webpage(
  59. 'http://ida.omroep.nl/npoplayer/i.js',
  60. video_id,
  61. note='Downloading token'
  62. )
  63. token = self._search_regex(r'npoplayer\.token = "(.+?)"', token_page, 'token')
  64. formats = []
  65. quality = qualities(['adaptive', 'wmv_sb', 'h264_sb', 'wmv_bb', 'h264_bb', 'wvc1_std', 'h264_std'])
  66. for format_id in metadata['pubopties']:
  67. format_info = self._download_json(
  68. 'http://ida.omroep.nl/odi/?prid=%s&puboptions=%s&adaptive=yes&token=%s' % (video_id, format_id, token),
  69. video_id, 'Downloading %s JSON' % format_id)
  70. if format_info.get('error_code', 0) or format_info.get('errorcode', 0):
  71. continue
  72. streams = format_info.get('streams')
  73. if streams:
  74. video_info = self._download_json(
  75. streams[0] + '&type=json',
  76. video_id, 'Downloading %s stream JSON' % format_id)
  77. else:
  78. video_info = format_info
  79. video_url = video_info.get('url')
  80. if not video_url:
  81. continue
  82. if format_id == 'adaptive':
  83. formats.extend(self._extract_m3u8_formats(video_url, video_id))
  84. else:
  85. formats.append({
  86. 'url': video_url,
  87. 'format_id': format_id,
  88. 'quality': quality(format_id),
  89. })
  90. self._sort_formats(formats)
  91. return {
  92. 'id': video_id,
  93. 'title': metadata['titel'],
  94. 'description': metadata['info'],
  95. 'thumbnail': metadata.get('images', [{'url': None}])[-1]['url'],
  96. 'upload_date': unified_strdate(metadata.get('gidsdatum')),
  97. 'duration': parse_duration(metadata.get('tijdsduur')),
  98. 'formats': formats,
  99. }