hbo.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. join_nonempty,
  6. parse_duration,
  7. urljoin,
  8. xpath_element,
  9. xpath_text,
  10. )
  11. class HBOBaseIE(InfoExtractor):
  12. _FORMATS_INFO = {
  13. 'pro7': {
  14. 'width': 1280,
  15. 'height': 720,
  16. },
  17. '1920': {
  18. 'width': 1280,
  19. 'height': 720,
  20. },
  21. 'pro6': {
  22. 'width': 768,
  23. 'height': 432,
  24. },
  25. '640': {
  26. 'width': 768,
  27. 'height': 432,
  28. },
  29. 'pro5': {
  30. 'width': 640,
  31. 'height': 360,
  32. },
  33. 'highwifi': {
  34. 'width': 640,
  35. 'height': 360,
  36. },
  37. 'high3g': {
  38. 'width': 640,
  39. 'height': 360,
  40. },
  41. 'medwifi': {
  42. 'width': 400,
  43. 'height': 224,
  44. },
  45. 'med3g': {
  46. 'width': 400,
  47. 'height': 224,
  48. },
  49. }
  50. def _extract_info(self, url, display_id):
  51. video_data = self._download_xml(url, display_id)
  52. video_id = xpath_text(video_data, 'id', fatal=True)
  53. episode_title = title = xpath_text(video_data, 'title', fatal=True)
  54. series = xpath_text(video_data, 'program')
  55. if series:
  56. title = f'{series} - {title}'
  57. formats = []
  58. for source in xpath_element(video_data, 'videos', 'sources', True):
  59. if source.tag == 'size':
  60. path = xpath_text(source, './/path')
  61. if not path:
  62. continue
  63. width = source.attrib.get('width')
  64. format_info = self._FORMATS_INFO.get(width, {})
  65. height = format_info.get('height')
  66. fmt = {
  67. 'url': path,
  68. 'format_id': join_nonempty('http'. height and f'{height}p'),
  69. 'width': format_info.get('width'),
  70. 'height': height,
  71. }
  72. rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
  73. if rtmp:
  74. fmt.update({
  75. 'url': rtmp.group('url'),
  76. 'play_path': rtmp.group('playpath'),
  77. 'app': rtmp.group('app'),
  78. 'ext': 'flv',
  79. 'format_id': fmt['format_id'].replace('http', 'rtmp'),
  80. })
  81. formats.append(fmt)
  82. else:
  83. video_url = source.text
  84. if not video_url:
  85. continue
  86. if source.tag == 'tarball':
  87. formats.extend(self._extract_m3u8_formats(
  88. video_url.replace('.tar', '/base_index_w8.m3u8'),
  89. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  90. elif source.tag == 'hls':
  91. m3u8_formats = self._extract_m3u8_formats(
  92. video_url.replace('.tar', '/base_index.m3u8'),
  93. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  94. for f in m3u8_formats:
  95. if f.get('vcodec') == 'none' and not f.get('tbr'):
  96. f['tbr'] = int_or_none(self._search_regex(
  97. r'-(\d+)k/', f['url'], 'tbr', default=None))
  98. formats.extend(m3u8_formats)
  99. elif source.tag == 'dash':
  100. formats.extend(self._extract_mpd_formats(
  101. video_url.replace('.tar', '/manifest.mpd'),
  102. video_id, mpd_id='dash', fatal=False))
  103. else:
  104. format_info = self._FORMATS_INFO.get(source.tag, {})
  105. formats.append({
  106. 'format_id': f'http-{source.tag}',
  107. 'url': video_url,
  108. 'width': format_info.get('width'),
  109. 'height': format_info.get('height'),
  110. })
  111. thumbnails = []
  112. card_sizes = xpath_element(video_data, 'titleCardSizes')
  113. if card_sizes is not None:
  114. for size in card_sizes:
  115. path = xpath_text(size, 'path')
  116. if not path:
  117. continue
  118. width = int_or_none(size.get('width'))
  119. thumbnails.append({
  120. 'id': width,
  121. 'url': path,
  122. 'width': width,
  123. })
  124. subtitles = None
  125. caption_url = xpath_text(video_data, 'captionUrl')
  126. if caption_url:
  127. subtitles = {
  128. 'en': [{
  129. 'url': caption_url,
  130. 'ext': 'ttml',
  131. }],
  132. }
  133. return {
  134. 'id': video_id,
  135. 'title': title,
  136. 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
  137. 'series': series,
  138. 'episode': episode_title,
  139. 'formats': formats,
  140. 'thumbnails': thumbnails,
  141. 'subtitles': subtitles,
  142. }
  143. class HBOIE(HBOBaseIE):
  144. IE_NAME = 'hbo'
  145. _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?:video|embed)(?:/[^/]+)*/(?P<id>[^/?#]+)'
  146. _TEST = {
  147. 'url': 'https://www.hbo.com/video/game-of-thrones/seasons/season-8/videos/trailer',
  148. 'md5': '8126210656f433c452a21367f9ad85b3',
  149. 'info_dict': {
  150. 'id': '22113301',
  151. 'ext': 'mp4',
  152. 'title': 'Game of Thrones - Trailer',
  153. },
  154. 'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
  155. }
  156. def _real_extract(self, url):
  157. display_id = self._match_id(url)
  158. webpage = self._download_webpage(url, display_id)
  159. location_path = self._parse_json(self._html_search_regex(
  160. r'data-state="({.+?})"', webpage, 'state'), display_id)['video']['locationUrl']
  161. return self._extract_info(urljoin(url, location_path), display_id)