logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

hbo.py (6128B)


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