chzzk.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import functools
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. UserNotLive,
  5. float_or_none,
  6. int_or_none,
  7. parse_iso8601,
  8. url_or_none,
  9. )
  10. from ..utils.traversal import traverse_obj
  11. class CHZZKLiveIE(InfoExtractor):
  12. IE_NAME = 'chzzk:live'
  13. _VALID_URL = r'https?://chzzk\.naver\.com/live/(?P<id>[\da-f]+)'
  14. _TESTS = [{
  15. 'url': 'https://chzzk.naver.com/live/c68b8ef525fb3d2fa146344d84991753',
  16. 'info_dict': {
  17. 'id': 'c68b8ef525fb3d2fa146344d84991753',
  18. 'ext': 'mp4',
  19. 'title': str,
  20. 'channel': '진짜도현',
  21. 'channel_id': 'c68b8ef525fb3d2fa146344d84991753',
  22. 'channel_is_verified': False,
  23. 'thumbnail': r're:^https?://.*\.jpg$',
  24. 'timestamp': 1705510344,
  25. 'upload_date': '20240117',
  26. 'live_status': 'is_live',
  27. 'view_count': int,
  28. 'concurrent_view_count': int,
  29. },
  30. 'skip': 'The channel is not currently live',
  31. }]
  32. def _real_extract(self, url):
  33. channel_id = self._match_id(url)
  34. live_detail = self._download_json(
  35. f'https://api.chzzk.naver.com/service/v2/channels/{channel_id}/live-detail', channel_id,
  36. note='Downloading channel info', errnote='Unable to download channel info')['content']
  37. if live_detail.get('status') == 'CLOSE':
  38. raise UserNotLive(video_id=channel_id)
  39. live_playback = self._parse_json(live_detail['livePlaybackJson'], channel_id)
  40. thumbnails = []
  41. thumbnail_template = traverse_obj(
  42. live_playback, ('thumbnail', 'snapshotThumbnailTemplate', {url_or_none}))
  43. if thumbnail_template and '{type}' in thumbnail_template:
  44. for width in traverse_obj(live_playback, ('thumbnail', 'types', ..., {str})):
  45. thumbnails.append({
  46. 'id': width,
  47. 'url': thumbnail_template.replace('{type}', width),
  48. 'width': int_or_none(width),
  49. })
  50. formats, subtitles = [], {}
  51. for media in traverse_obj(live_playback, ('media', lambda _, v: url_or_none(v['path']))):
  52. is_low_latency = media.get('mediaId') == 'LLHLS'
  53. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  54. media['path'], channel_id, 'mp4', fatal=False, live=True,
  55. m3u8_id='hls-ll' if is_low_latency else 'hls')
  56. for f in fmts:
  57. if is_low_latency:
  58. f['source_preference'] = -2
  59. if '-afragalow.stream-audio.stream' in f['format_id']:
  60. f['quality'] = -2
  61. formats.extend(fmts)
  62. self._merge_subtitles(subs, target=subtitles)
  63. return {
  64. 'id': channel_id,
  65. 'is_live': True,
  66. 'formats': formats,
  67. 'subtitles': subtitles,
  68. 'thumbnails': thumbnails,
  69. **traverse_obj(live_detail, {
  70. 'title': ('liveTitle', {str}),
  71. 'timestamp': ('openDate', {functools.partial(parse_iso8601, delimiter=' ')}),
  72. 'concurrent_view_count': ('concurrentUserCount', {int_or_none}),
  73. 'view_count': ('accumulateCount', {int_or_none}),
  74. 'channel': ('channel', 'channelName', {str}),
  75. 'channel_id': ('channel', 'channelId', {str}),
  76. 'channel_is_verified': ('channel', 'verifiedMark', {bool}),
  77. }),
  78. }
  79. class CHZZKVideoIE(InfoExtractor):
  80. IE_NAME = 'chzzk:video'
  81. _VALID_URL = r'https?://chzzk\.naver\.com/video/(?P<id>\d+)'
  82. _TESTS = [{
  83. 'url': 'https://chzzk.naver.com/video/1754',
  84. 'md5': 'b0c0c1bb888d913b93d702b1512c7f06',
  85. 'info_dict': {
  86. 'id': '1754',
  87. 'ext': 'mp4',
  88. 'title': '치지직 테스트 방송',
  89. 'channel': '침착맨',
  90. 'channel_id': 'bb382c2c0cc9fa7c86ab3b037fb5799c',
  91. 'channel_is_verified': False,
  92. 'thumbnail': r're:^https?://.*\.jpg$',
  93. 'duration': 15577,
  94. 'timestamp': 1702970505.417,
  95. 'upload_date': '20231219',
  96. 'view_count': int,
  97. },
  98. }]
  99. def _real_extract(self, url):
  100. video_id = self._match_id(url)
  101. video_meta = self._download_json(
  102. f'https://api.chzzk.naver.com/service/v2/videos/{video_id}', video_id,
  103. note='Downloading video info', errnote='Unable to download video info')['content']
  104. formats, subtitles = self._extract_mpd_formats_and_subtitles(
  105. f'https://apis.naver.com/neonplayer/vodplay/v1/playback/{video_meta["videoId"]}', video_id,
  106. query={
  107. 'key': video_meta['inKey'],
  108. 'env': 'real',
  109. 'lc': 'en_US',
  110. 'cpl': 'en_US',
  111. }, note='Downloading video playback', errnote='Unable to download video playback')
  112. return {
  113. 'id': video_id,
  114. 'formats': formats,
  115. 'subtitles': subtitles,
  116. **traverse_obj(video_meta, {
  117. 'title': ('videoTitle', {str}),
  118. 'thumbnail': ('thumbnailImageUrl', {url_or_none}),
  119. 'timestamp': ('publishDateAt', {functools.partial(float_or_none, scale=1000)}),
  120. 'view_count': ('readCount', {int_or_none}),
  121. 'duration': ('duration', {int_or_none}),
  122. 'channel': ('channel', 'channelName', {str}),
  123. 'channel_id': ('channel', 'channelId', {str}),
  124. 'channel_is_verified': ('channel', 'verifiedMark', {bool}),
  125. }),
  126. }