beampro.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. int_or_none,
  9. parse_iso8601,
  10. try_get,
  11. )
  12. class BeamProLiveIE(InfoExtractor):
  13. IE_NAME = 'Beam:live'
  14. _VALID_URL = r'https?://(?:\w+.)?beam.pro/(?P<id>[^?]+)$'
  15. _API_CHANNEL = 'https://beam.pro/api/v1/channels/{0}'
  16. _API_MANIFEST = 'https://beam.pro/api/v1/channels/{0}/manifest.m3u8'
  17. _RATINGS = {'family': 0, 'teen': 13, '18+': 18}
  18. _TEST = {
  19. 'url': 'http://www.beam.pro/niterhayven',
  20. 'info_dict': {
  21. 'id': '261562',
  22. 'ext': 'mp4',
  23. 'uploader': 'niterhayven',
  24. 'timestamp': 1483477281,
  25. 'age_limit': 18,
  26. 'title': 'Introducing The Witcher 3 // The Grind Starts Now!',
  27. 'thumbnail': r're:https://.*\.jpg$',
  28. 'upload_date': '20170103',
  29. 'uploader_id': 373396,
  30. 'description': 'md5:0b161ac080f15fe05d18a07adb44a74d',
  31. 'is_live': True,
  32. },
  33. 'skip': 'niterhayven is offline',
  34. 'params': {
  35. 'skip_download': True,
  36. },
  37. }
  38. def _real_extract(self, url):
  39. channel_id = self._match_id(url)
  40. chan_data = self._download_json(self._API_CHANNEL.format(channel_id), channel_id)
  41. if not chan_data.get('online'):
  42. raise ExtractorError('{0} is offline'.format(channel_id), expected=True)
  43. formats = self._extract_m3u8_formats(
  44. self._API_MANIFEST.format(chan_data.get('id')), channel_id, ext='mp4')
  45. self._sort_formats(formats)
  46. info = {}
  47. info['formats'] = formats
  48. if chan_data:
  49. info.update(self._extract_info(chan_data))
  50. if not info.get('title'):
  51. info['title'] = self._live_title(channel_id)
  52. if not info.get('id'): # barely possible but just in case
  53. info['id'] = compat_str(abs(hash(channel_id)) % (10 ** 8))
  54. return info
  55. def _extract_info(self, info):
  56. thumbnail = try_get(info, lambda x: x['thumbnail']['url'], compat_str)
  57. username = try_get(info, lambda x: x['user']['url'], compat_str)
  58. video_id = compat_str(info['id']) if info.get('id') else None
  59. rating = info.get('audience')
  60. return {
  61. 'id': video_id,
  62. 'title': info.get('name'),
  63. 'description': clean_html(info.get('description')),
  64. 'age_limit': self._RATINGS[rating] if rating in self._RATINGS else None,
  65. 'is_live': True if info.get('online') else False,
  66. 'timestamp': parse_iso8601(info.get('updatedAt')),
  67. 'uploader': info.get('token') or username,
  68. 'uploader_id': int_or_none(info.get('userId')),
  69. 'view_count': int_or_none(info.get('viewersTotal')),
  70. 'thumbnail': thumbnail,
  71. }