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.

164 lines
5.4 KiB

  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. )
  11. class HBOBaseIE(InfoExtractor):
  12. _FORMATS_INFO = {
  13. '1920': {
  14. 'width': 1280,
  15. 'height': 720,
  16. },
  17. '640': {
  18. 'width': 768,
  19. 'height': 432,
  20. },
  21. 'highwifi': {
  22. 'width': 640,
  23. 'height': 360,
  24. },
  25. 'high3g': {
  26. 'width': 640,
  27. 'height': 360,
  28. },
  29. 'medwifi': {
  30. 'width': 400,
  31. 'height': 224,
  32. },
  33. 'med3g': {
  34. 'width': 400,
  35. 'height': 224,
  36. },
  37. }
  38. def _extract_from_id(self, video_id):
  39. video_data = self._download_xml(
  40. 'http://render.lv3.hbo.com/data/content/global/videos/data/%s.xml' % video_id, video_id)
  41. title = xpath_text(video_data, 'title', 'title', True)
  42. formats = []
  43. for source in xpath_element(video_data, 'videos', 'sources', True):
  44. if source.tag == 'size':
  45. path = xpath_text(source, './/path')
  46. if not path:
  47. continue
  48. width = source.attrib.get('width')
  49. format_info = self._FORMATS_INFO.get(width, {})
  50. height = format_info.get('height')
  51. fmt = {
  52. 'url': path,
  53. 'format_id': 'http%s' % ('-%dp' % height if height else ''),
  54. 'width': format_info.get('width'),
  55. 'height': height,
  56. }
  57. rtmp = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>.+))/(?P<playpath>mp4:.+)$', path)
  58. if rtmp:
  59. fmt.update({
  60. 'url': rtmp.group('url'),
  61. 'play_path': rtmp.group('playpath'),
  62. 'app': rtmp.group('app'),
  63. 'ext': 'flv',
  64. 'format_id': fmt['format_id'].replace('http', 'rtmp'),
  65. })
  66. formats.append(fmt)
  67. else:
  68. video_url = source.text
  69. if not video_url:
  70. continue
  71. if source.tag == 'tarball':
  72. formats.extend(self._extract_m3u8_formats(
  73. video_url.replace('.tar', '/base_index_w8.m3u8'),
  74. video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  75. else:
  76. format_info = self._FORMATS_INFO.get(source.tag, {})
  77. formats.append({
  78. 'format_id': 'http-%s' % source.tag,
  79. 'url': video_url,
  80. 'width': format_info.get('width'),
  81. 'height': format_info.get('height'),
  82. })
  83. self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
  84. thumbnails = []
  85. card_sizes = xpath_element(video_data, 'titleCardSizes')
  86. if card_sizes is not None:
  87. for size in card_sizes:
  88. path = xpath_text(size, 'path')
  89. if not path:
  90. continue
  91. width = int_or_none(size.get('width'))
  92. thumbnails.append({
  93. 'id': width,
  94. 'url': path,
  95. 'width': width,
  96. })
  97. return {
  98. 'id': video_id,
  99. 'title': title,
  100. 'duration': parse_duration(xpath_text(video_data, 'duration/tv14')),
  101. 'formats': formats,
  102. 'thumbnails': thumbnails,
  103. }
  104. class HBOIE(HBOBaseIE):
  105. _VALID_URL = r'https?://(?:www\.)?hbo\.com/video/video\.html\?.*vid=(?P<id>[0-9]+)'
  106. _TEST = {
  107. 'url': 'http://www.hbo.com/video/video.html?autoplay=true&g=u&vid=1437839',
  108. 'md5': '1c33253f0c7782142c993c0ba62a8753',
  109. 'info_dict': {
  110. 'id': '1437839',
  111. 'ext': 'mp4',
  112. 'title': 'Ep. 64 Clip: Encryption',
  113. 'thumbnail': r're:https?://.*\.jpg$',
  114. 'duration': 1072,
  115. }
  116. }
  117. def _real_extract(self, url):
  118. video_id = self._match_id(url)
  119. return self._extract_from_id(video_id)
  120. class HBOEpisodeIE(HBOBaseIE):
  121. _VALID_URL = r'https?://(?:www\.)?hbo\.com/(?!video)([^/]+/)+video/(?P<id>[0-9a-z-]+)\.html'
  122. _TESTS = [{
  123. 'url': 'http://www.hbo.com/girls/episodes/5/52-i-love-you-baby/video/ep-52-inside-the-episode.html?autoplay=true',
  124. 'md5': '689132b253cc0ab7434237fc3a293210',
  125. 'info_dict': {
  126. 'id': '1439518',
  127. 'display_id': 'ep-52-inside-the-episode',
  128. 'ext': 'mp4',
  129. 'title': 'Ep. 52: Inside the Episode',
  130. 'thumbnail': r're:https?://.*\.jpg$',
  131. 'duration': 240,
  132. },
  133. }, {
  134. 'url': 'http://www.hbo.com/game-of-thrones/about/video/season-5-invitation-to-the-set.html?autoplay=true',
  135. 'only_matching': True,
  136. }]
  137. def _real_extract(self, url):
  138. display_id = self._match_id(url)
  139. webpage = self._download_webpage(url, display_id)
  140. video_id = self._search_regex(
  141. r'(?P<q1>[\'"])videoId(?P=q1)\s*:\s*(?P<q2>[\'"])(?P<video_id>\d+)(?P=q2)',
  142. webpage, 'video ID', group='video_id')
  143. info_dict = self._extract_from_id(video_id)
  144. info_dict['display_id'] = display_id
  145. return info_dict