]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/itv.py
debian/control: Mark compliance with policy 4.1.1. No further changes needed.
[youtubedl] / youtube_dl / 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
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_str,
11 compat_etree_register_namespace,
12 )
13 from ..utils import (
14 extract_attributes,
15 xpath_with_ns,
16 xpath_element,
17 xpath_text,
18 int_or_none,
19 parse_duration,
20 ExtractorError,
21 determine_ext,
22 )
23
24
25 class ITVIE(InfoExtractor):
26 _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
27 _GEO_COUNTRIES = ['GB']
28 _TEST = {
29 'url': 'http://www.itv.com/hub/mr-bean-animated-series/2a2936a0053',
30 'info_dict': {
31 'id': '2a2936a0053',
32 'ext': 'flv',
33 'title': 'Home Movie',
34 },
35 'params': {
36 # rtmp download
37 'skip_download': True,
38 },
39 }
40
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'))
46
47 ns_map = {
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',
52 }
53 for ns, full_ns in ns_map.items():
54 compat_etree_register_namespace(ns, full_ns)
55
56 def _add_ns(name):
57 return xpath_with_ns(name, ns_map)
58
59 def _add_sub_element(element, name):
60 return etree.SubElement(element, _add_ns(name))
61
62 production_id = (
63 params.get('data-video-autoplay-id') or
64 '%s#001' % (
65 params.get('data-video-episode-id') or
66 video_id.replace('a', '/')))
67
68 req_env = etree.Element(_add_ns('soapenv:Envelope'))
69 _add_sub_element(req_env, 'soapenv:Header')
70 body = _add_sub_element(req_env, 'soapenv:Body')
71 get_playlist = _add_sub_element(body, ('tem:GetPlaylist'))
72 request = _add_sub_element(get_playlist, 'tem:request')
73 _add_sub_element(request, 'itv:ProductionId').text = production_id
74 _add_sub_element(request, 'itv:RequestGuid').text = compat_str(uuid.uuid4()).upper()
75 vodcrid = _add_sub_element(request, 'itv:Vodcrid')
76 _add_sub_element(vodcrid, 'com:Id')
77 _add_sub_element(request, 'itv:Partition')
78 user_info = _add_sub_element(get_playlist, 'tem:userInfo')
79 _add_sub_element(user_info, 'itv:Broadcaster').text = 'Itv'
80 _add_sub_element(user_info, 'itv:DM')
81 _add_sub_element(user_info, 'itv:RevenueScienceValue')
82 _add_sub_element(user_info, 'itv:SessionId')
83 _add_sub_element(user_info, 'itv:SsoToken')
84 _add_sub_element(user_info, 'itv:UserToken')
85 site_info = _add_sub_element(get_playlist, 'tem:siteInfo')
86 _add_sub_element(site_info, 'itv:AdvertisingRestriction').text = 'None'
87 _add_sub_element(site_info, 'itv:AdvertisingSite').text = 'ITV'
88 _add_sub_element(site_info, 'itv:AdvertisingType').text = 'Any'
89 _add_sub_element(site_info, 'itv:Area').text = 'ITVPLAYER.VIDEO'
90 _add_sub_element(site_info, 'itv:Category')
91 _add_sub_element(site_info, 'itv:Platform').text = 'DotCom'
92 _add_sub_element(site_info, 'itv:Site').text = 'ItvCom'
93 device_info = _add_sub_element(get_playlist, 'tem:deviceInfo')
94 _add_sub_element(device_info, 'itv:ScreenSize').text = 'Big'
95 player_info = _add_sub_element(get_playlist, 'tem:playerInfo')
96 _add_sub_element(player_info, 'itv:Version').text = '2'
97
98 headers = self.geo_verification_headers()
99 headers.update({
100 'Content-Type': 'text/xml; charset=utf-8',
101 'SOAPAction': 'http://tempuri.org/PlaylistService/GetPlaylist',
102 })
103 resp_env = self._download_xml(
104 params['data-playlist-url'], video_id,
105 headers=headers, data=etree.tostring(req_env))
106 playlist = xpath_element(resp_env, './/Playlist')
107 if playlist is None:
108 fault_code = xpath_text(resp_env, './/faultcode')
109 fault_string = xpath_text(resp_env, './/faultstring')
110 if fault_code == 'InvalidGeoRegion':
111 self.raise_geo_restricted(
112 msg=fault_string, countries=self._GEO_COUNTRIES)
113 raise ExtractorError('%s said: %s' % (self.IE_NAME, fault_string))
114 title = xpath_text(playlist, 'EpisodeTitle', fatal=True)
115 video_element = xpath_element(playlist, 'VideoEntries/Video', fatal=True)
116 media_files = xpath_element(video_element, 'MediaFiles', fatal=True)
117 rtmp_url = media_files.attrib['base']
118
119 formats = []
120 for media_file in media_files.findall('MediaFile'):
121 play_path = xpath_text(media_file, 'URL')
122 if not play_path:
123 continue
124 tbr = int_or_none(media_file.get('bitrate'), 1000)
125 f = {
126 'format_id': 'rtmp' + ('-%d' % tbr if tbr else ''),
127 'play_path': play_path,
128 # Providing this swfVfy allows to avoid truncated downloads
129 'player_url': 'http://www.itv.com/mercury/Mercury_VideoPlayer.swf',
130 'page_url': url,
131 'tbr': tbr,
132 'ext': 'flv',
133 }
134 app = self._search_regex(
135 'rtmpe?://[^/]+/(.+)$', rtmp_url, 'app', default=None)
136 if app:
137 f.update({
138 'url': rtmp_url.split('?', 1)[0],
139 'app': app,
140 })
141 else:
142 f['url'] = rtmp_url
143 formats.append(f)
144
145 ios_playlist_url = params.get('data-video-playlist')
146 hmac = params.get('data-video-hmac')
147 if ios_playlist_url and hmac:
148 headers = self.geo_verification_headers()
149 headers.update({
150 'Accept': 'application/vnd.itv.vod.playlist.v2+json',
151 'Content-Type': 'application/json',
152 'hmac': hmac.upper(),
153 })
154 ios_playlist = self._download_json(
155 ios_playlist_url, video_id, data=json.dumps({
156 'user': {
157 'itvUserId': '',
158 'entitlements': [],
159 'token': ''
160 },
161 'device': {
162 'manufacturer': 'Apple',
163 'model': 'iPad',
164 'os': {
165 'name': 'iPhone OS',
166 'version': '9.3',
167 'type': 'ios'
168 }
169 },
170 'client': {
171 'version': '4.1',
172 'id': 'browser'
173 },
174 'variantAvailability': {
175 'featureset': {
176 'min': ['hls', 'aes'],
177 'max': ['hls', 'aes']
178 },
179 'platformTag': 'mobile'
180 }
181 }).encode(), headers=headers, fatal=False)
182 if ios_playlist:
183 video_data = ios_playlist.get('Playlist', {}).get('Video', {})
184 ios_base_url = video_data.get('Base')
185 for media_file in video_data.get('MediaFiles', []):
186 href = media_file.get('Href')
187 if not href:
188 continue
189 if ios_base_url:
190 href = ios_base_url + href
191 ext = determine_ext(href)
192 if ext == 'm3u8':
193 formats.extend(self._extract_m3u8_formats(
194 href, video_id, 'mp4', entry_protocol='m3u8_native',
195 m3u8_id='hls', fatal=False))
196 else:
197 formats.append({
198 'url': href,
199 })
200 self._sort_formats(formats)
201
202 subtitles = {}
203 for caption_url in video_element.findall('ClosedCaptioningURIs/URL'):
204 if not caption_url.text:
205 continue
206 ext = determine_ext(caption_url.text, 'ttml')
207 subtitles.setdefault('en', []).append({
208 'url': caption_url.text,
209 'ext': 'ttml' if ext == 'xml' else ext,
210 })
211
212 info = self._search_json_ld(webpage, video_id, default={})
213 info.update({
214 'id': video_id,
215 'title': title,
216 'formats': formats,
217 'subtitles': subtitles,
218 'episode_title': title,
219 'episode_number': int_or_none(xpath_text(playlist, 'EpisodeNumber')),
220 'series': xpath_text(playlist, 'ProgrammeTitle'),
221 'duartion': parse_duration(xpath_text(playlist, 'Duration')),
222 })
223 return info