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.

206 lines
6.9 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. 'Downloading metadata JSON')
  43. date = 'media_live_since'
  44. media_type = 'livestream'
  45. if metadata.get('media_type') == 'video':
  46. media_type = 'video'
  47. date = 'media_date_added'
  48. video_meta = metadata.get(media_type, [])[0]
  49. title = video_meta.get('media_status')
  50. alt_title = video_meta.get('media_title')
  51. description = clean_html(
  52. video_meta.get('media_description') or
  53. video_meta.get('media_description_md'))
  54. duration = float_or_none(video_meta.get('media_duration'))
  55. uploader = video_meta.get('media_user_name')
  56. views = int_or_none(video_meta.get('media_views'))
  57. timestamp = parse_iso8601(video_meta.get(date), ' ')
  58. categories = [video_meta.get('category_name')]
  59. thumbs = [
  60. {'url': thumb_base + video_meta.get('media_thumbnail'),
  61. 'width': 320,
  62. 'height': 180},
  63. {'url': thumb_base + video_meta.get('media_thumbnail_large'),
  64. 'width': 768,
  65. 'height': 432},
  66. ]
  67. return {
  68. 'id': video_id,
  69. 'title': title,
  70. 'alt_title': alt_title,
  71. 'description': description,
  72. 'ext': 'mp4',
  73. 'thumbnails': thumbs,
  74. 'duration': duration,
  75. 'uploader': uploader,
  76. 'view_count': views,
  77. 'timestamp': timestamp,
  78. 'categories': categories,
  79. }
  80. def _real_extract(self, url):
  81. video_id = self._match_id(url)
  82. player_config = self._download_json(
  83. 'https://www.hitbox.tv/api/player/config/video/%s' % video_id,
  84. video_id, 'Downloading video JSON')
  85. formats = []
  86. for video in player_config['clip']['bitrates']:
  87. label = video.get('label')
  88. if label == 'Auto':
  89. continue
  90. video_url = video.get('url')
  91. if not video_url:
  92. continue
  93. bitrate = int_or_none(video.get('bitrate'))
  94. if determine_ext(video_url) == 'm3u8':
  95. if not video_url.startswith('http'):
  96. continue
  97. formats.append({
  98. 'url': video_url,
  99. 'ext': 'mp4',
  100. 'tbr': bitrate,
  101. 'format_note': label,
  102. 'protocol': 'm3u8_native',
  103. })
  104. else:
  105. formats.append({
  106. 'url': video_url,
  107. 'tbr': bitrate,
  108. 'format_note': label,
  109. })
  110. self._sort_formats(formats)
  111. metadata = self._extract_metadata(
  112. 'https://www.hitbox.tv/api/media/video',
  113. video_id)
  114. metadata['formats'] = formats
  115. return metadata
  116. class HitboxLiveIE(HitboxIE):
  117. IE_NAME = 'hitbox:live'
  118. _VALID_URL = r'https?://(?:www\.)?hitbox\.tv/(?!video)(?P<id>.+)'
  119. _TEST = {
  120. 'url': 'http://www.hitbox.tv/dimak',
  121. 'info_dict': {
  122. 'id': 'dimak',
  123. 'ext': 'mp4',
  124. 'description': 'md5:c9f80fa4410bc588d7faa40003fc7d0e',
  125. 'timestamp': int,
  126. 'upload_date': compat_str,
  127. 'title': compat_str,
  128. 'uploader': 'Dimak',
  129. },
  130. 'params': {
  131. # live
  132. 'skip_download': True,
  133. },
  134. }
  135. def _real_extract(self, url):
  136. video_id = self._match_id(url)
  137. player_config = self._download_json(
  138. 'https://www.hitbox.tv/api/player/config/live/%s' % video_id,
  139. video_id)
  140. formats = []
  141. cdns = player_config.get('cdns')
  142. servers = []
  143. for cdn in cdns:
  144. # Subscribe URLs are not playable
  145. if cdn.get('rtmpSubscribe') is True:
  146. continue
  147. base_url = cdn.get('netConnectionUrl')
  148. host = re.search('.+\.([^\.]+\.[^\./]+)/.+', base_url).group(1)
  149. if base_url not in servers:
  150. servers.append(base_url)
  151. for stream in cdn.get('bitrates'):
  152. label = stream.get('label')
  153. if label == 'Auto':
  154. continue
  155. stream_url = stream.get('url')
  156. if not stream_url:
  157. continue
  158. bitrate = int_or_none(stream.get('bitrate'))
  159. if stream.get('provider') == 'hls' or determine_ext(stream_url) == 'm3u8':
  160. if not stream_url.startswith('http'):
  161. continue
  162. formats.append({
  163. 'url': stream_url,
  164. 'ext': 'mp4',
  165. 'tbr': bitrate,
  166. 'format_note': label,
  167. 'rtmp_live': True,
  168. })
  169. else:
  170. formats.append({
  171. 'url': '%s/%s' % (base_url, stream_url),
  172. 'ext': 'mp4',
  173. 'tbr': bitrate,
  174. 'rtmp_live': True,
  175. 'format_note': host,
  176. 'page_url': url,
  177. 'player_url': 'http://www.hitbox.tv/static/player/flowplayer/flowplayer.commercial-3.2.16.swf',
  178. })
  179. self._sort_formats(formats)
  180. metadata = self._extract_metadata(
  181. 'https://www.hitbox.tv/api/media/live',
  182. video_id)
  183. metadata['formats'] = formats
  184. metadata['is_live'] = True
  185. metadata['title'] = self._live_title(metadata.get('title'))
  186. return metadata