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