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