]>
Commit | Line | Data |
---|---|---|
a71b8d3b RA |
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 | ||
8 | from .common import InfoExtractor | |
9 | from ..compat import compat_str | |
10 | from ..utils import ( | |
11 | extract_attributes, | |
12 | xpath_with_ns, | |
13 | xpath_element, | |
14 | xpath_text, | |
15 | int_or_none, | |
16 | parse_duration, | |
17 | ExtractorError, | |
18 | determine_ext, | |
19 | ) | |
20 | ||
21 | ||
22 | class ITVIE(InfoExtractor): | |
f592ff98 | 23 | _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)' |
a71b8d3b RA |
24 | _TEST = { |
25 | 'url': 'http://www.itv.com/hub/mr-bean-animated-series/2a2936a0053', | |
26 | 'info_dict': { | |
27 | 'id': '2a2936a0053', | |
28 | 'ext': 'flv', | |
29 | 'title': 'Home Movie', | |
30 | }, | |
31 | 'params': { | |
32 | # rtmp download | |
33 | 'skip_download': True, | |
34 | }, | |
35 | } | |
36 | ||
37 | def _real_extract(self, url): | |
38 | video_id = self._match_id(url) | |
39 | webpage = self._download_webpage(url, video_id) | |
40 | params = extract_attributes(self._search_regex( | |
41 | r'(?s)(<[^>]+id="video"[^>]*>)', webpage, 'params')) | |
42 | ||
43 | ns_map = { | |
44 | 'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/', | |
45 | 'tem': 'http://tempuri.org/', | |
46 | 'itv': 'http://schemas.datacontract.org/2004/07/Itv.BB.Mercury.Common.Types', | |
47 | 'com': 'http://schemas.itv.com/2009/05/Common', | |
48 | } | |
49 | for ns, full_ns in ns_map.items(): | |
50 | etree.register_namespace(ns, full_ns) | |
51 | ||
52 | def _add_ns(name): | |
53 | return xpath_with_ns(name, ns_map) | |
54 | ||
55 | def _add_sub_element(element, name): | |
56 | return etree.SubElement(element, _add_ns(name)) | |
57 | ||
58 | req_env = etree.Element(_add_ns('soapenv:Envelope')) | |
59 | _add_sub_element(req_env, 'soapenv:Header') | |
60 | body = _add_sub_element(req_env, 'soapenv:Body') | |
61 | get_playlist = _add_sub_element(body, ('tem:GetPlaylist')) | |
62 | request = _add_sub_element(get_playlist, 'tem:request') | |
63 | _add_sub_element(request, 'itv:ProductionId').text = params['data-video-id'] | |
64 | _add_sub_element(request, 'itv:RequestGuid').text = compat_str(uuid.uuid4()).upper() | |
65 | vodcrid = _add_sub_element(request, 'itv:Vodcrid') | |
66 | _add_sub_element(vodcrid, 'com:Id') | |
67 | _add_sub_element(request, 'itv:Partition') | |
68 | user_info = _add_sub_element(get_playlist, 'tem:userInfo') | |
69 | _add_sub_element(user_info, 'itv:Broadcaster').text = 'Itv' | |
70 | _add_sub_element(user_info, 'itv:DM') | |
71 | _add_sub_element(user_info, 'itv:RevenueScienceValue') | |
72 | _add_sub_element(user_info, 'itv:SessionId') | |
73 | _add_sub_element(user_info, 'itv:SsoToken') | |
74 | _add_sub_element(user_info, 'itv:UserToken') | |
75 | site_info = _add_sub_element(get_playlist, 'tem:siteInfo') | |
76 | _add_sub_element(site_info, 'itv:AdvertisingRestriction').text = 'None' | |
77 | _add_sub_element(site_info, 'itv:AdvertisingSite').text = 'ITV' | |
78 | _add_sub_element(site_info, 'itv:AdvertisingType').text = 'Any' | |
79 | _add_sub_element(site_info, 'itv:Area').text = 'ITVPLAYER.VIDEO' | |
80 | _add_sub_element(site_info, 'itv:Category') | |
81 | _add_sub_element(site_info, 'itv:Platform').text = 'DotCom' | |
82 | _add_sub_element(site_info, 'itv:Site').text = 'ItvCom' | |
83 | device_info = _add_sub_element(get_playlist, 'tem:deviceInfo') | |
84 | _add_sub_element(device_info, 'itv:ScreenSize').text = 'Big' | |
85 | player_info = _add_sub_element(get_playlist, 'tem:playerInfo') | |
86 | _add_sub_element(player_info, 'itv:Version').text = '2' | |
87 | ||
88 | headers = self.geo_verification_headers() | |
89 | headers.update({ | |
90 | 'Content-Type': 'text/xml; charset=utf-8', | |
91 | 'SOAPAction': 'http://tempuri.org/PlaylistService/GetPlaylist', | |
92 | }) | |
93 | resp_env = self._download_xml( | |
94 | params['data-playlist-url'], video_id, | |
95 | headers=headers, data=etree.tostring(req_env)) | |
96 | playlist = xpath_element(resp_env, './/Playlist') | |
97 | if playlist is None: | |
98 | fault_string = xpath_text(resp_env, './/faultstring') | |
99 | raise ExtractorError('%s said: %s' % (self.IE_NAME, fault_string)) | |
100 | title = xpath_text(playlist, 'EpisodeTitle', fatal=True) | |
f592ff98 RA |
101 | video_element = xpath_element(playlist, 'VideoEntries/Video', fatal=True) |
102 | media_files = xpath_element(video_element, 'MediaFiles', fatal=True) | |
a71b8d3b RA |
103 | rtmp_url = media_files.attrib['base'] |
104 | ||
105 | formats = [] | |
106 | for media_file in media_files.findall('MediaFile'): | |
107 | play_path = xpath_text(media_file, 'URL') | |
108 | if not play_path: | |
109 | continue | |
110 | tbr = int_or_none(media_file.get('bitrate'), 1000) | |
111 | formats.append({ | |
112 | 'format_id': 'rtmp' + ('-%d' % tbr if tbr else ''), | |
113 | 'url': rtmp_url, | |
114 | 'play_path': play_path, | |
115 | 'tbr': tbr, | |
116 | 'ext': 'flv', | |
117 | }) | |
118 | ||
119 | ios_playlist_url = params.get('data-video-playlist') | |
120 | hmac = params.get('data-video-hmac') | |
121 | if ios_playlist_url and hmac: | |
122 | headers = self.geo_verification_headers() | |
123 | headers.update({ | |
124 | 'Accept': 'application/vnd.itv.vod.playlist.v2+json', | |
125 | 'Content-Type': 'application/json', | |
126 | 'hmac': hmac.upper(), | |
127 | }) | |
128 | ios_playlist = self._download_json( | |
129 | ios_playlist_url, video_id, data=json.dumps({ | |
130 | 'user': { | |
131 | 'itvUserId': '', | |
132 | 'entitlements': [], | |
133 | 'token': '' | |
134 | }, | |
135 | 'device': { | |
136 | 'manufacturer': 'Apple', | |
137 | 'model': 'iPad', | |
138 | 'os': { | |
139 | 'name': 'iPhone OS', | |
140 | 'version': '9.3', | |
141 | 'type': 'ios' | |
142 | } | |
143 | }, | |
144 | 'client': { | |
145 | 'version': '4.1', | |
146 | 'id': 'browser' | |
147 | }, | |
148 | 'variantAvailability': { | |
149 | 'featureset': { | |
150 | 'min': ['hls', 'aes'], | |
151 | 'max': ['hls', 'aes'] | |
152 | }, | |
153 | 'platformTag': 'mobile' | |
154 | } | |
155 | }).encode(), headers=headers, fatal=False) | |
156 | if ios_playlist: | |
157 | video_data = ios_playlist.get('Playlist', {}).get('Video', {}) | |
158 | ios_base_url = video_data.get('Base') | |
159 | for media_file in video_data.get('MediaFiles', []): | |
160 | href = media_file.get('Href') | |
161 | if not href: | |
162 | continue | |
163 | if ios_base_url: | |
164 | href = ios_base_url + href | |
165 | ext = determine_ext(href) | |
166 | if ext == 'm3u8': | |
167 | formats.extend(self._extract_m3u8_formats(href, video_id, 'mp4', m3u8_id='hls', fatal=False)) | |
168 | else: | |
169 | formats.append({ | |
170 | 'url': href, | |
171 | }) | |
172 | self._sort_formats(formats) | |
173 | ||
f592ff98 RA |
174 | subtitles = {} |
175 | for caption_url in video_element.findall('ClosedCaptioningURIs/URL'): | |
176 | if not caption_url.text: | |
177 | continue | |
178 | ext = determine_ext(caption_url.text, 'ttml') | |
179 | subtitles.setdefault('en', []).append({ | |
4edeac5b | 180 | 'url': caption_url.text, |
f592ff98 RA |
181 | 'ext': 'ttml' if ext == 'xml' else ext, |
182 | }) | |
183 | ||
a71b8d3b RA |
184 | return { |
185 | 'id': video_id, | |
186 | 'title': title, | |
187 | 'formats': formats, | |
f592ff98 | 188 | 'subtitles': subtitles, |
a71b8d3b RA |
189 | 'episode_title': title, |
190 | 'episode_number': int_or_none(xpath_text(playlist, 'EpisodeNumber')), | |
191 | 'series': xpath_text(playlist, 'ProgrammeTitle'), | |
192 | 'duartion': parse_duration(xpath_text(playlist, 'Duration')), | |
193 | } |