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