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