You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
7.8 KiB

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