]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/itv.py
Merge pull request #106 from diegorodriguezv/fix-tmz
[yt-dlp.git] / youtube_dlc / extractor / itv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import uuid
5 import xml.etree.ElementTree as etree
6 import json
7 import re
8
9 from .common import InfoExtractor
10 from .brightcove import BrightcoveNewIE
11 from ..compat import (
12 compat_str,
13 compat_etree_register_namespace,
14 )
15 from ..utils import (
16 determine_ext,
17 ExtractorError,
18 extract_attributes,
19 int_or_none,
20 merge_dicts,
21 parse_duration,
22 smuggle_url,
23 try_get,
24 url_or_none,
25 xpath_with_ns,
26 xpath_element,
27 xpath_text,
28 )
29
30
31 class ITVIE(InfoExtractor):
32 _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
33 _GEO_COUNTRIES = ['GB']
34 _TESTS = [{
35 'url': 'http://www.itv.com/hub/mr-bean-animated-series/2a2936a0053',
36 'info_dict': {
37 'id': '2a2936a0053',
38 'ext': 'flv',
39 'title': 'Home Movie',
40 },
41 'params': {
42 # rtmp download
43 'skip_download': True,
44 },
45 }, {
46 # unavailable via data-playlist-url
47 'url': 'https://www.itv.com/hub/through-the-keyhole/2a2271a0033',
48 'only_matching': True,
49 }, {
50 # InvalidVodcrid
51 'url': 'https://www.itv.com/hub/james-martins-saturday-morning/2a5159a0034',
52 'only_matching': True,
53 }, {
54 # ContentUnavailable
55 'url': 'https://www.itv.com/hub/whos-doing-the-dishes/2a2898a0024',
56 'only_matching': True,
57 }]
58
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
61 webpage = self._download_webpage(url, video_id)
62 params = extract_attributes(self._search_regex(
63 r'(?s)(<[^>]+id="video"[^>]*>)', webpage, 'params'))
64
65 ns_map = {
66 'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
67 'tem': 'http://tempuri.org/',
68 'itv': 'http://schemas.datacontract.org/2004/07/Itv.BB.Mercury.Common.Types',
69 'com': 'http://schemas.itv.com/2009/05/Common',
70 }
71 for ns, full_ns in ns_map.items():
72 compat_etree_register_namespace(ns, full_ns)
73
74 def _add_ns(name):
75 return xpath_with_ns(name, ns_map)
76
77 def _add_sub_element(element, name):
78 return etree.SubElement(element, _add_ns(name))
79
80 production_id = (
81 params.get('data-video-autoplay-id')
82 or '%s#001' % (
83 params.get('data-video-episode-id')
84 or video_id.replace('a', '/')))
85
86 req_env = etree.Element(_add_ns('soapenv:Envelope'))
87 _add_sub_element(req_env, 'soapenv:Header')
88 body = _add_sub_element(req_env, 'soapenv:Body')
89 get_playlist = _add_sub_element(body, ('tem:GetPlaylist'))
90 request = _add_sub_element(get_playlist, 'tem:request')
91 _add_sub_element(request, 'itv:ProductionId').text = production_id
92 _add_sub_element(request, 'itv:RequestGuid').text = compat_str(uuid.uuid4()).upper()
93 vodcrid = _add_sub_element(request, 'itv:Vodcrid')
94 _add_sub_element(vodcrid, 'com:Id')
95 _add_sub_element(request, 'itv:Partition')
96 user_info = _add_sub_element(get_playlist, 'tem:userInfo')
97 _add_sub_element(user_info, 'itv:Broadcaster').text = 'Itv'
98 _add_sub_element(user_info, 'itv:DM')
99 _add_sub_element(user_info, 'itv:RevenueScienceValue')
100 _add_sub_element(user_info, 'itv:SessionId')
101 _add_sub_element(user_info, 'itv:SsoToken')
102 _add_sub_element(user_info, 'itv:UserToken')
103 site_info = _add_sub_element(get_playlist, 'tem:siteInfo')
104 _add_sub_element(site_info, 'itv:AdvertisingRestriction').text = 'None'
105 _add_sub_element(site_info, 'itv:AdvertisingSite').text = 'ITV'
106 _add_sub_element(site_info, 'itv:AdvertisingType').text = 'Any'
107 _add_sub_element(site_info, 'itv:Area').text = 'ITVPLAYER.VIDEO'
108 _add_sub_element(site_info, 'itv:Category')
109 _add_sub_element(site_info, 'itv:Platform').text = 'DotCom'
110 _add_sub_element(site_info, 'itv:Site').text = 'ItvCom'
111 device_info = _add_sub_element(get_playlist, 'tem:deviceInfo')
112 _add_sub_element(device_info, 'itv:ScreenSize').text = 'Big'
113 player_info = _add_sub_element(get_playlist, 'tem:playerInfo')
114 _add_sub_element(player_info, 'itv:Version').text = '2'
115
116 headers = self.geo_verification_headers()
117 headers.update({
118 'Content-Type': 'text/xml; charset=utf-8',
119 'SOAPAction': 'http://tempuri.org/PlaylistService/GetPlaylist',
120 })
121
122 info = self._search_json_ld(webpage, video_id, default={})
123 formats = []
124 subtitles = {}
125
126 def extract_subtitle(sub_url):
127 ext = determine_ext(sub_url, 'ttml')
128 subtitles.setdefault('en', []).append({
129 'url': sub_url,
130 'ext': 'ttml' if ext == 'xml' else ext,
131 })
132
133 resp_env = self._download_xml(
134 params['data-playlist-url'], video_id,
135 headers=headers, data=etree.tostring(req_env), fatal=False)
136 if resp_env:
137 playlist = xpath_element(resp_env, './/Playlist')
138 if playlist is None:
139 fault_code = xpath_text(resp_env, './/faultcode')
140 fault_string = xpath_text(resp_env, './/faultstring')
141 if fault_code == 'InvalidGeoRegion':
142 self.raise_geo_restricted(
143 msg=fault_string, countries=self._GEO_COUNTRIES)
144 elif fault_code not in (
145 'InvalidEntity', 'InvalidVodcrid', 'ContentUnavailable'):
146 raise ExtractorError(
147 '%s said: %s' % (self.IE_NAME, fault_string), expected=True)
148 info.update({
149 'title': self._og_search_title(webpage),
150 'episode_title': params.get('data-video-episode'),
151 'series': params.get('data-video-title'),
152 })
153 else:
154 title = xpath_text(playlist, 'EpisodeTitle', default=None)
155 info.update({
156 'title': title,
157 'episode_title': title,
158 'episode_number': int_or_none(xpath_text(playlist, 'EpisodeNumber')),
159 'series': xpath_text(playlist, 'ProgrammeTitle'),
160 'duration': parse_duration(xpath_text(playlist, 'Duration')),
161 })
162 video_element = xpath_element(playlist, 'VideoEntries/Video', fatal=True)
163 media_files = xpath_element(video_element, 'MediaFiles', fatal=True)
164 rtmp_url = media_files.attrib['base']
165
166 for media_file in media_files.findall('MediaFile'):
167 play_path = xpath_text(media_file, 'URL')
168 if not play_path:
169 continue
170 tbr = int_or_none(media_file.get('bitrate'), 1000)
171 f = {
172 'format_id': 'rtmp' + ('-%d' % tbr if tbr else ''),
173 'play_path': play_path,
174 # Providing this swfVfy allows to avoid truncated downloads
175 'player_url': 'http://www.itv.com/mercury/Mercury_VideoPlayer.swf',
176 'page_url': url,
177 'tbr': tbr,
178 'ext': 'flv',
179 }
180 app = self._search_regex(
181 'rtmpe?://[^/]+/(.+)$', rtmp_url, 'app', default=None)
182 if app:
183 f.update({
184 'url': rtmp_url.split('?', 1)[0],
185 'app': app,
186 })
187 else:
188 f['url'] = rtmp_url
189 formats.append(f)
190
191 for caption_url in video_element.findall('ClosedCaptioningURIs/URL'):
192 if caption_url.text:
193 extract_subtitle(caption_url.text)
194
195 ios_playlist_url = params.get('data-video-playlist') or params.get('data-video-id')
196 hmac = params.get('data-video-hmac')
197 if ios_playlist_url and hmac and re.match(r'https?://', ios_playlist_url):
198 headers = self.geo_verification_headers()
199 headers.update({
200 'Accept': 'application/vnd.itv.vod.playlist.v2+json',
201 'Content-Type': 'application/json',
202 'hmac': hmac.upper(),
203 })
204 ios_playlist = self._download_json(
205 ios_playlist_url, video_id, data=json.dumps({
206 'user': {
207 'itvUserId': '',
208 'entitlements': [],
209 'token': ''
210 },
211 'device': {
212 'manufacturer': 'Safari',
213 'model': '5',
214 'os': {
215 'name': 'Windows NT',
216 'version': '6.1',
217 'type': 'desktop'
218 }
219 },
220 'client': {
221 'version': '4.1',
222 'id': 'browser'
223 },
224 'variantAvailability': {
225 'featureset': {
226 'min': ['hls', 'aes', 'outband-webvtt'],
227 'max': ['hls', 'aes', 'outband-webvtt']
228 },
229 'platformTag': 'dotcom'
230 }
231 }).encode(), headers=headers, fatal=False)
232 if ios_playlist:
233 video_data = ios_playlist.get('Playlist', {}).get('Video', {})
234 ios_base_url = video_data.get('Base')
235 for media_file in video_data.get('MediaFiles', []):
236 href = media_file.get('Href')
237 if not href:
238 continue
239 if ios_base_url:
240 href = ios_base_url + href
241 ext = determine_ext(href)
242 if ext == 'm3u8':
243 formats.extend(self._extract_m3u8_formats(
244 href, video_id, 'mp4', entry_protocol='m3u8_native',
245 m3u8_id='hls', fatal=False))
246 else:
247 formats.append({
248 'url': href,
249 })
250 subs = video_data.get('Subtitles')
251 if isinstance(subs, list):
252 for sub in subs:
253 if not isinstance(sub, dict):
254 continue
255 href = url_or_none(sub.get('Href'))
256 if href:
257 extract_subtitle(href)
258 if not info.get('duration'):
259 info['duration'] = parse_duration(video_data.get('Duration'))
260
261 self._sort_formats(formats)
262
263 info.update({
264 'id': video_id,
265 'formats': formats,
266 'subtitles': subtitles,
267 })
268
269 webpage_info = self._search_json_ld(webpage, video_id, default={})
270 if not webpage_info.get('title'):
271 webpage_info['title'] = self._html_search_regex(
272 r'(?s)<h\d+[^>]+\bclass=["\'][^>]*episode-title["\'][^>]*>([^<]+)<',
273 webpage, 'title', default=None) or self._og_search_title(
274 webpage, default=None) or self._html_search_meta(
275 'twitter:title', webpage, 'title',
276 default=None) or webpage_info['episode']
277
278 return merge_dicts(info, webpage_info)
279
280
281 class ITVBTCCIE(InfoExtractor):
282 _VALID_URL = r'https?://(?:www\.)?itv\.com/btcc/(?:[^/]+/)*(?P<id>[^/?#&]+)'
283 _TEST = {
284 'url': 'https://www.itv.com/btcc/articles/btcc-2019-brands-hatch-gp-race-action',
285 'info_dict': {
286 'id': 'btcc-2019-brands-hatch-gp-race-action',
287 'title': 'BTCC 2019: Brands Hatch GP race action',
288 },
289 'playlist_count': 12,
290 }
291 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/1582188683001/HkiHLnNRx_default/index.html?videoId=%s'
292
293 def _real_extract(self, url):
294 playlist_id = self._match_id(url)
295
296 webpage = self._download_webpage(url, playlist_id)
297
298 json_map = try_get(self._parse_json(self._html_search_regex(
299 '(?s)<script[^>]+id=[\'"]__NEXT_DATA__[^>]*>([^<]+)</script>', webpage, 'json_map'), playlist_id),
300 lambda x: x['props']['pageProps']['article']['body']['content']) or []
301
302 # Discard empty objects
303 video_ids = []
304 for video in json_map:
305 if video['data'].get('id'):
306 video_ids.append(video['data']['id'])
307
308 entries = [
309 self.url_result(
310 smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % video_id, {
311 # ITV does not like some GB IP ranges, so here are some
312 # IP blocks it accepts
313 'geo_ip_blocks': [
314 '193.113.0.0/16', '54.36.162.0/23', '159.65.16.0/21'
315 ],
316 'referrer': url,
317 }),
318 ie=BrightcoveNewIE.ie_key(), video_id=video_id)
319 for video_id in video_ids]
320
321 title = self._og_search_title(webpage, fatal=False)
322
323 return self.playlist_result(entries, playlist_id, title)