nzherald.py 5.7 KB

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