2 from __future__
import unicode_literals
5 import xml
.etree
.ElementTree
as etree
8 from .common
import InfoExtractor
11 compat_etree_register_namespace
,
25 class ITVIE(InfoExtractor
):
26 _VALID_URL
= r
'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
27 _GEO_COUNTRIES
= ['GB']
29 'url': 'http://www.itv.com/hub/mr-bean-animated-series/2a2936a0053',
33 'title': 'Home Movie',
37 'skip_download': True,
41 def _real_extract(self
, url
):
42 video_id
= self
._match
_id
(url
)
43 webpage
= self
._download
_webpage
(url
, video_id
)
44 params
= extract_attributes(self
._search
_regex
(
45 r
'(?s)(<[^>]+id="video"[^>]*>)', webpage
, 'params'))
48 'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
49 'tem': 'http://tempuri.org/',
50 'itv': 'http://schemas.datacontract.org/2004/07/Itv.BB.Mercury.Common.Types',
51 'com': 'http://schemas.itv.com/2009/05/Common',
53 for ns
, full_ns
in ns_map
.items():
54 compat_etree_register_namespace(ns
, full_ns
)
57 return xpath_with_ns(name
, ns_map
)
59 def _add_sub_element(element
, name
):
60 return etree
.SubElement(element
, _add_ns(name
))
62 req_env
= etree
.Element(_add_ns('soapenv:Envelope'))
63 _add_sub_element(req_env
, 'soapenv:Header')
64 body
= _add_sub_element(req_env
, 'soapenv:Body')
65 get_playlist
= _add_sub_element(body
, ('tem:GetPlaylist'))
66 request
= _add_sub_element(get_playlist
, 'tem:request')
67 _add_sub_element(request
, 'itv:ProductionId').text
= params
['data-video-id']
68 _add_sub_element(request
, 'itv:RequestGuid').text
= compat_str(uuid
.uuid4()).upper()
69 vodcrid
= _add_sub_element(request
, 'itv:Vodcrid')
70 _add_sub_element(vodcrid
, 'com:Id')
71 _add_sub_element(request
, 'itv:Partition')
72 user_info
= _add_sub_element(get_playlist
, 'tem:userInfo')
73 _add_sub_element(user_info
, 'itv:Broadcaster').text
= 'Itv'
74 _add_sub_element(user_info
, 'itv:DM')
75 _add_sub_element(user_info
, 'itv:RevenueScienceValue')
76 _add_sub_element(user_info
, 'itv:SessionId')
77 _add_sub_element(user_info
, 'itv:SsoToken')
78 _add_sub_element(user_info
, 'itv:UserToken')
79 site_info
= _add_sub_element(get_playlist
, 'tem:siteInfo')
80 _add_sub_element(site_info
, 'itv:AdvertisingRestriction').text
= 'None'
81 _add_sub_element(site_info
, 'itv:AdvertisingSite').text
= 'ITV'
82 _add_sub_element(site_info
, 'itv:AdvertisingType').text
= 'Any'
83 _add_sub_element(site_info
, 'itv:Area').text
= 'ITVPLAYER.VIDEO'
84 _add_sub_element(site_info
, 'itv:Category')
85 _add_sub_element(site_info
, 'itv:Platform').text
= 'DotCom'
86 _add_sub_element(site_info
, 'itv:Site').text
= 'ItvCom'
87 device_info
= _add_sub_element(get_playlist
, 'tem:deviceInfo')
88 _add_sub_element(device_info
, 'itv:ScreenSize').text
= 'Big'
89 player_info
= _add_sub_element(get_playlist
, 'tem:playerInfo')
90 _add_sub_element(player_info
, 'itv:Version').text
= '2'
92 headers
= self
.geo_verification_headers()
94 'Content-Type': 'text/xml; charset=utf-8',
95 'SOAPAction': 'http://tempuri.org/PlaylistService/GetPlaylist',
97 resp_env
= self
._download
_xml
(
98 params
['data-playlist-url'], video_id
,
99 headers
=headers
, data
=etree
.tostring(req_env
))
100 playlist
= xpath_element(resp_env
, './/Playlist')
102 fault_code
= xpath_text(resp_env
, './/faultcode')
103 fault_string
= xpath_text(resp_env
, './/faultstring')
104 if fault_code
== 'InvalidGeoRegion':
105 self
.raise_geo_restricted(
106 msg
=fault_string
, countries
=self
._GEO
_COUNTRIES
)
107 raise ExtractorError('%s said: %s' % (self
.IE_NAME
, fault_string
))
108 title
= xpath_text(playlist
, 'EpisodeTitle', fatal
=True)
109 video_element
= xpath_element(playlist
, 'VideoEntries/Video', fatal
=True)
110 media_files
= xpath_element(video_element
, 'MediaFiles', fatal
=True)
111 rtmp_url
= media_files
.attrib
['base']
114 for media_file
in media_files
.findall('MediaFile'):
115 play_path
= xpath_text(media_file
, 'URL')
118 tbr
= int_or_none(media_file
.get('bitrate'), 1000)
120 'format_id': 'rtmp' + ('-%d' % tbr
if tbr
else ''),
121 'play_path': play_path
,
122 # Providing this swfVfy allows to avoid truncated downloads
123 'player_url': 'http://www.itv.com/mercury/Mercury_VideoPlayer.swf',
128 app
= self
._search
_regex
(
129 'rtmpe?://[^/]+/(.+)$', rtmp_url
, 'app', default
=None)
132 'url': rtmp_url
.split('?', 1)[0],
139 ios_playlist_url
= params
.get('data-video-playlist')
140 hmac
= params
.get('data-video-hmac')
141 if ios_playlist_url
and hmac
:
142 headers
= self
.geo_verification_headers()
144 'Accept': 'application/vnd.itv.vod.playlist.v2+json',
145 'Content-Type': 'application/json',
146 'hmac': hmac
.upper(),
148 ios_playlist
= self
._download
_json
(
149 ios_playlist_url
, video_id
, data
=json
.dumps({
156 'manufacturer': 'Apple',
168 'variantAvailability': {
170 'min': ['hls', 'aes'],
171 'max': ['hls', 'aes']
173 'platformTag': 'mobile'
175 }).encode(), headers
=headers
, fatal
=False)
177 video_data
= ios_playlist
.get('Playlist', {}).get('Video', {})
178 ios_base_url
= video_data
.get('Base')
179 for media_file
in video_data
.get('MediaFiles', []):
180 href
= media_file
.get('Href')
184 href
= ios_base_url
+ href
185 ext
= determine_ext(href
)
187 formats
.extend(self
._extract
_m
3u8_formats
(
188 href
, video_id
, 'mp4', entry_protocol
='m3u8_native',
189 m3u8_id
='hls', fatal
=False))
194 self
._sort
_formats
(formats
)
197 for caption_url
in video_element
.findall('ClosedCaptioningURIs/URL'):
198 if not caption_url
.text
:
200 ext
= determine_ext(caption_url
.text
, 'ttml')
201 subtitles
.setdefault('en', []).append({
202 'url': caption_url
.text
,
203 'ext': 'ttml' if ext
== 'xml' else ext
,
206 info
= self
._search
_json
_ld
(webpage
, video_id
, default
={})
211 'subtitles': subtitles
,
212 'episode_title': title
,
213 'episode_number': int_or_none(xpath_text(playlist
, 'EpisodeNumber')),
214 'series': xpath_text(playlist
, 'ProgrammeTitle'),
215 'duartion': parse_duration(xpath_text(playlist
, 'Duration')),