logo

youtube-dl

[mirror] Download/Watch videos from video hosters

beampro.py (6462B)


  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>[^?#&]+)'
  85. _TESTS = [{
  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. 'url': 'https://mixer.com/streamer?vod=IxFno1rqC0S_XJ1a2yGgNw',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'https://mixer.com/streamer?vod=Rh3LY0VAqkGpEQUe2pN-ig',
  109. 'only_matching': True,
  110. }]
  111. @staticmethod
  112. def _extract_format(vod, vod_type):
  113. if not vod.get('baseUrl'):
  114. return []
  115. if vod_type == 'hls':
  116. filename, protocol = 'manifest.m3u8', 'm3u8_native'
  117. elif vod_type == 'raw':
  118. filename, protocol = 'source.mp4', 'https'
  119. else:
  120. assert False
  121. data = vod.get('data') if isinstance(vod.get('data'), dict) else {}
  122. format_id = [vod_type]
  123. if isinstance(data.get('Height'), compat_str):
  124. format_id.append('%sp' % data['Height'])
  125. return [{
  126. 'url': urljoin(vod['baseUrl'], filename),
  127. 'format_id': '-'.join(format_id),
  128. 'ext': 'mp4',
  129. 'protocol': protocol,
  130. 'width': int_or_none(data.get('Width')),
  131. 'height': int_or_none(data.get('Height')),
  132. 'fps': int_or_none(data.get('Fps')),
  133. 'tbr': int_or_none(data.get('Bitrate'), 1000),
  134. }]
  135. def _real_extract(self, url):
  136. vod_id = self._match_id(url)
  137. vod_info = self._download_json(
  138. '%s/recordings/%s' % (self._API_BASE, vod_id), vod_id)
  139. state = vod_info.get('state')
  140. if state != 'AVAILABLE':
  141. raise ExtractorError(
  142. 'VOD %s is not available (state: %s)' % (vod_id, state),
  143. expected=True)
  144. formats = []
  145. thumbnail_url = None
  146. for vod in vod_info['vods']:
  147. vod_type = vod.get('format')
  148. if vod_type in ('hls', 'raw'):
  149. formats.extend(self._extract_format(vod, vod_type))
  150. elif vod_type == 'thumbnail':
  151. thumbnail_url = urljoin(vod.get('baseUrl'), 'source.png')
  152. self._sort_formats(formats)
  153. info = {
  154. 'id': vod_id,
  155. 'title': vod_info.get('name') or vod_id,
  156. 'duration': float_or_none(vod_info.get('duration')),
  157. 'thumbnail': thumbnail_url,
  158. 'timestamp': parse_iso8601(vod_info.get('createdAt')),
  159. 'view_count': int_or_none(vod_info.get('viewsTotal')),
  160. 'formats': formats,
  161. }
  162. info.update(self._extract_channel_info(vod_info.get('channel') or {}))
  163. return info