2 from __future__
import unicode_literals
5 import xml
.etree
.ElementTree
as etree
9 from .common
import InfoExtractor
10 from .brightcove
import BrightcoveNewIE
11 from ..compat
import (
13 compat_etree_register_namespace
,
31 class ITVIE(InfoExtractor
):
32 _VALID_URL
= r
'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
33 _GEO_COUNTRIES
= ['GB']
35 'url': 'http://www.itv.com/hub/mr-bean-animated-series/2a2936a0053',
39 'title': 'Home Movie',
43 'skip_download': True,
46 # unavailable via data-playlist-url
47 'url': 'https://www.itv.com/hub/through-the-keyhole/2a2271a0033',
48 'only_matching': True,
51 'url': 'https://www.itv.com/hub/james-martins-saturday-morning/2a5159a0034',
52 'only_matching': True,
55 'url': 'https://www.itv.com/hub/whos-doing-the-dishes/2a2898a0024',
56 'only_matching': True,
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'))
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',
71 for ns
, full_ns
in ns_map
.items():
72 compat_etree_register_namespace(ns
, full_ns
)
75 return xpath_with_ns(name
, ns_map
)
77 def _add_sub_element(element
, name
):
78 return etree
.SubElement(element
, _add_ns(name
))
81 params
.get('data-video-autoplay-id')
83 params
.get('data-video-episode-id')
84 or video_id
.replace('a', '/')))
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'
116 headers
= self
.geo_verification_headers()
118 'Content-Type': 'text/xml; charset=utf-8',
119 'SOAPAction': 'http://tempuri.org/PlaylistService/GetPlaylist',
122 info
= self
._search
_json
_ld
(webpage
, video_id
, default
={})
126 def extract_subtitle(sub_url
):
127 ext
= determine_ext(sub_url
, 'ttml')
128 subtitles
.setdefault('en', []).append({
130 'ext': 'ttml' if ext
== 'xml' else ext
,
133 resp_env
= self
._download
_xml
(
134 params
['data-playlist-url'], video_id
,
135 headers
=headers
, data
=etree
.tostring(req_env
), fatal
=False)
137 playlist
= xpath_element(resp_env
, './/Playlist')
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)
149 'title': self
._og
_search
_title
(webpage
),
150 'episode_title': params
.get('data-video-episode'),
151 'series': params
.get('data-video-title'),
154 title
= xpath_text(playlist
, 'EpisodeTitle', default
=None)
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')),
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']
166 for media_file
in media_files
.findall('MediaFile'):
167 play_path
= xpath_text(media_file
, 'URL')
170 tbr
= int_or_none(media_file
.get('bitrate'), 1000)
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',
180 app
= self
._search
_regex
(
181 'rtmpe?://[^/]+/(.+)$', rtmp_url
, 'app', default
=None)
184 'url': rtmp_url
.split('?', 1)[0],
191 for caption_url
in video_element
.findall('ClosedCaptioningURIs/URL'):
193 extract_subtitle(caption_url
.text
)
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()
200 'Accept': 'application/vnd.itv.vod.playlist.v2+json',
201 'Content-Type': 'application/json',
202 'hmac': hmac
.upper(),
204 ios_playlist
= self
._download
_json
(
205 ios_playlist_url
, video_id
, data
=json
.dumps({
212 'manufacturer': 'Safari',
215 'name': 'Windows NT',
224 'variantAvailability': {
226 'min': ['hls', 'aes', 'outband-webvtt'],
227 'max': ['hls', 'aes', 'outband-webvtt']
229 'platformTag': 'dotcom'
231 }).encode(), headers
=headers
, fatal
=False)
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')
240 href
= ios_base_url
+ href
241 ext
= determine_ext(href
)
243 formats
.extend(self
._extract
_m
3u8_formats
(
244 href
, video_id
, 'mp4', entry_protocol
='m3u8_native',
245 m3u8_id
='hls', fatal
=False))
250 subs
= video_data
.get('Subtitles')
251 if isinstance(subs
, list):
253 if not isinstance(sub
, dict):
255 href
= url_or_none(sub
.get('Href'))
257 extract_subtitle(href
)
258 if not info
.get('duration'):
259 info
['duration'] = parse_duration(video_data
.get('Duration'))
261 self
._sort
_formats
(formats
)
266 'subtitles': subtitles
,
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']
278 return merge_dicts(info, webpage_info)
281 class ITVBTCCIE(InfoExtractor):
282 _VALID_URL = r'https?://(?:www\.)?itv\.com/btcc/(?:[^/]+/)*(?P<id>[^/?#&]+)'
284 'url': 'https://www.itv.com/btcc/articles/btcc-2019-brands-hatch-gp-race-action',
286 'id': 'btcc-2019-brands-hatch-gp-race-action',
287 'title': 'BTCC 2019: Brands Hatch GP race action',
289 'playlist_count': 12,
291 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/1582188683001/HkiHLnNRx_default/index.html?videoId=%s'
293 def _real_extract(self, url):
294 playlist_id = self._match_id(url)
296 webpage = self._download_webpage(url, playlist_id)
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 []
302 # Discard empty objects
304 for video in json_map:
305 if video['data
'].get('id'):
306 video_ids.append(video['data
']['id'])
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
314 '193.113.0.0/16', '54.36.162.0/23', '159.65.16.0/21'
318 ie=BrightcoveNewIE.ie_key(), video_id=video_id)
319 for video_id in video_ids]
321 title = self._og_search_title(webpage, fatal=False)
323 return self.playlist_result(entries, playlist_id, title)