hbo.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. # #EXT-X-BYTERANGE is not supported by native hls downloader
  90. # and ffmpeg (#10955)
  91. # formats.extend(self._extract_m3u8_formats(
  92. # video_url.replace('.tar', '/base_index.m3u8'),
  93. # video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  94. continue
  95. elif source.tag == 'dash':
  96. formats.extend(self._extract_mpd_formats(
  97. video_url.replace('.tar', '/manifest.mpd'),
  98. video_id, mpd_id='dash', fatal=False))
  99. else:
  100. format_info = self._FORMATS_INFO.get(source.tag, {})
  101. formats.append({
  102. 'format_id': 'http-%s' % source.tag,
  103. 'url': video_url,
  104. 'width': format_info.get('width'),
  105. 'height': format_info.get('height'),
  106. })
  107. self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
  108. thumbnails = []
  109. card_sizes = xpath_element(video_data, 'titleCardSizes')
  110. if card_sizes is not None:
  111. for size in card_sizes:
  112. path = xpath_text(size, 'path')
  113. if not path:
  114. continue
  115. width = int_or_none(size.get('width'))
  116. thumbnails.append({
  117. 'id': width,
  118. 'url': path,
  119. 'width': width,
  120. })
  121. return {
  122. 'id': video_id,
  123. 'title': title,
  124. 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
  125. 'formats': formats,
  126. 'thumbnails': thumbnails,
  127. }
  128. class HBOIE(HBOBaseIE):
  129. IE_NAME = 'hbo'
  130. _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/video\.html\?.*vid=(?P<id>[0-9]+)'
  131. _TEST = {
  132. 'url': 'http://www.hbo.com/video/video.html?autoplay=true&g=u&vid=1437839',
  133. 'md5': '2c6a6bc1222c7e91cb3334dad1746e5a',
  134. 'info_dict': {
  135. 'id': '1437839',
  136. 'ext': 'mp4',
  137. 'title': 'Ep. 64 Clip: Encryption',
  138. 'thumbnail': r're:https?://.*\.jpg$',
  139. 'duration': 1072,
  140. }
  141. }
  142. def _real_extract(self, url):
  143. video_id = self._match_id(url)
  144. return self._extract_from_id(video_id)
  145. class HBOEpisodeIE(HBOBaseIE):
  146. IE_NAME = 'hbo:episode'
  147. _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?P<path>(?!video)(?:(?:[^/]+/)+video|watch-free-episodes)/(?P<id>[0-9a-z-]+))(?:\.html)?'
  148. _TESTS = [{
  149. 'url': 'http://www.hbo.com/girls/episodes/5/52-i-love-you-baby/video/ep-52-inside-the-episode.html?autoplay=true',
  150. 'md5': '61ead79b9c0dfa8d3d4b07ef4ac556fb',
  151. 'info_dict': {
  152. 'id': '1439518',
  153. 'display_id': 'ep-52-inside-the-episode',
  154. 'ext': 'mp4',
  155. 'title': 'Ep. 52: Inside the Episode',
  156. 'thumbnail': r're:https?://.*\.jpg$',
  157. 'duration': 240,
  158. },
  159. }, {
  160. 'url': 'http://www.hbo.com/game-of-thrones/about/video/season-5-invitation-to-the-set.html?autoplay=true',
  161. 'only_matching': True,
  162. }, {
  163. 'url': 'http://www.hbo.com/watch-free-episodes/last-week-tonight-with-john-oliver',
  164. 'only_matching': True,
  165. }]
  166. def _real_extract(self, url):
  167. path, display_id = re.match(self._VALID_URL, url).groups()
  168. content = self._download_json(
  169. 'http://www.hbo.com/api/content/' + path, display_id)['content']
  170. video_id = compat_str((content.get('parsed', {}).get(
  171. 'common:FullBleedVideo', {}) or content['selectedEpisode'])['videoId'])
  172. info_dict = self._extract_from_id(video_id)
  173. info_dict['display_id'] = display_id
  174. return info_dict