beampro.py 5.6 KB

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