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.

188 lines
6.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. clean_html,
  7. compat_str,
  8. float_or_none,
  9. int_or_none,
  10. parse_iso8601,
  11. try_get,
  12. urljoin,
  13. )
  14. class BeamProBaseIE(InfoExtractor):
  15. _API_BASE = 'https://mixer.com/api/v1'
  16. _RATINGS = {'family': 0, 'teen': 13, '18+': 18}
  17. def _extract_channel_info(self, chan):
  18. user_id = chan.get('userId') or try_get(chan, lambda x: x['user']['id'])
  19. return {
  20. 'uploader': chan.get('token') or try_get(
  21. chan, lambda x: x['user']['username'], compat_str),
  22. 'uploader_id': compat_str(user_id) if user_id else None,
  23. 'age_limit': self._RATINGS.get(chan.get('audience')),
  24. }
  25. class BeamProLiveIE(BeamProBaseIE):
  26. IE_NAME = 'Mixer:live'
  27. _VALID_URL = r'https?://(?:\w+\.)?(?:beam\.pro|mixer\.com)/(?P<id>[^/?#&]+)'
  28. _TEST = {
  29. 'url': 'http://mixer.com/niterhayven',
  30. 'info_dict': {
  31. 'id': '261562',
  32. 'ext': 'mp4',
  33. 'title': 'Introducing The Witcher 3 // The Grind Starts Now!',
  34. 'description': 'md5:0b161ac080f15fe05d18a07adb44a74d',
  35. 'thumbnail': r're:https://.*\.jpg$',
  36. 'timestamp': 1483477281,
  37. 'upload_date': '20170103',
  38. 'uploader': 'niterhayven',
  39. 'uploader_id': '373396',
  40. 'age_limit': 18,
  41. 'is_live': True,
  42. 'view_count': int,
  43. },
  44. 'skip': 'niterhayven is offline',
  45. 'params': {
  46. 'skip_download': True,
  47. },
  48. }
  49. _MANIFEST_URL_TEMPLATE = '%s/channels/%%s/manifest.%%s' % BeamProBaseIE._API_BASE
  50. @classmethod
  51. def suitable(cls, url):
  52. return False if BeamProVodIE.suitable(url) else super(BeamProLiveIE, cls).suitable(url)
  53. def _real_extract(self, url):
  54. channel_name = self._match_id(url)
  55. chan = self._download_json(
  56. '%s/channels/%s' % (self._API_BASE, channel_name), channel_name)
  57. if chan.get('online') is False:
  58. raise ExtractorError(
  59. '{0} is offline'.format(channel_name), expected=True)
  60. channel_id = chan['id']
  61. def manifest_url(kind):
  62. return self._MANIFEST_URL_TEMPLATE % (channel_id, kind)
  63. formats = self._extract_m3u8_formats(
  64. manifest_url('m3u8'), channel_name, ext='mp4', m3u8_id='hls',
  65. fatal=False)
  66. formats.extend(self._extract_smil_formats(
  67. manifest_url('smil'), channel_name, fatal=False))
  68. self._sort_formats(formats)
  69. info = {
  70. 'id': compat_str(chan.get('id') or channel_name),
  71. 'title': self._live_title(chan.get('name') or channel_name),
  72. 'description': clean_html(chan.get('description')),
  73. 'thumbnail': try_get(
  74. chan, lambda x: x['thumbnail']['url'], compat_str),
  75. 'timestamp': parse_iso8601(chan.get('updatedAt')),
  76. 'is_live': True,
  77. 'view_count': int_or_none(chan.get('viewersTotal')),
  78. 'formats': formats,
  79. }
  80. info.update(self._extract_channel_info(chan))
  81. return info
  82. class BeamProVodIE(BeamProBaseIE):
  83. IE_NAME = 'Mixer:vod'
  84. _VALID_URL = r'https?://(?:\w+\.)?(?:beam\.pro|mixer\.com)/[^/?#&]+\?.*?\bvod=(?P<id>\d+)'
  85. _TEST = {
  86. 'url': 'https://mixer.com/willow8714?vod=2259830',
  87. 'md5': 'b2431e6e8347dc92ebafb565d368b76b',
  88. 'info_dict': {
  89. 'id': '2259830',
  90. 'ext': 'mp4',
  91. 'title': 'willow8714\'s Channel',
  92. 'duration': 6828.15,
  93. 'thumbnail': r're:https://.*source\.png$',
  94. 'timestamp': 1494046474,
  95. 'upload_date': '20170506',
  96. 'uploader': 'willow8714',
  97. 'uploader_id': '6085379',
  98. 'age_limit': 13,
  99. 'view_count': int,
  100. },
  101. 'params': {
  102. 'skip_download': True,
  103. },
  104. }
  105. @staticmethod
  106. def _extract_format(vod, vod_type):
  107. if not vod.get('baseUrl'):
  108. return []
  109. if vod_type == 'hls':
  110. filename, protocol = 'manifest.m3u8', 'm3u8_native'
  111. elif vod_type == 'raw':
  112. filename, protocol = 'source.mp4', 'https'
  113. else:
  114. assert False
  115. data = vod.get('data') if isinstance(vod.get('data'), dict) else {}
  116. format_id = [vod_type]
  117. if isinstance(data.get('Height'), compat_str):
  118. format_id.append('%sp' % data['Height'])
  119. return [{
  120. 'url': urljoin(vod['baseUrl'], filename),
  121. 'format_id': '-'.join(format_id),
  122. 'ext': 'mp4',
  123. 'protocol': protocol,
  124. 'width': int_or_none(data.get('Width')),
  125. 'height': int_or_none(data.get('Height')),
  126. 'fps': int_or_none(data.get('Fps')),
  127. 'tbr': int_or_none(data.get('Bitrate'), 1000),
  128. }]
  129. def _real_extract(self, url):
  130. vod_id = self._match_id(url)
  131. vod_info = self._download_json(
  132. '%s/recordings/%s' % (self._API_BASE, vod_id), vod_id)
  133. state = vod_info.get('state')
  134. if state != 'AVAILABLE':
  135. raise ExtractorError(
  136. 'VOD %s is not available (state: %s)' % (vod_id, state),
  137. expected=True)
  138. formats = []
  139. thumbnail_url = None
  140. for vod in vod_info['vods']:
  141. vod_type = vod.get('format')
  142. if vod_type in ('hls', 'raw'):
  143. formats.extend(self._extract_format(vod, vod_type))
  144. elif vod_type == 'thumbnail':
  145. thumbnail_url = urljoin(vod.get('baseUrl'), 'source.png')
  146. self._sort_formats(formats)
  147. info = {
  148. 'id': vod_id,
  149. 'title': vod_info.get('name') or vod_id,
  150. 'duration': float_or_none(vod_info.get('duration')),
  151. 'thumbnail': thumbnail_url,
  152. 'timestamp': parse_iso8601(vod_info.get('createdAt')),
  153. 'view_count': int_or_none(vod_info.get('viewsTotal')),
  154. 'formats': formats,
  155. }
  156. info.update(self._extract_channel_info(vod_info.get('channel') or {}))
  157. return info