itv.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from .brightcove import BrightcoveNewIE
  7. from ..utils import (
  8. determine_ext,
  9. extract_attributes,
  10. get_element_by_class,
  11. JSON_LD_RE,
  12. merge_dicts,
  13. parse_duration,
  14. smuggle_url,
  15. strip_or_none,
  16. url_or_none,
  17. )
  18. class ITVIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:www\.)?itv\.com/hub/[^/]+/(?P<id>[0-9a-zA-Z]+)'
  20. _GEO_COUNTRIES = ['GB']
  21. _TESTS = [{
  22. 'url': 'https://www.itv.com/hub/liar/2a4547a0012',
  23. 'info_dict': {
  24. 'id': '2a4547a0012',
  25. 'ext': 'mp4',
  26. 'title': 'Liar - Series 2 - Episode 6',
  27. 'description': 'md5:d0f91536569dec79ea184f0a44cca089',
  28. 'series': 'Liar',
  29. 'season_number': 2,
  30. 'episode_number': 6,
  31. },
  32. 'params': {
  33. # m3u8 download
  34. 'skip_download': True,
  35. },
  36. }, {
  37. # unavailable via data-playlist-url
  38. 'url': 'https://www.itv.com/hub/through-the-keyhole/2a2271a0033',
  39. 'only_matching': True,
  40. }, {
  41. # InvalidVodcrid
  42. 'url': 'https://www.itv.com/hub/james-martins-saturday-morning/2a5159a0034',
  43. 'only_matching': True,
  44. }, {
  45. # ContentUnavailable
  46. 'url': 'https://www.itv.com/hub/whos-doing-the-dishes/2a2898a0024',
  47. 'only_matching': True,
  48. }]
  49. def _real_extract(self, url):
  50. video_id = self._match_id(url)
  51. webpage = self._download_webpage(url, video_id)
  52. params = extract_attributes(self._search_regex(
  53. r'(?s)(<[^>]+id="video"[^>]*>)', webpage, 'params'))
  54. ios_playlist_url = params.get('data-video-playlist') or params['data-video-id']
  55. hmac = params['data-video-hmac']
  56. headers = self.geo_verification_headers()
  57. headers.update({
  58. 'Accept': 'application/vnd.itv.vod.playlist.v2+json',
  59. 'Content-Type': 'application/json',
  60. 'hmac': hmac.upper(),
  61. })
  62. ios_playlist = self._download_json(
  63. ios_playlist_url, video_id, data=json.dumps({
  64. 'user': {
  65. 'itvUserId': '',
  66. 'entitlements': [],
  67. 'token': ''
  68. },
  69. 'device': {
  70. 'manufacturer': 'Safari',
  71. 'model': '5',
  72. 'os': {
  73. 'name': 'Windows NT',
  74. 'version': '6.1',
  75. 'type': 'desktop'
  76. }
  77. },
  78. 'client': {
  79. 'version': '4.1',
  80. 'id': 'browser'
  81. },
  82. 'variantAvailability': {
  83. 'featureset': {
  84. 'min': ['hls', 'aes', 'outband-webvtt'],
  85. 'max': ['hls', 'aes', 'outband-webvtt']
  86. },
  87. 'platformTag': 'dotcom'
  88. }
  89. }).encode(), headers=headers)
  90. video_data = ios_playlist['Playlist']['Video']
  91. ios_base_url = video_data.get('Base')
  92. formats = []
  93. for media_file in (video_data.get('MediaFiles') or []):
  94. href = media_file.get('Href')
  95. if not href:
  96. continue
  97. if ios_base_url:
  98. href = ios_base_url + href
  99. ext = determine_ext(href)
  100. if ext == 'm3u8':
  101. formats.extend(self._extract_m3u8_formats(
  102. href, video_id, 'mp4', entry_protocol='m3u8_native',
  103. m3u8_id='hls', fatal=False))
  104. else:
  105. formats.append({
  106. 'url': href,
  107. })
  108. self._sort_formats(formats)
  109. subtitles = {}
  110. subs = video_data.get('Subtitles') or []
  111. for sub in subs:
  112. if not isinstance(sub, dict):
  113. continue
  114. href = url_or_none(sub.get('Href'))
  115. if not href:
  116. continue
  117. subtitles.setdefault('en', []).append({
  118. 'url': href,
  119. 'ext': determine_ext(href, 'vtt'),
  120. })
  121. info = self._search_json_ld(webpage, video_id, default={})
  122. if not info:
  123. json_ld = self._parse_json(self._search_regex(
  124. JSON_LD_RE, webpage, 'JSON-LD', '{}',
  125. group='json_ld'), video_id, fatal=False)
  126. if json_ld and json_ld.get('@type') == 'BreadcrumbList':
  127. for ile in (json_ld.get('itemListElement:') or []):
  128. item = ile.get('item:') or {}
  129. if item.get('@type') == 'TVEpisode':
  130. item['@context'] = 'http://schema.org'
  131. info = self._json_ld(item, video_id, fatal=False) or {}
  132. break
  133. return merge_dicts({
  134. 'id': video_id,
  135. 'title': self._html_search_meta(['og:title', 'twitter:title'], webpage),
  136. 'formats': formats,
  137. 'subtitles': subtitles,
  138. 'duration': parse_duration(video_data.get('Duration')),
  139. 'description': strip_or_none(get_element_by_class('episode-info__synopsis', webpage)),
  140. }, info)
  141. class ITVBTCCIE(InfoExtractor):
  142. _VALID_URL = r'https?://(?:www\.)?itv\.com/btcc/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  143. _TEST = {
  144. 'url': 'http://www.itv.com/btcc/races/btcc-2018-all-the-action-from-brands-hatch',
  145. 'info_dict': {
  146. 'id': 'btcc-2018-all-the-action-from-brands-hatch',
  147. 'title': 'BTCC 2018: All the action from Brands Hatch',
  148. },
  149. 'playlist_mincount': 9,
  150. }
  151. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/1582188683001/HkiHLnNRx_default/index.html?videoId=%s'
  152. def _real_extract(self, url):
  153. playlist_id = self._match_id(url)
  154. webpage = self._download_webpage(url, playlist_id)
  155. entries = [
  156. self.url_result(
  157. smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % video_id, {
  158. # ITV does not like some GB IP ranges, so here are some
  159. # IP blocks it accepts
  160. 'geo_ip_blocks': [
  161. '193.113.0.0/16', '54.36.162.0/23', '159.65.16.0/21'
  162. ],
  163. 'referrer': url,
  164. }),
  165. ie=BrightcoveNewIE.ie_key(), video_id=video_id)
  166. for video_id in re.findall(r'data-video-id=["\'](\d+)', webpage)]
  167. title = self._og_search_title(webpage, fatal=False)
  168. return self.playlist_result(entries, playlist_id, title)