hitbox.py 5.1 KB

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