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