]>
Commit | Line | Data |
---|---|---|
a71b8d3b RA |
1 | import json |
2 | ||
3 | from .common import InfoExtractor | |
ea1f5e5d | 4 | from .brightcove import BrightcoveNewIE |
ea706726 | 5 | |
6 | from ..compat import compat_str | |
a71b8d3b | 7 | from ..utils import ( |
ea706726 | 8 | base_url, |
29f7c58a | 9 | clean_html, |
a4ec4517 | 10 | determine_ext, |
a71b8d3b | 11 | extract_attributes, |
ea706726 | 12 | ExtractorError, |
29f7c58a | 13 | get_element_by_class, |
14 | JSON_LD_RE, | |
30374f4d | 15 | merge_dicts, |
a71b8d3b | 16 | parse_duration, |
ea1f5e5d | 17 | smuggle_url, |
6857df60 | 18 | try_get, |
3052a30d | 19 | url_or_none, |
ea706726 | 20 | url_basename, |
21 | urljoin, | |
a71b8d3b RA |
22 | ) |
23 | ||
24 | ||
25 | class ITVIE(InfoExtractor): | |
f592ff98 | 26 | _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)' |
4248dad9 | 27 | _GEO_COUNTRIES = ['GB'] |
3fae11ac | 28 | _TESTS = [{ |
ea706726 | 29 | 'url': 'https://www.itv.com/hub/plebs/2a1873a0002', |
30 | 'info_dict': { | |
31 | 'id': '2a1873a0002', | |
32 | 'ext': 'mp4', | |
33 | 'title': 'Plebs - The Orgy', | |
34 | 'description': 'md5:4d7159af53ebd5b36e8b3ec82a41fdb4', | |
35 | 'series': 'Plebs', | |
36 | 'season_number': 1, | |
37 | 'episode_number': 1, | |
38 | 'thumbnail': r're:https?://hubimages\.itv\.com/episode/2_1873_0002' | |
39 | }, | |
40 | 'params': { | |
41 | # m3u8 download | |
42 | 'skip_download': True, | |
43 | }, | |
44 | }, { | |
45 | 'url': 'https://www.itv.com/hub/the-jonathan-ross-show/2a1166a0209', | |
a71b8d3b | 46 | 'info_dict': { |
ea706726 | 47 | 'id': '2a1166a0209', |
29f7c58a | 48 | 'ext': 'mp4', |
ea706726 | 49 | 'title': 'The Jonathan Ross Show - Series 17 - Episode 8', |
50 | 'description': 'md5:3023dcdd375db1bc9967186cdb3f1399', | |
51 | 'series': 'The Jonathan Ross Show', | |
52 | 'episode_number': 8, | |
53 | 'season_number': 17, | |
54 | 'thumbnail': r're:https?://hubimages\.itv\.com/episode/2_1873_0002' | |
a71b8d3b RA |
55 | }, |
56 | 'params': { | |
29f7c58a | 57 | # m3u8 download |
a71b8d3b RA |
58 | 'skip_download': True, |
59 | }, | |
3fae11ac S |
60 | }, { |
61 | # unavailable via data-playlist-url | |
62 | 'url': 'https://www.itv.com/hub/through-the-keyhole/2a2271a0033', | |
63 | 'only_matching': True, | |
c18142da S |
64 | }, { |
65 | # InvalidVodcrid | |
66 | 'url': 'https://www.itv.com/hub/james-martins-saturday-morning/2a5159a0034', | |
67 | 'only_matching': True, | |
68 | }, { | |
69 | # ContentUnavailable | |
70 | 'url': 'https://www.itv.com/hub/whos-doing-the-dishes/2a2898a0024', | |
71 | 'only_matching': True, | |
3fae11ac | 72 | }] |
a71b8d3b | 73 | |
ea706726 | 74 | def _generate_api_headers(self, hmac): |
75 | return merge_dicts({ | |
29f7c58a | 76 | 'Accept': 'application/vnd.itv.vod.playlist.v2+json', |
77 | 'Content-Type': 'application/json', | |
78 | 'hmac': hmac.upper(), | |
ea706726 | 79 | }, self.geo_verification_headers()) |
80 | ||
81 | def _call_api(self, video_id, playlist_url, headers, platform_tag, featureset, fatal=True): | |
82 | return self._download_json( | |
83 | playlist_url, video_id, data=json.dumps({ | |
29f7c58a | 84 | 'user': { |
85 | 'itvUserId': '', | |
86 | 'entitlements': [], | |
87 | 'token': '' | |
88 | }, | |
89 | 'device': { | |
90 | 'manufacturer': 'Safari', | |
91 | 'model': '5', | |
92 | 'os': { | |
93 | 'name': 'Windows NT', | |
94 | 'version': '6.1', | |
95 | 'type': 'desktop' | |
96 | } | |
97 | }, | |
98 | 'client': { | |
99 | 'version': '4.1', | |
100 | 'id': 'browser' | |
101 | }, | |
102 | 'variantAvailability': { | |
103 | 'featureset': { | |
ea706726 | 104 | 'min': featureset, |
105 | 'max': featureset | |
29f7c58a | 106 | }, |
ea706726 | 107 | 'platformTag': platform_tag |
29f7c58a | 108 | } |
ea706726 | 109 | }).encode(), headers=headers, fatal=fatal) |
110 | ||
111 | def _get_subtitles(self, video_id, variants, ios_playlist_url, headers, *args, **kwargs): | |
112 | subtitles = {} | |
298bf1d2 | 113 | # Prefer last matching featureset |
114 | # See: https://github.com/yt-dlp/yt-dlp/issues/986 | |
ea706726 | 115 | platform_tag_subs, featureset_subs = next( |
116 | ((platform_tag, featureset) | |
bc8ab44e | 117 | for platform_tag, featuresets in reversed(list(variants.items())) for featureset in featuresets |
ea706726 | 118 | if try_get(featureset, lambda x: x[2]) == 'outband-webvtt'), |
119 | (None, None)) | |
298bf1d2 | 120 | |
121 | if platform_tag_subs and featureset_subs: | |
ea706726 | 122 | subs_playlist = self._call_api( |
123 | video_id, ios_playlist_url, headers, platform_tag_subs, featureset_subs, fatal=False) | |
124 | subs = try_get(subs_playlist, lambda x: x['Playlist']['Video']['Subtitles'], list) or [] | |
125 | for sub in subs: | |
126 | if not isinstance(sub, dict): | |
127 | continue | |
128 | href = url_or_none(sub.get('Href')) | |
129 | if not href: | |
130 | continue | |
131 | subtitles.setdefault('en', []).append({'url': href}) | |
132 | return subtitles | |
133 | ||
134 | def _real_extract(self, url): | |
135 | video_id = self._match_id(url) | |
136 | webpage = self._download_webpage(url, video_id) | |
137 | params = extract_attributes(self._search_regex( | |
138 | r'(?s)(<[^>]+id="video"[^>]*>)', webpage, 'params')) | |
139 | variants = self._parse_json( | |
140 | try_get(params, lambda x: x['data-video-variants'], compat_str) or '{}', | |
141 | video_id, fatal=False) | |
298bf1d2 | 142 | # Prefer last matching featureset |
143 | # See: https://github.com/yt-dlp/yt-dlp/issues/986 | |
ea706726 | 144 | platform_tag_video, featureset_video = next( |
145 | ((platform_tag, featureset) | |
bc8ab44e | 146 | for platform_tag, featuresets in reversed(list(variants.items())) for featureset in featuresets |
da27aeea | 147 | if set(try_get(featureset, lambda x: x[:2]) or []) == {'aes', 'hls'}), |
ea706726 | 148 | (None, None)) |
149 | if not platform_tag_video or not featureset_video: | |
150 | raise ExtractorError('No downloads available', expected=True, video_id=video_id) | |
3fae11ac | 151 | |
ea706726 | 152 | ios_playlist_url = params.get('data-video-playlist') or params['data-video-id'] |
153 | headers = self._generate_api_headers(params['data-video-hmac']) | |
154 | ios_playlist = self._call_api( | |
155 | video_id, ios_playlist_url, headers, platform_tag_video, featureset_video) | |
156 | ||
157 | video_data = try_get(ios_playlist, lambda x: x['Playlist']['Video'], dict) or {} | |
158 | ios_base_url = video_data.get('Base') | |
3fae11ac | 159 | formats = [] |
29f7c58a | 160 | for media_file in (video_data.get('MediaFiles') or []): |
161 | href = media_file.get('Href') | |
162 | if not href: | |
163 | continue | |
164 | if ios_base_url: | |
165 | href = ios_base_url + href | |
166 | ext = determine_ext(href) | |
167 | if ext == 'm3u8': | |
168 | formats.extend(self._extract_m3u8_formats( | |
169 | href, video_id, 'mp4', entry_protocol='m3u8_native', | |
170 | m3u8_id='hls', fatal=False)) | |
30374f4d | 171 | else: |
29f7c58a | 172 | formats.append({ |
173 | 'url': href, | |
30374f4d | 174 | }) |
29f7c58a | 175 | self._sort_formats(formats) |
29f7c58a | 176 | info = self._search_json_ld(webpage, video_id, default={}) |
177 | if not info: | |
178 | json_ld = self._parse_json(self._search_regex( | |
179 | JSON_LD_RE, webpage, 'JSON-LD', '{}', | |
180 | group='json_ld'), video_id, fatal=False) | |
181 | if json_ld and json_ld.get('@type') == 'BreadcrumbList': | |
182 | for ile in (json_ld.get('itemListElement:') or []): | |
183 | item = ile.get('item:') or {} | |
184 | if item.get('@type') == 'TVEpisode': | |
185 | item['@context'] = 'http://schema.org' | |
186 | info = self._json_ld(item, video_id, fatal=False) or {} | |
187 | break | |
188 | ||
ea706726 | 189 | thumbnails = [] |
190 | thumbnail_url = try_get(params, lambda x: x['data-video-posterframe'], compat_str) | |
191 | if thumbnail_url: | |
192 | thumbnails.extend([{ | |
193 | 'url': thumbnail_url.format(width=1920, height=1080, quality=100, blur=0, bg='false'), | |
194 | 'width': 1920, | |
195 | 'height': 1080, | |
196 | }, { | |
197 | 'url': urljoin(base_url(thumbnail_url), url_basename(thumbnail_url)), | |
198 | 'preference': -2 | |
199 | }]) | |
200 | ||
201 | thumbnail_url = self._html_search_meta(['og:image', 'twitter:image'], webpage, default=None) | |
202 | if thumbnail_url: | |
203 | thumbnails.append({ | |
204 | 'url': thumbnail_url, | |
205 | }) | |
206 | self._remove_duplicate_formats(thumbnails) | |
207 | ||
29f7c58a | 208 | return merge_dicts({ |
a71b8d3b | 209 | 'id': video_id, |
29f7c58a | 210 | 'title': self._html_search_meta(['og:title', 'twitter:title'], webpage), |
a71b8d3b | 211 | 'formats': formats, |
ea706726 | 212 | 'subtitles': self.extract_subtitles(video_id, variants, ios_playlist_url, headers), |
29f7c58a | 213 | 'duration': parse_duration(video_data.get('Duration')), |
214 | 'description': clean_html(get_element_by_class('episode-info__synopsis', webpage)), | |
ea706726 | 215 | 'thumbnails': thumbnails |
29f7c58a | 216 | }, info) |
ea1f5e5d S |
217 | |
218 | ||
219 | class ITVBTCCIE(InfoExtractor): | |
3783b5f1 | 220 | _VALID_URL = r'https?://(?:www\.)?itv\.com/(?:news|btcc)/(?:[^/]+/)*(?P<id>[^/?#&]+)' |
221 | _TESTS = [{ | |
6857df60 | 222 | 'url': 'https://www.itv.com/btcc/articles/btcc-2019-brands-hatch-gp-race-action', |
ea1f5e5d | 223 | 'info_dict': { |
6857df60 W |
224 | 'id': 'btcc-2019-brands-hatch-gp-race-action', |
225 | 'title': 'BTCC 2019: Brands Hatch GP race action', | |
ea1f5e5d | 226 | }, |
85da4055 | 227 | 'playlist_count': 12, |
3783b5f1 | 228 | }, { |
229 | 'url': 'https://www.itv.com/news/2021-10-27/i-have-to-protect-the-country-says-rishi-sunak-as-uk-faces-interest-rate-hike', | |
230 | 'info_dict': { | |
231 | 'id': 'i-have-to-protect-the-country-says-rishi-sunak-as-uk-faces-interest-rate-hike', | |
232 | 'title': 'md5:6ef054dd9f069330db3dcc66cb772d32' | |
233 | }, | |
234 | 'playlist_count': 4 | |
235 | }] | |
236 | BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_default/index.html?videoId=%s' | |
ea1f5e5d S |
237 | |
238 | def _real_extract(self, url): | |
239 | playlist_id = self._match_id(url) | |
240 | ||
241 | webpage = self._download_webpage(url, playlist_id) | |
242 | ||
135dfa2c | 243 | json_map = try_get( |
244 | self._search_nextjs_data(webpage, playlist_id), | |
6857df60 W |
245 | lambda x: x['props']['pageProps']['article']['body']['content']) or [] |
246 | ||
3783b5f1 | 247 | entries = [] |
6857df60 | 248 | for video in json_map: |
3783b5f1 | 249 | if not any(video['data'].get(attr) == 'Brightcove' for attr in ('name', 'type')): |
250 | continue | |
251 | video_id = video['data']['id'] | |
252 | account_id = video['data']['accountId'] | |
253 | player_id = video['data']['playerId'] | |
254 | entries.append(self.url_result( | |
255 | smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % (account_id, player_id, video_id), { | |
ea1f5e5d S |
256 | # ITV does not like some GB IP ranges, so here are some |
257 | # IP blocks it accepts | |
258 | 'geo_ip_blocks': [ | |
259 | '193.113.0.0/16', '54.36.162.0/23', '159.65.16.0/21' | |
260 | ], | |
261 | 'referrer': url, | |
262 | }), | |
3783b5f1 | 263 | ie=BrightcoveNewIE.ie_key(), video_id=video_id)) |
ea1f5e5d S |
264 | |
265 | title = self._og_search_title(webpage, fatal=False) | |
266 | ||
267 | return self.playlist_result(entries, playlist_id, title) |