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.

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