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.

195 lines
6.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. xpath_text,
  8. xpath_element,
  9. int_or_none,
  10. parse_duration,
  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_from_id(self, video_id):
  52. video_data = self._download_xml(
  53. 'http://render.lv3.hbo.com/data/content/global/videos/data/%s.xml' % video_id, video_id)
  54. title = xpath_text(video_data, 'title', 'title', True)
  55. formats = []
  56. for source in xpath_element(video_data, 'videos', 'sources', True):
  57. if source.tag == 'size':
  58. path = xpath_text(source, './/path')
  59. if not path:
  60. continue
  61. width = source.attrib.get('width')
  62. format_info = self._FORMATS_INFO.get(width, {})
  63. height = format_info.get('height')
  64. fmt = {
  65. 'url': path,
  66. 'format_id': 'http%s' % ('-%dp' % height if height else ''),
  67. 'width': format_info.get('width'),
  68. 'height': height,
  69. }
  70. rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
  71. if rtmp:
  72. fmt.update({
  73. 'url': rtmp.group('url'),
  74. 'play_path': rtmp.group('playpath'),
  75. 'app': rtmp.group('app'),
  76. 'ext': 'flv',
  77. 'format_id': fmt['format_id'].replace('http', 'rtmp'),
  78. })
  79. formats.append(fmt)
  80. else:
  81. video_url = source.text
  82. if not video_url:
  83. continue
  84. if source.tag == 'tarball':
  85. formats.extend(self._extract_m3u8_formats(
  86. video_url.replace('.tar', '/base_index_w8.m3u8'),
  87. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  88. elif source.tag == 'hls':
  89. m3u8_formats = self._extract_m3u8_formats(
  90. video_url.replace('.tar', '/base_index.m3u8'),
  91. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
  92. for f in m3u8_formats:
  93. if f.get('vcodec') == 'none' and not f.get('tbr'):
  94. f['tbr'] = int_or_none(self._search_regex(
  95. r'-(\d+)k/', f['url'], 'tbr', default=None))
  96. formats.extend(m3u8_formats)
  97. elif source.tag == 'dash':
  98. formats.extend(self._extract_mpd_formats(
  99. video_url.replace('.tar', '/manifest.mpd'),
  100. video_id, mpd_id='dash', fatal=False))
  101. else:
  102. format_info = self._FORMATS_INFO.get(source.tag, {})
  103. formats.append({
  104. 'format_id': 'http-%s' % source.tag,
  105. 'url': video_url,
  106. 'width': format_info.get('width'),
  107. 'height': format_info.get('height'),
  108. })
  109. self._sort_formats(formats)
  110. thumbnails = []
  111. card_sizes = xpath_element(video_data, 'titleCardSizes')
  112. if card_sizes is not None:
  113. for size in card_sizes:
  114. path = xpath_text(size, 'path')
  115. if not path:
  116. continue
  117. width = int_or_none(size.get('width'))
  118. thumbnails.append({
  119. 'id': width,
  120. 'url': path,
  121. 'width': width,
  122. })
  123. return {
  124. 'id': video_id,
  125. 'title': title,
  126. 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
  127. 'formats': formats,
  128. 'thumbnails': thumbnails,
  129. }
  130. class HBOIE(HBOBaseIE):
  131. IE_NAME = 'hbo'
  132. _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/video\.html\?.*vid=(?P<id>[0-9]+)'
  133. _TEST = {
  134. 'url': 'http://www.hbo.com/video/video.html?autoplay=true&g=u&vid=1437839',
  135. 'md5': '2c6a6bc1222c7e91cb3334dad1746e5a',
  136. 'info_dict': {
  137. 'id': '1437839',
  138. 'ext': 'mp4',
  139. 'title': 'Ep. 64 Clip: Encryption',
  140. 'thumbnail': r're:https?://.*\.jpg$',
  141. 'duration': 1072,
  142. }
  143. }
  144. def _real_extract(self, url):
  145. video_id = self._match_id(url)
  146. return self._extract_from_id(video_id)
  147. class HBOEpisodeIE(HBOBaseIE):
  148. IE_NAME = 'hbo:episode'
  149. _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?P<path>(?!video)(?:(?:[^/]+/)+video|watch-free-episodes)/(?P<id>[0-9a-z-]+))(?:\.html)?'
  150. _TESTS = [{
  151. 'url': 'http://www.hbo.com/girls/episodes/5/52-i-love-you-baby/video/ep-52-inside-the-episode.html?autoplay=true',
  152. 'md5': '61ead79b9c0dfa8d3d4b07ef4ac556fb',
  153. 'info_dict': {
  154. 'id': '1439518',
  155. 'display_id': 'ep-52-inside-the-episode',
  156. 'ext': 'mp4',
  157. 'title': 'Ep. 52: Inside the Episode',
  158. 'thumbnail': r're:https?://.*\.jpg$',
  159. 'duration': 240,
  160. },
  161. }, {
  162. 'url': 'http://www.hbo.com/game-of-thrones/about/video/season-5-invitation-to-the-set.html?autoplay=true',
  163. 'only_matching': True,
  164. }, {
  165. 'url': 'http://www.hbo.com/watch-free-episodes/last-week-tonight-with-john-oliver',
  166. 'only_matching': True,
  167. }]
  168. def _real_extract(self, url):
  169. path, display_id = re.match(self._VALID_URL, url).groups()
  170. content = self._download_json(
  171. 'http://www.hbo.com/api/content/' + path, display_id)['content']
  172. video_id = compat_str((content.get('parsed', {}).get(
  173. 'common:FullBleedVideo', {}) or content['selectedEpisode'])['videoId'])
  174. info_dict = self._extract_from_id(video_id)
  175. info_dict['display_id'] = display_id
  176. return info_dict