]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/gbnews.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / gbnews.py
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 extract_attributes,
5 get_elements_html_by_class,
6 url_or_none,
7 )
8 from ..utils.traversal import traverse_obj
9
10
11 class GBNewsIE(InfoExtractor):
12 IE_DESC = 'GB News clips, features and live streams'
13 _VALID_URL = r'https?://(?:www\.)?gbnews\.(?:uk|com)/(?:\w+/)?(?P<id>[^#?]+)'
14
15 _PLATFORM = 'safari'
16 _SSMP_URL = 'https://mm-v2.simplestream.com/ssmp/api.php'
17 _TESTS = [{
18 'url': 'https://www.gbnews.com/news/bbc-claudine-gay-harvard-university-antisemitism-row',
19 'info_dict': {
20 'id': '52264136',
21 'ext': 'mp4',
22 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
23 'display_id': 'bbc-claudine-gay-harvard-university-antisemitism-row',
24 'description': 'The post was criticised by former employers of the broadcaster',
25 'title': 'BBC deletes post after furious backlash over headline downplaying antisemitism',
26 },
27 }, {
28 'url': 'https://www.gbnews.com/royal/prince-harry-in-love-with-kate-meghan-markle-jealous-royal',
29 'info_dict': {
30 'id': '52328390',
31 'ext': 'mp4',
32 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
33 'display_id': 'prince-harry-in-love-with-kate-meghan-markle-jealous-royal',
34 'description': 'Ingrid Seward has published 17 books documenting the highs and lows of the Royal Family',
35 'title': 'Royal author claims Prince Harry was \'in love\' with Kate - Meghan was \'jealous\'',
36 },
37 }, {
38 'url': 'https://www.gbnews.uk/watchlive',
39 'info_dict': {
40 'id': '1069',
41 'ext': 'mp4',
42 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
43 'display_id': 'watchlive',
44 'live_status': 'is_live',
45 'title': r're:^GB News Live',
46 },
47 'params': {'skip_download': 'm3u8'},
48 }]
49 _SS_ENDPOINTS = None
50
51 def _get_ss_endpoint(self, data_id, data_env):
52 if not self._SS_ENDPOINTS:
53 self._SS_ENDPOINTS = {}
54
55 if not data_id:
56 data_id = 'GB003'
57 if not data_env:
58 data_env = 'production'
59 key = data_id, data_env
60 result = self._SS_ENDPOINTS.get(key)
61 if result:
62 return result
63
64 json_data = self._download_json(
65 self._SSMP_URL, None, 'Downloading Simplestream JSON metadata', query={
66 'id': data_id,
67 'env': data_env,
68 })
69 meta_url = traverse_obj(json_data, ('response', 'api_hostname', {url_or_none}))
70 if not meta_url:
71 raise ExtractorError('No API host found')
72
73 self._SS_ENDPOINTS[key] = meta_url
74 return meta_url
75
76 def _real_extract(self, url):
77 display_id = self._match_id(url).rpartition('/')[2]
78 webpage = self._download_webpage(url, display_id)
79
80 video_data = None
81 elements = get_elements_html_by_class('simplestream', webpage)
82 for html_tag in elements:
83 attributes = extract_attributes(html_tag)
84 if 'sidebar' not in (attributes.get('class') or ''):
85 video_data = attributes
86 if not video_data:
87 raise ExtractorError('Could not find video element', expected=True)
88
89 endpoint_url = self._get_ss_endpoint(video_data.get('data-id'), video_data.get('data-env'))
90
91 uvid = video_data['data-uvid']
92 video_type = video_data.get('data-type')
93 if not video_type or video_type == 'vod':
94 video_type = 'show'
95 stream_data = self._download_json(
96 f'{endpoint_url}/api/{video_type}/stream/{uvid}',
97 uvid, 'Downloading stream JSON', query={
98 'key': video_data.get('data-key'),
99 'platform': self._PLATFORM,
100 })
101 if traverse_obj(stream_data, 'drm'):
102 self.report_drm(uvid)
103
104 return {
105 'id': uvid,
106 'display_id': display_id,
107 'title': self._og_search_title(webpage, default=None),
108 'description': self._og_search_description(webpage, default=None),
109 'formats': self._extract_m3u8_formats(traverse_obj(stream_data, (
110 'response', 'stream', {url_or_none})), uvid, 'mp4'),
111 'thumbnail': self._og_search_thumbnail(webpage, default=None),
112 'is_live': video_type == 'live',
113 }