]>
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): | |
23 | _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-z]+)' | |
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) | |
101 | media_files = xpath_element(playlist, 'VideoEntries/Video/MediaFiles', fatal=True) | |
102 | rtmp_url = media_files.attrib['base'] | |
103 | ||
104 | formats = [] | |
105 | for media_file in media_files.findall('MediaFile'): | |
106 | play_path = xpath_text(media_file, 'URL') | |
107 | if not play_path: | |
108 | continue | |
109 | tbr = int_or_none(media_file.get('bitrate'), 1000) | |
110 | formats.append({ | |
111 | 'format_id': 'rtmp' + ('-%d' % tbr if tbr else ''), | |
112 | 'url': rtmp_url, | |
113 | 'play_path': play_path, | |
114 | 'tbr': tbr, | |
115 | 'ext': 'flv', | |
116 | }) | |
117 | ||
118 | ios_playlist_url = params.get('data-video-playlist') | |
119 | hmac = params.get('data-video-hmac') | |
120 | if ios_playlist_url and hmac: | |
121 | headers = self.geo_verification_headers() | |
122 | headers.update({ | |
123 | 'Accept': 'application/vnd.itv.vod.playlist.v2+json', | |
124 | 'Content-Type': 'application/json', | |
125 | 'hmac': hmac.upper(), | |
126 | }) | |
127 | ios_playlist = self._download_json( | |
128 | ios_playlist_url, video_id, data=json.dumps({ | |
129 | 'user': { | |
130 | 'itvUserId': '', | |
131 | 'entitlements': [], | |
132 | 'token': '' | |
133 | }, | |
134 | 'device': { | |
135 | 'manufacturer': 'Apple', | |
136 | 'model': 'iPad', | |
137 | 'os': { | |
138 | 'name': 'iPhone OS', | |
139 | 'version': '9.3', | |
140 | 'type': 'ios' | |
141 | } | |
142 | }, | |
143 | 'client': { | |
144 | 'version': '4.1', | |
145 | 'id': 'browser' | |
146 | }, | |
147 | 'variantAvailability': { | |
148 | 'featureset': { | |
149 | 'min': ['hls', 'aes'], | |
150 | 'max': ['hls', 'aes'] | |
151 | }, | |
152 | 'platformTag': 'mobile' | |
153 | } | |
154 | }).encode(), headers=headers, fatal=False) | |
155 | if ios_playlist: | |
156 | video_data = ios_playlist.get('Playlist', {}).get('Video', {}) | |
157 | ios_base_url = video_data.get('Base') | |
158 | for media_file in video_data.get('MediaFiles', []): | |
159 | href = media_file.get('Href') | |
160 | if not href: | |
161 | continue | |
162 | if ios_base_url: | |
163 | href = ios_base_url + href | |
164 | ext = determine_ext(href) | |
165 | if ext == 'm3u8': | |
166 | formats.extend(self._extract_m3u8_formats(href, video_id, 'mp4', m3u8_id='hls', fatal=False)) | |
167 | else: | |
168 | formats.append({ | |
169 | 'url': href, | |
170 | }) | |
171 | self._sort_formats(formats) | |
172 | ||
173 | return { | |
174 | 'id': video_id, | |
175 | 'title': title, | |
176 | 'formats': formats, | |
177 | 'episode_title': title, | |
178 | 'episode_number': int_or_none(xpath_text(playlist, 'EpisodeNumber')), | |
179 | 'series': xpath_text(playlist, 'ProgrammeTitle'), | |
180 | 'duartion': parse_duration(xpath_text(playlist, 'Duration')), | |
181 | } |