logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

hitbox.py (7414B)


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