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.

182 lines
6.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. clean_html,
  7. parse_iso8601,
  8. float_or_none,
  9. int_or_none,
  10. compat_str,
  11. determine_ext,
  12. )
  13. class HitboxIE(InfoExtractor):
  14. IE_NAME = 'hitbox'
  15. _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/video/(?P<id>[0-9]+)'
  16. _TEST = {
  17. 'url': 'http://www.hitbox.tv/video/203213',
  18. 'info_dict': {
  19. 'id': '203213',
  20. 'title': 'hitbox @ gamescom, Sub Button Hype extended, Giveaway - hitbox News Update with Oxy',
  21. 'alt_title': 'hitboxlive - Aug 9th #6',
  22. 'description': '',
  23. 'ext': 'mp4',
  24. 'thumbnail': 're:^https?://.*\.jpg$',
  25. 'duration': 215.1666,
  26. 'resolution': 'HD 720p',
  27. 'uploader': 'hitboxlive',
  28. 'view_count': int,
  29. 'timestamp': 1407576133,
  30. 'upload_date': '20140809',
  31. 'categories': ['Live Show'],
  32. },
  33. 'params': {
  34. # m3u8 download
  35. 'skip_download': True,
  36. },
  37. }
  38. def _extract_metadata(self, url, video_id):
  39. thumb_base = 'https://edge.sf.hitbox.tv'
  40. metadata = self._download_json(
  41. '%s/%s' % (url, video_id), video_id)
  42. date = 'media_live_since'
  43. media_type = 'livestream'
  44. if metadata.get('media_type') == 'video':
  45. media_type = 'video'
  46. date = 'media_date_added'
  47. video_meta = metadata.get(media_type, [])[0]
  48. title = video_meta.get('media_status')
  49. alt_title = video_meta.get('media_title')
  50. description = clean_html(
  51. video_meta.get('media_description') or
  52. video_meta.get('media_description_md'))
  53. duration = float_or_none(video_meta.get('media_duration'))
  54. uploader = video_meta.get('media_user_name')
  55. views = int_or_none(video_meta.get('media_views'))
  56. timestamp = parse_iso8601(video_meta.get(date), ' ')
  57. categories = [video_meta.get('category_name')]
  58. thumbs = [
  59. {'url': thumb_base + video_meta.get('media_thumbnail'),
  60. 'width': 320,
  61. 'height': 180},
  62. {'url': thumb_base + video_meta.get('media_thumbnail_large'),
  63. 'width': 768,
  64. 'height': 432},
  65. ]
  66. return {
  67. 'id': video_id,
  68. 'title': title,
  69. 'alt_title': alt_title,
  70. 'description': description,
  71. 'ext': 'mp4',
  72. 'thumbnails': thumbs,
  73. 'duration': duration,
  74. 'uploader': uploader,
  75. 'view_count': views,
  76. 'timestamp': timestamp,
  77. 'categories': categories,
  78. }
  79. def _real_extract(self, url):
  80. video_id = self._match_id(url)
  81. metadata = self._extract_metadata(
  82. 'https://www.hitbox.tv/api/media/video',
  83. video_id)
  84. player_config = self._download_json(
  85. 'https://www.hitbox.tv/api/player/config/video/%s' % video_id,
  86. video_id)
  87. clip = player_config.get('clip')
  88. video_url = clip.get('url')
  89. res = clip.get('bitrates', [])[0].get('label')
  90. metadata['resolution'] = res
  91. metadata['url'] = video_url
  92. metadata['protocol'] = 'm3u8'
  93. return metadata
  94. class HitboxLiveIE(HitboxIE):
  95. IE_NAME = 'hitbox:live'
  96. _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/(?!video)(?P<id>.+)'
  97. _TEST = {
  98. 'url': 'http://www.hitbox.tv/dimak',
  99. 'info_dict': {
  100. 'id': 'dimak',
  101. 'ext': 'mp4',
  102. 'description': 'md5:c9f80fa4410bc588d7faa40003fc7d0e',
  103. 'timestamp': int,
  104. 'upload_date': compat_str,
  105. 'title': compat_str,
  106. 'uploader': 'Dimak',
  107. },
  108. 'params': {
  109. # live
  110. 'skip_download': True,
  111. },
  112. }
  113. def _real_extract(self, url):
  114. video_id = self._match_id(url)
  115. metadata = self._extract_metadata(
  116. 'https://www.hitbox.tv/api/media/live',
  117. video_id)
  118. player_config = self._download_json(
  119. 'https://www.hitbox.tv/api/player/config/live/%s' % video_id,
  120. video_id)
  121. formats = []
  122. cdns = player_config.get('cdns')
  123. servers = []
  124. for cdn in cdns:
  125. base_url = cdn.get('netConnectionUrl')
  126. host = re.search('.+\.([^\.]+\.[^\./]+)/.+', base_url).group(1)
  127. if base_url not in servers:
  128. servers.append(base_url)
  129. for stream in cdn.get('bitrates'):
  130. label = stream.get('label')
  131. if label == 'Auto':
  132. continue
  133. stream_url = stream.get('url')
  134. if not stream_url:
  135. continue
  136. bitrate = int_or_none(stream.get('bitrate'))
  137. if stream.get('provider') == 'hls' or determine_ext(stream_url) == 'm3u8':
  138. if not stream_url.startswith('http'):
  139. continue
  140. formats.append({
  141. 'url': stream_url,
  142. 'ext': 'mp4',
  143. 'tbr': bitrate,
  144. 'format_note': label,
  145. 'rtmp_live': True,
  146. })
  147. else:
  148. formats.append({
  149. 'url': '%s/%s' % (base_url, stream_url),
  150. 'ext': 'mp4',
  151. 'tbr': bitrate,
  152. 'rtmp_live': True,
  153. 'format_note': host,
  154. 'page_url': url,
  155. 'player_url': 'http://www.hitbox.tv/static/player/flowplayer/flowplayer.commercial-3.2.16.swf',
  156. })
  157. self._sort_formats(formats)
  158. metadata['formats'] = formats
  159. metadata['is_live'] = True
  160. metadata['title'] = self._live_title(metadata.get('title'))
  161. return metadata