nzherald.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import json
  2. from .brightcove import BrightcoveNewIE
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. ExtractorError,
  7. traverse_obj
  8. )
  9. class NZHeraldIE(InfoExtractor):
  10. IE_NAME = 'nzherald'
  11. _VALID_URL = r'https?://(?:www\.)?nzherald\.co\.nz/[\w\/-]+\/(?P<id>[A-Z0-9]+)'
  12. _TESTS = [
  13. {
  14. # Video accessible under 'video' key
  15. 'url': 'https://www.nzherald.co.nz/nz/queen-elizabeth-death-nz-public-holiday-announced-for-september-26/CEOPBSXO2JDCLNK3H7E3BIE2FA/',
  16. 'info_dict': {
  17. 'id': '6312191736112',
  18. 'ext': 'mp4',
  19. 'title': 'Focus: PM holds post-Cabinet press conference',
  20. 'duration': 238.08,
  21. 'upload_date': '20220912',
  22. 'uploader_id': '1308227299001',
  23. 'timestamp': 1662957159,
  24. 'tags': [],
  25. 'thumbnail': r're:https?://.*\.jpg$',
  26. 'description': 'md5:2f17713fcbfcfbe38bb9e7dfccbb0f2e',
  27. }
  28. }, {
  29. # Webpage has brightcove embed player url
  30. 'url': 'https://www.nzherald.co.nz/travel/pencarrow-coastal-trail/HDVTPJEPP46HJ2UEMK4EGD2DFI/',
  31. 'info_dict': {
  32. 'id': '6261791733001',
  33. 'ext': 'mp4',
  34. 'title': 'Pencarrow Coastal Trail',
  35. 'timestamp': 1625102897,
  36. 'upload_date': '20210701',
  37. 'uploader_id': '1308227299001',
  38. 'description': 'md5:d361aaa0c6498f7ac1bc4fc0a0aec1e4',
  39. 'thumbnail': r're:https?://.*\.jpg$',
  40. 'tags': ['travel', 'video'],
  41. 'duration': 43.627,
  42. }
  43. }, {
  44. # two video embeds of the same video
  45. 'url': 'https://www.nzherald.co.nz/nz/truck-driver-captured-cutting-off-motorist-on-state-highway-1-in-canterbury/FIHNJB7PLLPHWQPK4S7ZBDUC4I/',
  46. 'info_dict': {
  47. 'id': '6251114530001',
  48. 'ext': 'mp4',
  49. 'title': 'Truck travelling north from Rakaia runs car off road',
  50. 'timestamp': 1619730509,
  51. 'upload_date': '20210429',
  52. 'uploader_id': '1308227299001',
  53. 'description': 'md5:4cae7dfb7613ac4c73b9e73a75c6b5d7'
  54. },
  55. 'skip': 'video removed',
  56. }, {
  57. # customVideo embed requiring additional API call
  58. 'url': 'https://www.nzherald.co.nz/nz/politics/reserve-bank-rejects-political-criticisms-stands-by-review/2JO5Q4WLZRCBBNWTLACZMOP4RA/',
  59. 'info_dict': {
  60. 'id': '6315123873112',
  61. 'ext': 'mp4',
  62. 'timestamp': 1667862725,
  63. 'title': 'Focus: Luxon on re-appointment of Reserve Bank governor Adrian Orr',
  64. 'upload_date': '20221107',
  65. 'description': 'md5:df2f1f7033a8160c66e28e4743f5d934',
  66. 'uploader_id': '1308227299001',
  67. 'tags': ['video', 'nz herald focus', 'politics', 'politics videos'],
  68. 'thumbnail': r're:https?://.*\.jpg$',
  69. 'duration': 99.584,
  70. }
  71. }, {
  72. 'url': 'https://www.nzherald.co.nz/kahu/kaupapa-companies-my-taiao-supporting-maori-in-study-and-business/PQBO2J25WCG77VGRX7W7BVYEAI/',
  73. 'only_matching': True
  74. }, {
  75. 'url': 'https://nzherald.co.nz/the-country/video/focus-nzs-first-mass-covid-19-vaccination-event/N5I7IL3BRFLZSD33TLDLYJDGK4/',
  76. 'only_matching': True
  77. }, {
  78. 'url': 'https://www.nzherald.co.nz/the-vision-is-clear/news/tvic-damian-roper-planting-trees-an-addiction/AN2AAEPNRK5VLISDWQAJZB6ATQ',
  79. 'only_matching': True
  80. }
  81. ]
  82. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/1308227299001/S1BXZn8t_default/index.html?videoId=%s'
  83. def _extract_bc_embed_url(self, webpage):
  84. """The initial webpage may include the brightcove player embed url"""
  85. bc_url = BrightcoveNewIE._extract_url(self, webpage)
  86. return bc_url or self._search_regex(
  87. r'(?:embedUrl)\"\s*:\s*\"(?P<embed_url>%s)' % BrightcoveNewIE._VALID_URL,
  88. webpage, 'embed url', default=None, group='embed_url')
  89. def _real_extract(self, url):
  90. article_id = self._match_id(url)
  91. webpage = self._download_webpage(url, article_id)
  92. bc_url = self._extract_bc_embed_url(webpage)
  93. if not bc_url:
  94. fusion_metadata = self._parse_json(
  95. self._search_regex(r'Fusion\.globalContent\s*=\s*({.+?})\s*;', webpage, 'fusion metadata'), article_id)
  96. video_metadata = fusion_metadata.get('video')
  97. if not video_metadata:
  98. custom_video_id = traverse_obj(fusion_metadata, ('customVideo', 'embed', 'id'), expected_type=str)
  99. if custom_video_id:
  100. video_metadata = self._download_json(
  101. 'https://www.nzherald.co.nz/pf/api/v3/content/fetch/full-content-by-id', article_id,
  102. query={'query': json.dumps({'id': custom_video_id, 'site': 'nzh'}), '_website': 'nzh'})
  103. bc_video_id = traverse_obj(
  104. video_metadata or fusion_metadata, # fusion metadata is the video metadata for video-only pages
  105. 'brightcoveId', ('content_elements', ..., 'referent', 'id'),
  106. get_all=False, expected_type=compat_str)
  107. if not bc_video_id:
  108. if isinstance(video_metadata, dict) and len(video_metadata) == 0:
  109. raise ExtractorError('This article does not have a video.', expected=True)
  110. else:
  111. raise ExtractorError('Failed to extract brightcove video id')
  112. bc_url = self.BRIGHTCOVE_URL_TEMPLATE % bc_video_id
  113. return self.url_result(bc_url, 'BrightcoveNew')